[英]iOS SWIFT - File Archive - Argument Type “[String?]” : does not conform to expected type'AnyObject'
I am trying to write into an Archive and getting this error . 我正在尝试写入存档并收到此错误。
My tech stack is : XCode 7.1 Beta and SWIFT .Appreciate if any of you could share the exact code to fix this issue. 我的技术堆栈是:XCode 7.1 Beta和SWIFT。如果您有任何人可以共享确切的代码来解决此问题,请相信。 Thanks in advance. 提前致谢。
Argument Type "[String?]" : does not conform to expected type'AnyObject' 参数类型“[String?]”:不符合预期类型'AnyObject'
@IBAction func saveArch(sender: AnyObject) {
var contactArray = [name.text, address.text, phone.text]
NSKeyedArchiver.archiveRootObject(contactArray,
toFile: dataFilePath!)
}
Thanks 谢谢
You should try 你应该试试
NSKeyedArchiver.archiveRootObject(NSArray(array: contactArray),
toFile: dataFilePath!)
You are sending a swift []
object that does not conform to AnyObject
since arrays and object are different things in swift. 您正在发送一个不符合AnyObject
的swift []
对象,因为数组和对象在swift中是不同的东西。
You also have a problem with optionals: one or all of your .text
is of type String? 您还有选项问题:您的.text
中的一个或全部是String类型? (therefor it might be nil). (因此它可能是零)。
If you are positive none of this field is nil, you can use 如果你是肯定的,这个领域都不是零,你可以使用
var contactArray = [name.text!, address.text!, phone.text!]
Or change the declaration. 或者更改声明。
If you are not sure, you should do something like 如果你不确定,你应该做点什么
var contactArray = [String]()
for element in [name.text, address.text, phone.text] where element != nil {
array.append(element!)
}
NSKeyedArchiver.archiveRootObject(NSArray(array: contactArray),
toFile: dataFilePath!)
This way you only add non nil
elements to the contactArray By the way, Xcode 7.1 is out. 这样,您只添加非nil
到contactArray元素顺便说一句,Xcode的7.1出来了。 No need to use the beta anymore 无需再使用测试版
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.