3

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?

2 Answers 2

4

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.

0

AnyObject can only be used for classes Therefore :

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

just make your array type NSArray

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.