Swift语法try

init使用,函数调用

   class NamedShape {
        var numberOfSides: Int = 0
        var name: String

        init(name: String) {
            self.name = name
            print(self.name)
        }
        
        func simpleDescription() -> String {
            return "A shape with \(numberOfSides) sides."
        }
        
    }
    NamedShape(name:"liu")
    var narr = NamedShape(name:"liu").simpleDescription()
    print(narr)

try?

另一种处理错误的方式使用try?将结果转换为可选的。如果函数抛出错误,该错误会被抛弃并且结果为nil。否则的话,结果会是一个包含函数返回值的可选值。
let printerSuccess = try? send(job: 1884, toPrinter: "Mergenthaler")
let printerFailure = try? send(job: 1885, toPrinter: "Never Has Toner")

你可能感兴趣的:(Swift语法try)