Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

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?

like image 476
Bright Avatar asked Aug 27 '26 21:08

Bright


1 Answers

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.

like image 51
btmcnellis Avatar answered Aug 30 '26 13:08

btmcnellis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!