Cannot Subscript a Value of [Anyobject]? with an Index of Type Int

Cannot subscript a value of [AnyObject]? with an index of type Int

The problem isn't the cast, but the fact that self.objects seems to be an optional array: [AnyObject]?. Therefore, if you want to access one of its values via a subscript, you have to unwrap the array first:

var user2: PFUser
if let userObject = self.objects?[indexPath.row] {
user2 = userObject as! PFUser
} else {
// Handle the case of `self.objects` being `nil`.
}

The expression self.objects?[indexPath.row] uses optional chaining to first unwrap self.objects, and then call its subscript.


As of Swift 2, you could also use the guard statement:

var user2: PFUser
guard let userObject = self.objects?[indexPath.row] else {
// Handle the case of `self.objects` being `nil` and exit the current scope.
}
user2 = userObject as! PFUser

Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'UIImagePickerController.Infokey'

You’re using an older version of Swift, to get the image URL:

let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL

I suggest that you upgrade to Swift 5.0 in the future

NSDictionary in Swift:Cannot subscript a value of type 'AnyObject?' with a index of type 'Int'

When you subscript a dictionary, as in jsonResult["subjects"], you get an Optional. You need to unwrap the Optional. Moreover, because this dictionary is arriving from JSON, Swift doesn't know what sort of thing that Optional contains: it is typed as AnyObject - that is why Swift describes the Optional as an AnyObject?. So you also tell Swift what type of object this really is - it's an array of dictionaries, and you need to tell Swift that, or you won't be able to subscript it with [0].

You can do both those things in a single move, like this:

if let array = jsonResult["subjects"] as? [[NSObject:AnyObject]] {
let result = array[0]
// ...
}

If you are very, very sure of your ground, you can force the unwrapping and the casting, and reduce that to a single line, like this:

let result = (jsonResult["subjects"] as! [[NSObject:AnyObject]])[0]

But I can't recommend that. There are too many ways it can go wrong.

Error: Cannot subscript a value of type 'X' with ...'

Note that besides the subscript parameter issue already mentioned in comments by @Hamish there are a few other issues in your code: ArraySlice also conforms to RandomAccessCollection so just checking the array count doesn't guarantee it is a safe index. You should add a guard statement to check if the indices property contains the Index. You should also change your subscript parameter to Index instead of Int:

class CustomClass {
let value: Int
init(value: Int) {
self.value = value
}
}

extension Collection {
subscript(safe index: Index) -> Element? {
guard indices.contains(index) else {
return nil
}
return self[index]
// or simply
// return indices.contains(index) ? self[index] : nil
}
}

Playground testing:

let steps = [CustomClass(value: 0),CustomClass(value: 1),CustomClass(value: 2),CustomClass(value: 3),CustomClass(value: 4),CustomClass(value: 5),CustomClass(value: 6)]

if let step6 = steps[safe: 6] {
print(step6.value) // 6
}

let stepsSlice = steps[0...4]
let step6 = stepsSlice[safe: 6]
print(step6?.value) // nil

Cannot subscript a value of type '[AnyObject]' with an index of type 'AnyObject'

Don't declare your array as [AnyObject] because you clearly want to store a concrete type - Dictionary. Use instead

var rows: [[String : AnyObject]] = [[:]]

let firstLine: [String : AnyObject] = ["time": time, "Playing": false]
rows.append(firstLine)

Cannot subscript a value of type '[AnyObject]?' with an index of type 'UInt32'

the fact that results seems to be an optional array: [AnyObject]?.
Therefore, if you want to access one of its values via a subscript, you have to unwrap the array first:

if let userObject: AnyObject = results?[randomNumberCast]

Cannot subscript a value of type 'JSON' with an index of type '(key: String)'

Try to remove key: and index: from your code, i think will resolve.

ex: self[sub as! String]

Cannot subscript a value of type '[String]' with an index of type 'Int'

As of Swift 2, you can't simply loop over a String to enumerate the characters anymore, now you have to call the characters method on the String itself:

for letter in "hello".characters {
print(letter)
}

So for you, given that objectAttributeValues[countOfRun] seems to return a String, that would be:

for character in objectAttributeValues[countOfRun].characters {
...
}

This is because String does not conform to SequenceType anymore in Swift 2.



Related Topics



Leave a reply



Submit