Swift 嵌套数组错误
我知道这是一个非常新的问题,但这已经困扰了我好几天了,我似乎找不到一个我真正理解的解决方案 我试图创建一个嵌套数组来存储纬度和经度,但是它的Xcode/playerd抛出了一个EXC_BAD_指令错误 我想声明、初始化和打印数组的内容。我做错了什么Swift 嵌套数组错误,swift,Swift,我知道这是一个非常新的问题,但这已经困扰了我好几天了,我似乎找不到一个我真正理解的解决方案 我试图创建一个嵌套数组来存储纬度和经度,但是它的Xcode/playerd抛出了一个EXC_BAD_指令错误 我想声明、初始化和打印数组的内容。我做错了什么 var arrayLocations:[[Float]] = [] arrayLocations[0] = [27.1750199, 78.0399665] print("\(arrayLocations[0][0]) and \(arrayLo
var arrayLocations:[[Float]] = []
arrayLocations[0] = [27.1750199, 78.0399665]
print("\(arrayLocations[0][0]) and \(arrayLocations[0][1])")
无法为索引0赋值,因为如果数组为空,则不存在索引0 您必须
追加
或插入
项目:
arrayLocations.append([27.1750199, 78.0399665])
无法为索引0赋值,因为如果数组为空,则不存在索引0 您必须
追加
或插入
项目:
arrayLocations.append([27.1750199, 78.0399665])
正如他在信上所说的
无法将值分配给索引0,因为在以下情况下没有索引0 数组为空 您必须追加或插入项目:
arrayLocations.append([27.1750199, 78.0399665])
arrayLocations.append([27.1750199,78.0399665])
我建议您使用这个集合
,这样您的代码就不会经常崩溃
extension Collection where Indices.Iterator.Element == Index {
/// Returns the element at the specified index if it is within bounds, otherwise nil.
subscript (safe index: Index) -> Generator.Element? {
return indices.contains(index) ? self[index] : nil
}
}
通过使用它,您可以只使用if let
块
样本:
这确保了即使您尝试访问索引为0
的对象,您的代码也不会崩溃
建议:
注意:这个建议不是答案的一部分,而是改进代码的建议
看起来您正在尝试创建一个纬度和经度数组。对纬度和经度对象使用数组不是很明智。我建议您改为创建一个对象,它可以是struct
或typeAlias
例如:
struct CoordinateStruct {
var longitude: Float
var latitude: Float
}
/* or */
typealias CoordinateTypeAlias = (latitude: Float, longitude: Float)
/************************* */
// Your code would then look like this
var arrayLocations:[CoordinateStruct] = []
arrayLocations.append(CoordinateStruct(longitude: 27.12312312, latitude: 78.123123))
/* or */
var arrayLocations:[CoordinateTypeAlias] = []
arrayLocations.append((27.12312312, 78.123123))
// Which would make accessing them very easy and readable, like this.
if let aLocation = arrayLocations[safe: 0] {
print(aLocation.longitude)
print(aLocation.latitude)
// instead of this
print(aLocation[0]) // a person who reads your code won't understand this
print(aLocation[1]) // a person who reads your code won't understand this
}
正如他在信上所说的
无法将值分配给索引0,因为在以下情况下没有索引0
数组为空
您必须追加或插入项目:
arrayLocations.append([27.1750199, 78.0399665])
arrayLocations.append([27.1750199,78.0399665])
我建议您使用这个集合
,这样您的代码就不会经常崩溃
extension Collection where Indices.Iterator.Element == Index {
/// Returns the element at the specified index if it is within bounds, otherwise nil.
subscript (safe index: Index) -> Generator.Element? {
return indices.contains(index) ? self[index] : nil
}
}
通过使用它,您可以只使用if let
块
样本:
这确保了即使您尝试访问索引为0
的对象,您的代码也不会崩溃
建议:
注意:这个建议不是答案的一部分,而是改进代码的建议
看起来您正在尝试创建一个纬度和经度数组。对纬度和经度对象使用数组不是很明智。我建议您改为创建一个对象,它可以是struct
或typeAlias
例如:
struct CoordinateStruct {
var longitude: Float
var latitude: Float
}
/* or */
typealias CoordinateTypeAlias = (latitude: Float, longitude: Float)
/************************* */
// Your code would then look like this
var arrayLocations:[CoordinateStruct] = []
arrayLocations.append(CoordinateStruct(longitude: 27.12312312, latitude: 78.123123))
/* or */
var arrayLocations:[CoordinateTypeAlias] = []
arrayLocations.append((27.12312312, 78.123123))
// Which would make accessing them very easy and readable, like this.
if let aLocation = arrayLocations[safe: 0] {
print(aLocation.longitude)
print(aLocation.latitude)
// instead of this
print(aLocation[0]) // a person who reads your code won't understand this
print(aLocation[1]) // a person who reads your code won't understand this
}
无法将值分配到0位置,因为索引0为空
您可以通过以下方式完成此操作-
具有初始值-
var arrayLocations:[[Float]] = [[0.00,0.00]]
arrayLocations[0] = [27.1750199, 78.0399665]
print("\(arrayLocations[0][0]) and \(arrayLocations[0][1])")
否则,您可以使用append
或insert
作为的答案来执行此操作。由于索引0为空,您无法将值分配到0位置
您可以通过以下方式完成此操作-
具有初始值-
var arrayLocations:[[Float]] = [[0.00,0.00]]
arrayLocations[0] = [27.1750199, 78.0399665]
print("\(arrayLocations[0][0]) and \(arrayLocations[0][1])")
否则,您可以使用append
或insert
as的答案来执行此操作。非常感谢!这正是我所困惑的。所以基本上你不能给某些索引赋值,只需添加它们就行了?你只能用索引订阅来更新值。明白了!干杯哦,为什么打印时不显示整数?我使用Float来保持整数的完整性。非常感谢!这正是我所困惑的。所以基本上你不能给某些索引赋值,只需添加它们就行了?你只能用索引订阅来更新值。明白了!干杯哦,为什么打印时不显示整数?我使用Float是为了保持整数的完整性。如果你从另一个答案(可能来自或)复制代码,请不要忘记添加到原始源代码的链接@MartinR I添加了它,请看顶部我说的,正如vadian在他的答案上所说的,
这是hyperlinkedI,意思是下标(安全索引:index)
method…@MartinR哦,很抱歉,我不知道它来自哪里。我将编辑我的答案。@Aecasorg:您试图通过下标向数组追加一个值:arrayLocations[0]=…
如果数组为空,则会崩溃。这与我链接到的问答中的错误完全相同,解决方法是使用附加
或+=
,如问答答案中所述。如果搜索“不能使用下标语法将新项附加到数组末尾”然后你会发现更多的副本。如果你从另一个答案(可能来自或)复制代码,请不要忘记添加到原始源代码的链接@MartinR I添加了它,看看顶部我说的,正如vadian在他的答案上所说的,
它是hyperlinkedI,意思是下标(安全索引:index)
方法…@MartinR噢,对不起,我不知道它是从哪里来的。我将编辑我的答案。@Aecasorg:您试图通过下标向数组追加一个值:arrayLocations[0]=…
如果数组为空,则会崩溃。这与我链接到的问答中的错误完全相同,解决方案是使用附加
或+=
,如问答的答案所述。如果搜索“不能使用下标语法将新项附加到数组末尾”,则会发现更多重复项。