VesselWheel

Nil Coalescing Operator : ??(nil 병합연산자) 본문

Xcode Study

Nil Coalescing Operator : ??(nil 병합연산자)

JasonYang 2023. 10. 14. 07:40

// Nil Coalescing Operator :  ??

// nil 병합연산자

 

var message = ""

var input: String? = "Swfit5"

 

if let input{

    message = "Hello, " + input

}else{

    message = "Who are U?"

}

print("message :",message)

 

var str1 = "Hello, " + (input != nil ? input! : "Who A U?")

print(str1)

 

var str2 = "Hello, " + (input ?? "Who A U?")

print(str2)

 

/*

 Optional Chaining

           ㄴ Optional 은 연달아서 호출함

 1) Optional Chaining 의 결과는 항상 Optional 이 됨

 2) Optional Chaining 에 포함된 표현식에서

    하나라도 nil 을 return 하면 이어지는 표현식을

    평가(evaluation)하지 않고 바로 nil을 return함

 */

 

struct Contacts{

    var email: [String: String]

    var address: String

    

}

 

struct Person{

    var name: String

    var contacts: Contacts

    

    // 구조체 생성자

    init(name: String, email: String){

        self.name = name

        contacts = Contacts(email: ["home": email], address: "Shinchon")

    }

}

var p1 = Person(name:"이순신", email:"turtle@gmail.com")

 

print(p1.name)

print(p1.contacts.email)

print(p1.contacts.address)

 

print("-- type --")

// String

print(type(of: p1.name))

// Dictionary<String, String>

// [String(key): String(value)]

print(type(of: p1.contacts.email))

// String

print(type(of: p1.contacts.address))

print("-------------------")

 

var p2: Person? = Person(name:"유관순", email:"you@gmail.com")

 

 

// p2?.contacts.address

//    ㄴ optional chaining

print(p2?.name)

print(p2?.contacts.email)

print(p2?.contacts.address)

 

p2 = nil

print(p2?.name)

print(p2?.contacts.email)

print(p2?.contacts.address)

 

// Person

print(type(of: p1))

// Optional<Person>

print(type(of: p2))

 

// expression : 표현식(값, 변수, 수식)

// p2?.contacts.address

//    ㄴ optional chaining

// 1) Optional Chaining 의 결과는 항상 Optional 이 됨

// Optional<String>

print(type(of: p2?.name))

// Optional<Dictionary<String, String>>

// [String(key): String(value)]

print(type(of: p2?.contacts.email))

// Optional<String>

print(type(of: p2?.contacts.address))

 

struct Contacts2{

    var email: [String: String]?

    var address: String?

    

    func displayAddress(){

        print(address ?? "no address")

    }

}

 

struct Person2{

    var name: String

    var contacts: Contacts2?

    

    // 생성자

    init(name: String, email: String){

        self.name = name

        contacts = Contacts2(email: ["home": email], address: "Shinchon")

    }

    

    func getContacts2() -> Contacts2?{

        return contacts

    }

}

 

var p3 = Person2(name: "이율곡", email:"yool@gmail.com")

 

// Optional 표현식에 통해서 속성(멤버변수)이나

// 메소드를 호출하는 경우에는 ?(!) 를 붙여줌

let p3_address = p3.contacts?.address

 

// p3_address : Optional("Shinchon")

print("p3_address :",p3_address)

 

// p3_address : Shinchon

print("p3_address :",p3_address!)

 

// p3_address : Optional<String>

print("p3_address :",type(of: p3_address))

 

// Optional("Shinchon")

print(p3.getContacts2()?.address)

 

// Shinchon

p3.getContacts2()?.displayAddress()

 

/*

  [1, 2, 3, 4, 5]

  (1, 2, 3, 4, 5)

  ["name":"이순신", "age":46, "job":"장군"]

 */

let p3_email = p3.getContacts2()?.email?["home"]

// p3_email : Optional("yool@gmail.com")

print("p3_email :",p3_email)

// p3_email : yool@gmail.com

print("p3_email :",p3_email!)

 

 

print(p3.contacts?.address)

 

p3.contacts?.address = "Mapo"

print(p3.contacts?.address)

'Xcode Study' 카테고리의 다른 글

Function (함수)  (1) 2023.10.14
Optional Pattern  (0) 2023.10.14
Optional(?) Binding  (0) 2023.05.03
Optional type 선언(?), 해제(!)  (0) 2023.05.03
While loop 계속(반복조건)  (0) 2023.05.02