Bright
Bright

Reputation: 5751

Argument type '[String?]' does not conform to expected type 'AnyObject'

Code:

var contactArray = [nameField.text, addressField.text, phoneField.text]
NSKeyedArchiver.archiveRootObject(contactArray, toFile: dataFilePath!)
//Error on contactArray: Argument type '[String?]' does not conform to expected type 'AnyObject'

Since the contactArray is a non optional value, I can't force unwrap it, what should I do?

Upvotes: 3

Views: 5464

Answers (2)

Gontse Ranoto
Gontse Ranoto

Reputation: 51

AnyObject can only be used for classes Therefore :

var contactArray : NSArray = [nameField.text, addressField.text, phoneField.text];

just make your array type NSArray

Upvotes: 0

btmcnellis
btmcnellis

Reputation: 712

You are correct that contactArray is not an optional; it's an array of optionals. You need to unwrap each individual element of the array as you construct it, e.g.:

var contactArray = [nameField.text!, addressField.text!, phoneField.text!]

Also, unless you plan on modifying that array later, you should use let instead of var to make sure it can't be modified.

Upvotes: 4

Related Questions