swift 和oc的部分小区别

1swift 有元组

let aContant = (404, “NOT FOUND”)

2swifttype safety ,声明变量可以不要写类型

3swift里面有选择类型

4The concept of optionals doesn’t exist in C or OC. The nearest thing in Objective-C is the ability to return nil from a method that would otherwise return an object, with nil meaning “the absence of a valid object.” However, this only works for objects—it doesn’t work for structures, basic C types, or enumeration values. For these types, Objective-C methods typically return a special value (such as NSNotFound) to indicate the absence of a value. This approach assumes that the method’s caller knows there is a special value to test against and remembers to check for it. Swift’s optionals let you indicate the absence of a value for any type at all, without the need for special constants.

选择类型在c或者oc里面不存在,在oc里面最接近的东西是返回一个nil,这个方法一般情况下是返回一个对象的,nil就意味着一个有效对象的缺失。但是这个情形只对对象有用,它对结构,基本C,或者枚举值没用,对于这些类型,oc方法一般返回一个特殊的值来显示一个值的缺失,例如NSNotFound,这种设置方法假定方法的叫唤者知道有一个特殊的值要测定,记得要核对一下,swift optinal 让你显示一个值的缺失,任何类型,而不需要特殊的常量。

5NOTE

Swift’s nil is not the same as nil in Objective-C. In Objective-C,nil is a pointer to a nonexistent object. In Swift, nil is not a pointer—it is the absence of a value of a certain type. Optionals of any type can be set to nil, not just object types.

swiftniloc里面的nil不相同.in ocnil是一个指向不存在对象,但是在swift中,nil不是指针,它是很多类型的值的缺失的情况,任何类型的optional都可以设置为nil

5you can include as many optional bindings and Boolean conditions in a single if statement as you need to, separated by commas

如果你需要,你能包含很多的选择性bingding bool值情形,通过逗号分开

if let firstNumber = Int(“4”),let secondNumber = Int(“42”,first Nuber < secondNumber && secondNumber < 100) {

   print(“dddddd”)

}

6Unlike the assignment operator in C and Objective-C ,the assignment operator in swift does not itself return a value.

不像c语言或者oc语言,在swift中的赋值运算符不返回一个值

7Swfit also provides two identify operators(=== and !==),which you use to test whether two object references both refer to the same object instance.

===和!==测试两个对象指向相同的实例

8if 语句条件判断不需要加括号

9String变量能修改,常量不能修改,这个与oc里面的字符串不一样

10oc里面string有个length,swift里面有个characters这两个值并不一定相同

11shoppingList[4…6] = [“Bananas”,”Apples”]

用来替换的只有两个元素,被替换却有三个元素,即使这样也可以

12In contrast with switch statements in C and Objective-C,switch statements in Swift do not fall through the bottom of each case and into the next one by default. Instead, the entires witch statement finishes its execution as soon as the first matching switch case is completed, without requiring an explicit break statement. This makes the switch statement safer and easier to use than the one in C and avoids executing more than oneswitch case by mistake.

coc不同的是,在swift里面的switch 声明,不会穿过第一个case 而进入下一个case ,整个swift语句只要第一个case完成了,那么整个switch就完成了它的执行,不需要一个明显的关键字break.这意味着switch语句c里面的switch语句更加安全,它能避免错误地执行超过一次case.

swift允许多个switchcase包含多个相同的值。因为反正只执行一个

13、字符串不要用@

14switch语句coc需要break才能中断case,但是swift不要,同样,swift不能自动划过去case,必须加上fallthrough 才行。

15、我们能在函数里面修改函数外面的值。

var someInt = 3

var anotherInt = 107

swapTwoInts(&someInt, &anotherInt)

print (“someInt is now \(someInt), and anotherInt is now \(anotherInt)”)

//Prints “someInt is now 107, and antherInt is now 3”

16、方法选择器oc@swift#

17The fact that structures and enumerations can define methods in Swift is a major difference from C and Objective-C

结构枚举里面能定义方法是ocswift的主要的不同。

18Property and variable observers are a fairly unique feature that most other languages don't have. It is especially unique that Swift has variable observers.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值