Cannot assign a value of type '[AnyObject]' to a value of type '[String]' Swift 2

Hi,

I try to updating my project from Swift to Swift 2 but I get an error with the following code:


let PhotoController = UIImagePickerController()
  PhotoController.delegate = self 
  PhotoController.sourceType = UIImagePickerControllerSourceType.Camera
  let mediaTypes:[AnyObject] = [kUTTypeImage]
  PhotoController.mediaTypes = mediaTypes 
  PhotoController.allowsEditing = false
 self.presentViewController(PhotoController, animated: true, completion: nil)


On the following line:


PhotoController.mediaTypes = mediaTypes


the compiler indicate this error:

cannot assign a value of type '[AnyObject]' to a value of type '[String]'


Can you help me please ? Thanks a lot !

Accepted Answer

As in the reference mediaTypes, UIImagePickerController's property `mediaType` is of type [String] .

Why do you declare your local variable mediaType as [AnyObject] ?

        let mediaTypes: [String] = [kUTTypeImage as String]


EDIT: Replaced wrong link.

It works ! thanks a lot

In Swift 2, API signatures containing collection types (NSArray, NSDictionary, NSSet) have changed to more specific types utilizing Lightweight Generics in Objective-C.

If you find some APIs do not work, in which parameters formerly contained [AnyObject], [NSObject: AnyObject] or Set<NSObject>, you'd better check the latest API reference. Good luck and may your app be great.

Cannot assign a value of type '[AnyObject]' to a value of type '[String]' Swift 2
 
 
Q