What does an exclamation mark mean in the Swift language?

后端 未结 22 2165
南方客
南方客 2020-11-22 03:47

The Swift Programming Language guide has the following example:

class Person {
    let name: String
    init(name: String) { self.name = name }
    var apar         


        
相关标签:
22条回答
  • 2020-11-22 04:22

    John is an optional Person, meaning it can hold a value or be nil.

    john.apartment = number73
    

    is used if john is not an optional. Since john is never nil we can be sure it won't call apartment on a nil value. While

    john!.apartment = number73
    

    promises the compiler that john is not nil then unwraps the optional to get john's value and accesses john's apartment property. Use this if you know that john is not nil. If you call this on a nil optional, you'll get a runtime error.

    The documentation includes a nice example for using this where convertedNumber is an optional.

    if convertedNumber {
        println("\(possibleNumber) has an integer value of \(convertedNumber!)")
    } else {
        println("\(possibleNumber) could not be converted to an integer")
    }
    
    0 讨论(0)
  • 2020-11-22 04:22
    Simple the Optional variable allows nil to be stored.
    
    var str : String? = nil
    
    str = "Data"
    
    To convert Optional to the Specific DataType, We unwrap the variable using the keyword "!"
    
    func get(message : String){
       return
    }
    
    get(message : str!)  // Unwapped to pass as String
    
    0 讨论(0)
  • 2020-11-22 04:23

    Here are some examples:

    var name:String = "Hello World"
    var word:String?
    

    Where word is an optional value. means it may or may not contain a value.

    word = name 
    

    Here name has a value so we can assign it

    var cow:String = nil
    var dog:String!
    

    Where dog is forcefully unwrapped means it must contain a value

    dog = cow
    

    The application will crash because we are assign nil to unwrapped

    0 讨论(0)
  • 2020-11-22 04:23

    The ! at the end of an object says the object is an optional and to unwrap if it can otherwise returns a nil. This is often used to trap errors that would otherwise crash the program.

    0 讨论(0)
  • 2020-11-22 04:26

    In this case...

    var John: Person!

    it means, that initially John will have nil value, it will be set and once set will never be nil-led again. Therefore for convenience I can use the easier syntax for accessing an optional var because this is an "Implicitly unwrapped optional"

    0 讨论(0)
  • 2020-11-22 04:28

    The entire story begins with a feature of swift called optional vars. These are the vars which may have a value or may not have a value. In general swift doesn't allow us to use a variable which isn't initialised, as this may lead to crashes or unexpected reasons and also server a placeholder for backdoors. Thus in order to declare a variable whose value isn't initially determined we use a '?'. When such a variable is declared, to use it as a part of some expression one has to unwrap them before use, unwrapping is an operation through which value of a variable is discovered this applies to objects. Without unwrapping if you try to use them you will have compile time error. To unwrap a variable which is an optional var, exclamation mark "!" is used.

    Now there are times when you know that such optional variables will be assigned values by system for example or your own program but sometime later , for example UI outlets, in such situation instead of declaring an optional variable using a question mark "?" we use "!".

    Thus system knows that this variable which is declared with "!" is optional right now and has no value but will receive a value in later in its lifetime.

    Thus exclamation mark holds two different usages, 1. To declare a variable which will be optional and will receive value definitely later 2. To unwrap an optional variable before using it in an expression.

    Above descriptions avoids too much of technical stuff, i hope.

    0 讨论(0)
提交回复
热议问题