更新到Xcode7和iOS9后,我收到错误"对成员' Int.init'"在此行的类型转换" dueDate":Int(date.timeIntervalSince1970 * 1000),"在swift文件中。请帮帮我。
var date: NSDate! //Declared in the beginning of file
var updatedTask = [
"description": self.descriptionTextfield.text,
"title": self.titleTextfield.text,
"priority": self.priorityTextfield.text!.uppercaseString,
"type": self.typeTextfield.text!.uppercaseString,
"dueDate": Int(date.timeIntervalSince1970 * 1000),
"privacy": self.privateSwitch.on ? "PRIVATE": "PUBLIC"
]
答案 0 :(得分:3)
最小再现代码为:
var field: UITextField = UITextField()
let dict = [
"foo": Int(42),
"bar": field.text
]
此处的问题是UITextField.text
:
Xcode6.4:
var text: String! // default is nil
Xcode7.0:
public var text: String? // default is nil
它已从ImplicitlyUnwrappedOptional
更改为Optional
因此上面代码中的字典文字具有类型
[
String: Int,
String: Optional<String>
]
然后Optional<String>
无法桥接到AnyObject
。这就是编译器混淆的原因。
要解决此问题,您必须强制解包.text
属性:
var updatedTask = [
"description": self.descriptionTextfield.text!, // <- HERE
"title": self.titleTextfield.text!, // <- HERE
"priority": self.priorityTextfield.text!.uppercaseString,
"type": self.typeTextfield.text!.uppercaseString,
"dueDate": Int(date.timeIntervalSince1970 * 1000),
"privacy": self.privateSwitch.on ? "PRIVATE": "PUBLIC"
]
答案 1 :(得分:0)
需要给编译器更多关于类型的信息解决了我的问题。谢谢你的帮助。
var updatedTask : [String : Any] =
[
"description": self.descriptionTextfield.text!, // <- HERE
"title": self.titleTextfield.text!, // <- HERE
"priority": self.priorityTextfield.text!.uppercaseString,
"type": self.typeTextfield.text!.uppercaseString,
"dueDate": Int(date.timeIntervalSince1970 * 1000),
"privacy": self.privateSwitch.on ? "PRIVATE": "PUBLIC"
]