Reputation: 8396
I've currently upgraded to the new Xcode 7, and the following code never had any errors with swift 1.2, but now its telling me that :
Cannot subscript a value of type any object
var imageArray : NSArray = []
let url = NSURL(string: (self.imageArray[indexPath.row][0] as? String)!)
I know its about [0]
but how do i rewrite it to be accepted ?
Upvotes: 0
Views: 290
Reputation: 77641
OK, so first you are using an NSArray
. You can drop that and make everything much easier.
In Swift always use strong typing where possible. Avoid Any and AnyObject when they are not needed. (They are VERY RARELY needed).
The error is happening because you're not telling the code what is actually in the imageArray
.
Also, imageArray
tells me the array is full of images. Name your variables more descriptively. imageUrlStringArray
or arrayOfImageUrlArrays
. Something more descriptive anyway.
Declare imageArray
like...
var imageArray = [[String]]()
This tells the compiler that imageArray
is a 2D array with Strings
at the second level.
Now you can create your URL easily...
guard
let urlString = imageArray[indexPath.row].first,
let url = NSURL(string: urlString)
else { // Handle the inability to create a URL }
// Do something with the url
Upvotes: 3