I am having trouble checking a value in a mutlidimensional array.
我在检查多维数组中的值时遇到问题。
I am creating an array within an array with 3 arrays inside.
我在一个内部有3个数组的数组中创建一个数组。
The array is called profilerdata, the variable "key" is a string so it should just be three stings in these arrays.
该数组称为profilerdata,变量“key”是一个字符串,因此它应该只是这些数组中的三个字符串。
The array in my log as a SwiftDeferredNSArray which I think is the first problem as I have read this is a non mutable array.
我的日志中的数组作为SwiftDeferredNSArray,我认为是第一个问题,因为我已经读过这个是一个非可变数组。
var newKeyArray = [[key],[String](),[""]]
for thing in dataArray as! [[String: AnyObject]] {
newKeyArray[1].append("0")
}
print(newKeyArray)
profilerData.append(newKeyArray as AnyObject)
I then try and read the value in the array like so:
然后我尝试读取数组中的值,如下所示:
var n = 1
while n <= elementArray.count {
if profilerData[n][0] as! String == headertitle {
print("it matches")
} else {
print("it does not match")
}
n += 1
}
The variable "headertitle" is a string too but when this runs I get an error:
变量“headertitle”也是一个字符串,但是当它运行时我得到一个错误:
Could not cast value of type 'Swift._SwiftDeferredNSArray' (0x104ce8040) to 'NSString' (0x101f97c60).
无法将'Swift._SwiftDeferredNSArray'(0x104ce8040)类型的值转换为'NSString'(0x101f97c60)。
This error is occuring on this line
此错误发生在此行上
if profilerData[n][0] as! String == headertitle {
Any help much appreciated,
任何帮助非常感谢,
p.s. Please explain quite simply as I'm new to Swift.
附:请简单解释一下,因为我是Swift的新手。
1 个解决方案
#1
1
Your arrays are one level deeper than you seem to think they are. To see this, try this code, which simplifies what you're doing:
你的数组比你认为的更深一级。要查看此内容,请尝试使用此代码,以简化您正在执行的操作:
let key = "key"
let newKeyArray = [[key],[String](),[""]]
var profilerData = [Any]()
profilerData.append(newKeyArray)
let oneLevel = profilerData[0] as! Array<Array<String>>
let twoLevel = oneLevel[0] // Array<String>
let threeLevel = twoLevel[0] // String
#1
1
Your arrays are one level deeper than you seem to think they are. To see this, try this code, which simplifies what you're doing:
你的数组比你认为的更深一级。要查看此内容,请尝试使用此代码,以简化您正在执行的操作:
let key = "key"
let newKeyArray = [[key],[String](),[""]]
var profilerData = [Any]()
profilerData.append(newKeyArray)
let oneLevel = profilerData[0] as! Array<Array<String>>
let twoLevel = oneLevel[0] // Array<String>
let threeLevel = twoLevel[0] // String