Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift 和oc的部分小区别大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

1、swift有元组 let aContant = (404, “NOT FOUND”) 2、swift有type safety ,声明变量可以不要写类型 3、swift里面有选择类型 4、The concept of optionals doesn’t exist in C or Objective-C. The nearest thing in Objective-C is the abili

1swift元组

let aContant = (404,“NOT FOUND”)

2、swift有type safety,声明变量可以不要写类型

3、swift里面有选择类型

4、The concept of optionals doesn’t exist in C or Objective-C. The nearest thing in Objective-C is the ability to returnnil from a method that would otherwise return an object,withnil 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’snil 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 ofany type can be set to nil,not just object types.

swift的nil跟oc里面的nil不相同.in oc,nil是一个指向不存在对象,但是在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”,firstNuber < secondnumber && secondnumber < 100) {

print(“dddddd”)

}

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

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

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

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

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

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

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

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

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

12、

In contrast withswitch 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 entireswitch statement finishes its execution as soon as the first matchingswitch case is completed,without requiring an explicitbreakstatement. This makes the switch statement safer and easier to use than the one in C and avoids execuTing more than oneswitch case by mistake.

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

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

13、字符串不要用@

14、switch语句c和oc需要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”

大佬总结

以上是大佬教程为你收集整理的swift 和oc的部分小区别全部内容,希望文章能够帮你解决swift 和oc的部分小区别所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。