What does an exclamation mark mean in the Swift language?

后端 未结 22 2163
南方客
南方客 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:14

    In objective C variables with no value were equal to 'nil'(it was also possible to use 'nil' values same as 0 and false), hence it was possible to use variables in conditional statements (Variables having values are same as 'TRUE' and those with no values were equal to 'FALSE').

    Swift provides type safety by providing 'optional value'. i.e. It prevents errors formed from assigning variables of different types.

    So in Swift, only booleans can be provided on conditional statements.

    var hw = "Hello World"
    

    Here, even-though 'hw' is a string, it can't be used in an if statement like in objective C.

    //This is an error
    
    if hw
    
     {..}
    

    For that it needs to be created as,

    var nhw : String? = "Hello World"
    
    //This is correct
    
    if nhw
    
     {..}
    
    0 讨论(0)
  • 2020-11-22 04:17

    john is an optional var and it can contain a nil value. To ensure that the value isn't nil use a ! at the end of the var name.

    From documentation

    “Once you’re sure that the optional does contain a value, you can access its underlying value by adding an exclamation mark (!) to the end of the optional’s name. The exclamation mark effectively says, “I know that this optional definitely has a value; please use it.”

    Another way to check non nil value is (optional unwrapping)

        if let j = json {
            // do something with j
        }
    
    0 讨论(0)
  • 2020-11-22 04:18

    TL;DR

    What does an exclamation mark mean in the Swift language?

    The exclamation mark effectively says, “I know that this optional definitely has a value; please use it.” This is known as forced unwrapping of the optional’s value:

    Example

    let possibleString: String? = "An optional string."
    print(possibleString!) // requires an exclamation mark to access its value
    // prints "An optional string."
    
    let assumedString: String! = "An implicitly unwrapped optional string."
    print(assumedString)  // no exclamation mark is needed to access its value
    // prints "An implicitly unwrapped optional string."
    

    Source: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_399

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

    If john were an optional var (declared thusly)

    var john: Person?
    

    then it would be possible for john to have no value (in ObjC parlance, nil value)

    The exclamation point basically tells the compiler "I know this has a value, you don't need to test for it". If you didn't want to use it, you could conditionally test for it:

    if let otherPerson = john {
        otherPerson.apartment = number73
    }
    

    The interior of this will only evaluate if john has a value.

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

    IN SIMPLE WORDS

    USING Exclamation mark indicates that variable must consists non nil value (it never be nil)

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

    An Optional variable may contain a value or may be not

    case 1: var myVar:String? = "Something"

    case 2: var myVar:String? = nil

    now if you ask myVar!, you are telling compiler to return a value in case 1 it will return "Something"

    in case 2 it will crash.

    Meaning ! mark will force compiler to return a value, even if its not there. thats why the name Force Unwrapping.

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