0

I am having trouble checking a value in a mutlidimensional array.

I am creating an array within an array with 3 arrays inside.

The array is called profilerdata, the variable "key" is a string so it should just be three stings in these arrays.

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.

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:

Could not cast value of type 'Swift._SwiftDeferredNSArray' (0x104ce8040) to '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.

2
  • Can you explain why you're casting newKeyArray to AnyObject? Also, can you quote the error message more accurately and explain what line it occurs on? Commented Aug 30, 2017 at 22:31
  • Ive added those details and i tried to cast it as a String but i get an error "Cannot convert value of type '[[String]]' to type 'String' in coercion" or just leave it append(newKeyArray) which gives me the error "Argument type '[[String]]' does not conform to expected type 'AnyObject'" Commented Aug 30, 2017 at 22:39

1 Answer 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
Sign up to request clarification or add additional context in comments.

4 Comments

thank you, i was on the right track at one point but was getting a message variable has no subscript so must have been going at it wrong, thank you
Well, that's why I said you need to think about why you're casting to AnyObject. You should not be using AnyObject or Any at all. (I used Any in my example only to show you what you were doing.) Use real types.
So what should i be using instead as i couldnt seem to use string as it threw an error?
I can't tell you what you "should" do. I have no notion what you're even trying to do. Your code is completely opaque to me. You are making an Array<Array<Array<String>>>. Why anyone would do that beats me totally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.