[英]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? 由于contactArray
是一个非可选值,我无法强行打开它,我该怎么办?
You are correct that contactArray
is not an optional; 你是正确的contactArray
不是一个可选的; it's an array of optionals. 这是一系列的选项。 You need to unwrap each individual element of the array as you construct it, eg: 您需要在构造数组时解开数组的每个元素,例如:
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. 此外,除非您计划稍后修改该数组,否则应使用let
而不是var
来确保它不能被修改。
AnyObject
can only be used for classes Therefore : AnyObject
只能用于类因此:
var contactArray : NSArray = [nameField.text, addressField.text, phoneField.text];
just make your array type NSArray
只需使您的数组类型为NSArray
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.