Reputation: 591
I am getting the following error AnyObject' is not convertible to 'String'. I am getting this error on the line if (answerArray != NSNull() && answerArray != nil)
I try casting it as String, but it didn't fix the problem. I posted the whole code below.
let answerArray : AnyObject = jsonparser.objectWithString(answer)
if (answerArray != NSNull() && answerArray != nil) {
}
Upvotes: 0
Views: 1970
Reputation: 4336
My guess would be the error is with answerArray != nil
- Swift is silly in that the error it returns here is "not convertible to String". More helpful would be answerArray
is not Optional.
And that's your problem - you have let answerArray : AnyObject...
- and AnyObject
is not Optional
. In Swift, 'answerArray' is never 'nil', because only Optional
types can be nil
.
Without looking at what jsonparser.objectWithString
does, it's hard for me to correct your code, but in essence, the compiler is telling you that you are checking a non-optional type for nil - which is incorrect.
Upvotes: 0
Reputation: 72750
My understanding is that jsonparser.objectWithString(answer)
is supposed to return an array, basing on the name of the variable it is assigned to.
If it's an array of heterogeneous types, you can attempt a cast to NSArray
:
if let answerArray = jsonparser.objectWithString(answer) as? NSArray {
...
}
If the array is supposed to contain objects of the same type (let's say Int
) instead, then you can try a cast to a swift array:
if let answerArray = jsonparser.objectWithString(answer) as? [Int] {
...
}
Note that a non-optional variable can never be nil
- and you have declared answerArray
as a non-optional.
If you want to check for NSNull
, I suggest reading this question and related answer.
Upvotes: 2