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 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 让你显示一个值的缺失,任何类型,而不需要特殊的常量。
5、NOTE
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
5、you 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的部分小区别所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)