Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 创建全局多维数组Swift 4_Ios_Arrays_Swift - Fatal编程技术网

Ios 创建全局多维数组Swift 4

Ios 创建全局多维数组Swift 4,ios,arrays,swift,Ios,Arrays,Swift,我试图将数据“productID”保存为Int,“quantity”保存为Int,“price”保存为swift4的全局数组中的Double 每个productID键都应该包含“数量”和“价格”列 如何在全局类中创建这样的数组?将产品保留为结构: struct Product { let id: Int let quantity: Int let price: Double } 然后定义数组: internal static var productArray = [Product]

我试图将数据“productID”保存为Int,“quantity”保存为Int,“price”保存为swift4的全局数组中的Double

每个productID键都应该包含“数量”和“价格”列


如何在全局类中创建这样的数组?

将产品保留为结构:

struct Product {
  let id: Int
  let quantity: Int
  let price: Double
}
然后定义数组:

internal static var productArray = [Product]()
并添加您的产品

let product = Product(id: 1, quantity: 99, price: 1.99)
productArray.append(product)

内部静态
前缀允许您在整个应用程序中访问阵列。但是,我认为您不希望在应用程序中使用此模式。

使用结构是一个很好的解决方案。我不知道内部修饰符+1.
let product = Product(id: 1, quantity: 99, price: 1.99)
productArray.append(product)