Search code examples
uiimageswiftdowncast

Downcast from AnyObject to UIImage[] - Swift


I'm trying to convert the results of a valueForKeyPath into an array of UIImages. When trying to do it I get met with this error

Undefined symbols for architecture i386: "TToFC9TableView19TableViewControllers6photosGSqGSaCSo7UIImage", referenced from: __TFC9TableView29JustPostedTableViewController11fetchPhotosfS0_FT_T_ in JustPostedTableViewController.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here's my attempt at the code in swift: The propertyListResults is an NSDictionary, and the parameter in valueForKeyPath is a String

var photos : AnyObject = (propertyListResults as AnyObject).valueForKeyPath(flickrFetcher.FLICKR_RESULTS_PHOTOS)

self.photos = photos as? UIImage[]

And the working Objective-C version of the code is

NSArray *photos = [propertyListResults valueForKeyPath:FLICKR_RESULTS_PHOTOS];
self.photos = photos

Solution

  • If you run swift-demangle on "__TToFC9TableView19TableViewControllers6photosGSqGSaCSo7UIImage__" you get:

    @objc TableView.TableViewController.photos.setter : (ObjectiveC.UIImage[])?
    

    That means that the linker is not finding a setter for photos on TableViewController. It seems that you are using a TableViewController instead of your own subclass that has a photos property.

    Also, photos as? UIImage[] returns an Optional because it is possible for it to fail. You should do the following:

    if let images : [UIImage] = photos as? [UIImage] {
        self.photos = images
    }
    

    This checks if photos can be converted to [UIImage]. If it can, it assigns it to self.photos

    Edit: Updated UIImage array syntax to the new array syntax in the beta 3