Swift Initialize Struct with optional stored properties

前端 未结 3 1148
终归单人心
终归单人心 2020-12-13 12:32

I\'m a Swift newbie and I\'m trying to get my head around using Structs with optional properties. I\'ve done quite a bit of searching and got something that works but it fee

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 13:14

    hope this could help you

     struct Business {
        let name : String
        var web : String?
        var address: String?
    
    
    // you also need use question mark in init progress
    init(name: String, web: String?, address: String?) {
        self.name = name
        self.web = web
        self.address = address
        }
    }
    

    Once you create a object you could use nil on your optional value For example :

    var newBusiness = Business(name: "AWS", web: nil, address: nil)   
    

提交回复
热议问题