对成员 'Int.init' 的引用不明确
Ambiguous reference to member 'Int.init'
更新到 Xcode7 和 iOS9 后,我在 "dueDate" 行的类型转换中收到错误 "Ambiguous reference to member 'Int.init'":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"
]
最小复制代码为:
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"
]
需要向编译器提供有关解决我的问题的类型的更多信息。感谢您的帮助。
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"
]
对于未来的读者,有 2 个问题。首先,您没有告诉编译器您的 Dictionary 包含什么类型的数据 ([String: Any])。其次,正如 rintaro 指出的那样,文本 属性 在您的文本字段中是可选的。
// Tell the compiler about your dictionary
var updatedTask : [String : Any] = [
"description": self.descriptionTextfield.text!, // Force unwrap the optional text property
// rest of dictionary members
]
更新到 Xcode7 和 iOS9 后,我在 "dueDate" 行的类型转换中收到错误 "Ambiguous reference to member 'Int.init'":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"
]
最小复制代码为:
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"
]
需要向编译器提供有关解决我的问题的类型的更多信息。感谢您的帮助。
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"
]
对于未来的读者,有 2 个问题。首先,您没有告诉编译器您的 Dictionary 包含什么类型的数据 ([String: Any])。其次,正如 rintaro 指出的那样,文本 属性 在您的文本字段中是可选的。
// Tell the compiler about your dictionary
var updatedTask : [String : Any] = [
"description": self.descriptionTextfield.text!, // Force unwrap the optional text property
// rest of dictionary members
]