简体   繁体   English

参数类型'[String?]'不符合预期类型'AnyObject'

[英]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.

相关问题 参数类型“ AnyObject”不符合预期的类型NSCopying - Argument Type 'AnyObject' does not conform to expected type NSCopying iOS SWIFT - 文件存档 - 参数类型“[String?]”:不符合预期类型&#39;AnyObject&#39; - iOS SWIFT - File Archive - Argument Type “[String?]” : does not conform to expected type'AnyObject' 类型[String:String]不符合协议“ AnyObject” - type [String: String] does not conform to protocol 'AnyObject' 字符串不符合NSarray Swift中的AnyObject类型 - String does not conform to type AnyObject in NSarray Swift 参数类型“字符串?” 不符合预期的类型“StringProtocol” - Argument type 'String?' does not conform to expected type 'StringProtocol' 参数类型与预期类型不符 - Argument type does not conform to expected type 参数类型不符合预期的类型 MKAnnotation - Argument type does not conform to expected type MKAnnotation 输入anyObject? 不符合“ StringLiteralConvertible” - Type anyObject? does not conform to 'StringLiteralConvertible' 类型“ AnyObject.Protocol”的值与预期的字典值类型“ AnyObject”不符 - Value of type “AnyObject.Protocol” does not conform to expected dictionary value type 'AnyObject' 无法转换类型[[String:AnyObject] ?. Type”(也称为“可选” <Dictionary<String, AnyObject> &gt; .Type)到期望的参数类型 - Cannot convert value of type '[String : AnyObject]?.Type' (aka 'Optional<Dictionary<String, AnyObject>>.Type) to expected argument type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM