Ambiguous reference to the Int.init member

After upgrading to Xcode7 and iOS9, I get the error message "Ambiguous reference to a member" Int.init "in the type conversion in this line dueDate: Int (date.timeIntervalSince1970 * 1000) in the fast file. Help me please.

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"
    ]
+4
source share
3 answers

You need to give the compiler more information about the type that resolved my problems. Thank you for your help.

 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"
 ]
0
source

The minimum replay code will be:

var field: UITextField = UITextField()
let dict = [
    "foo": Int(42),
    "bar": field.text
]

The problem is the type UITextField.text:

Xcode6.4:

    var text: String! // default is nil

Xcode7.0:

    public var text: String? // default is nil

He changed from ImplicitlyUnwrappedOptionaltoOptional

,

[
   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"
]
+3

There are 2 questions for future readers. First, you did not tell the compiler which data type your dictionary contains ([String: Any]). Secondly, as rintaro pointed out, the text property is optional in your text box.

// Tell the compiler about your dictionary
var updatedTask : [String : Any] = [
    "description": self.descriptionTextfield.text!, // Force unwrap the optional text property
    // rest of dictionary members
]
0
source

Source: https://habr.com/ru/post/1608449/


All Articles