Почему преобразование числового типа выдает ошибку?

swift3

#1

Подскажите новичку, столкнулся с такой ситуацией, вот код из учебника:

let wholeNumber: Double = 12345.0
 
if let valueMaintained = Int(exactly: wholeNumber) {
    print("\(wholeNumber) conversion to Int maintains value of \(valueMaintained)")
}

Выводит на печать

// Prints "12345.0 conversion to Int maintains value of 12345"

Вот мой код, по идее одинаковый.

let wholeNumber: Double = 12345.0

    if let valueMaintained = Int(exactly: wholeNumber) {
        print("\(wholeNumber) conversion to Int maintains value of \(valueMaintained)")
    }

Но на выходе получаю ошибку

cannot invoke initializer for type 'Int' with an argument list of type '(exactly: Double)'

Не могу понять почему, вроде все одинаково?


#2

На это сообщение поступили жалобы от участников сообщества, поэтому оно временно скрыто.


#3

Тема Initialization - Failable Initializers

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID203


#4

На это сообщение поступили жалобы от участников сообщества, поэтому оно временно скрыто.


#5

Failable Initializers

It is sometimes useful to define a class, structure, or enumeration for which initialization can fail. This failure might be triggered by invalid initialization parameter values, the absence of a required external resource, or some other condition that prevents initialization from succeeding.

To cope with initialization conditions that can fail, define one or more failable initializers as part of a class, structure, or enumeration definition. You write a failable initializer by placing a question mark after the init keyword (init?).

Note

You cannot define a failable and a nonfailable initializer with the same parameter types and names.

A failable initializer creates an optional value of the type it initializes. You write return nil within a failable initializer to indicate a point at which initialization failure can be triggered.

Note

Strictly speaking, initializers do not return a value. Rather, their role is to ensure that self is fully and correctly initialized by the time that initialization ends. Although you write return nil to trigger an initialization failure, you do not use the return keyword to indicate initialization success.

For instance, failable initializers are implemented for numeric type conversions. To ensure conversion between numeric types maintains the value exactly, use the init(exactly:) initializer. If the type conversion cannot maintain the value, the initializer fails.

let wholeNumber: Double = 12345.0
let pi = 3.14159
 
if let valueMaintained = Int(exactly: wholeNumber) {
    print("\(wholeNumber) conversion to Int maintains value of \(valueMaintained)")
}
// Prints "12345.0 conversion to Int maintains value of 12345"
 
let valueChanged = Int(exactly: pi)
// valueChanged is of type Int?, not Int
 
if valueChanged == nil {
    print("\(pi) conversion to Int does not maintain value")
}
// Prints "3.14159 conversion to Int does not maintain value"

#6

На это сообщение поступили жалобы от участников сообщества, поэтому оно временно скрыто.


#7

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID203


#8

На это сообщение поступили жалобы от участников сообщества, поэтому оно временно скрыто.


#9

все оказалось просто - этот код работает в Xcode 9.

Спасибо за помощь, что подсказал направление.


#10

12345678901234567890


#11

Вы из 2019 к нам заглянули? Чего нового в свифт 6, сопрограммы (подпрограммы) добавили?