Lesson 1 Objective-C vs. Swift

Learn the distinguishing language features of OC

OC vs. Swift

They differ substantially in their handling of nil, their approach to mutability and object typing.
1. OC和Swift共享iOS Frameworks和Design Patterns,但是在OC中,所有的對象都可以為nil,而在Swift中,只有Optioal類型的對象才可以為nil。
2. In swift, constants are constant, variables vary, and by default we are encouraged to favor constants. In OC mutability is by certain classes and property attributes; by default all objects are mutable.
OC classes with limited mutability include all Foundation collections. For example, NSStrings are not mutable; to make a change to a string one must either make a new string or use NSMutableString. The same goes for NSArray and NSMutableArray, NSDictionary and NSMutableDictionary, NSSet and NSMutableSet.
3. Swift is statically typed. The type of every entities known at compile time.
OC uses a combination of static and dynamic typing. The types of the majority of objects are declared at compiled time. In fact, it's required that every of object have a type.
How objects are bound to the methods the invoke?
In swift, method binding or method resolution happens at compile time.
In OC, methods in the objects invoking them are not bound until runtime.

What happens when you invoke a method on a nil pointer in Objective-C?
Sample Response
Nothing! It's ok to send a message to nil in Objective-C. There are no nil pointer exceptions. If the method invoked returns a number or a pointer, the value returned will be 0.

Does Objective-C have multiple inheritance? How does inheritance affect iOS architecture?
Sample Response
Objective-C has single inheritance; classes can only inherit from one other class. Both Swift and Objective-C achieve something akin to multiple inheritance through the widespread use of protocols. For example, view controllers commonly conform to multiple protocols like the TableViewDelegate and DataSource protocols as well as the TextFieldDelegate protocol.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容