swift 1: the basics

Attention :

    This place is mainly for collecting what i has learnt about swift, the content is only on my thought, and maybe something are not exactly correct.


    1 Swift introduces advanced types not found in OC, such as tuples. Tuples enable you to create and pass around groupings of values. You can use a tuple to return multiple values from a function as a single compound value.

    2 Swift introduces optional types to handle the absence of a value, it says either "there is a value and it equals x " or "there isn't a value at all".  

      Optionals are similar to using nil with pointers in OC, but they work for any type, not just classes.Optionals are safer and more expressive than nil pointers in OC and at the heart of many of swift's most powerful feature (not correctly know what this mean)

    3 swift is a type safe language. Optionals are an an example.

    4 can declare multiple constants or multiple variables on a single line, separated by commas:

     var x = 0.0, y = 0.0, z = 0.0

    5 type annotation :                                var welcomeMessage: String                        var blue, red, green: UIColor

    6 once you've declared a constant or variable of a certain type, you cann't redeclare it again with the same name, or change it to store values of a different type. Nor can you change a constant into a variable or a variable into a constant

    7 string interpolation :  include the name of a constant or variable as a placeholder in a longer string, and to prompt Swift to replace it with the current value of that constant or variable   l.g.:println("The current value of string if \(xxx)")

    8 Commnets: unlike multiline comments in C, multiline comments in Swift can be nested inside other multiline comments 

    9 Swift always choose Double(rather than Float )when inferring the type of floating-point numbers

    10 tuples are useful for temporary groups of related values. They are not suited to the creation of complex data structures. If your data structure is likely to persist beyong a temporary scope, model it as a class or structure, rather than as a tuple

    11 Optionals: 

    nil in OC meanins "the absence of a valid object", it only works for objects--it doesn't work for structures, basic C types, or enumeration values, for these types, OC methods typically return a special value(such as NSNotFound); Optionals means "there is a value and it equals x or there isn't a value at all", it is for any type at all, without the need for special constants.

    当某个variable 可能为空时,使用optionals,like: 

var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值