也是很久前的draft 囧 都是一些老生常談講到爛到不能再爛的東西,但是每次都會忘掉,就單純做個紀錄,因此此篇非常雜亂!
===================================
Obj-C introduction
dynamic language - dynamic type (id) and binding, a little more effort than C++, but has more flexible
http://rintarou.dyndns.org/2010/12/09/objective-c-淺談/
GC - suspend on iOS & OS X 10.8 (variety of algorithm)
property - access method(auto synthesize), encpulation, dot syntax, attribute configuration, KVO
hint:internal access - directly get and using setter to set instance variables
cocoa vs. cocoa touch
http://www.cnblogs.com/kkun/archive/2012/04/25/2470166.html
protocol
即類似java interface,主要是由於OC本身沒有多重繼承機制,所以protocol
可讓Class之間互相溝通與使用對方的method,protocol最頻繁被使用於
delegate 與 data source DP
Delegation
如我的blog所說,是一種DP讓Class之間可以互動並可委託其他Class執行
自己不希望執行的功能。其class互動與溝通有其他很多方法可以替代,但
是此方法可以充分發揮MVC精神,例如View不應該處理的MC,可透過此
DP完成這種概念。
由於protocol具@optional與@require(default)兩種method的設定,因
此在委託方中implement方法的使用時,要記得當使用@optional的方法時
,要多利用introspection機制確定方法有沒有被conform。
在使用introspection機制時也要再另外注意一件事,就是當method被頻繁
的call or callback時,應該想辦法將introspection機制與頻繁被執行的部分
分隔開來,避免浪費多餘的效能。(performance optimal)
優化機制作法:
override delegate setter,在當中先進行introspection機制並將結果
cache,未來直接使用cache的結果來判斷是否可以執行method即可。
category
在其他language中會發生繼承下來的方法不夠用或是沒找到自己想用的,這時
多數作法是在subclass增加方法,或是被經常用到的方法就新增到superclass裏
,這種情況下若是不複雜的且是自己implement的superclass可能不會造成太大
困擾,但若是接手他人的code或是superclass已經發展到很複雜並且太多subclass
在使用,若不想動到原本的code並且不會讓每個subclass都要繼承新增的method
,category所實現的額外加入method就非常的好用。
若沒有取名的category稱為類別延伸,通常用於隱藏不想public的instance variable
。需注意累目的方法名稱要加上prefix,避免override原始method
其存在主要是實現clean code 想法,透過category可對method分門別類,而不會
導致某個class既肥大又雜散
Observer Desgin Pattern:
KVC - KVO
http://rintarou.dyndns.org/2011/07/22/objective-c-programming-筆記-2/
KVC - https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/Overview.html#//apple_ref/doc/uid/20001838-SW1
透過String格式的identifier間接access object properties/instance variable,KVC與@property一樣,會自動使用accessor。
若是instance variable,則會自動建立accessor去存取variable,但因為KVC的判斷方法是 找外內部的accessor ->找變數 ->找不到
,因此一般都會為instance variable套上property,降低效率成本。
符合KVC格式 -
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/Compliant.html#//apple_ref/doc/uid/20002172-BAJEAIEE
Attribut, to-one and Many format
無論Attribute或to-one,找不到key會引用valueForUndefinedKey:回傳一個exception,可自行override這個函數。
attribute :
Simple value,譬如String, bool, int, float, NSNumber等,key path可直接指定variable name,當attribute非object時(scalar type or struct),需implement setNilValueForKey:。
若是scalar type時,setValue需使用NSNumber wrap,可使用literal syntax,另外要注意的是當傳入數字字串時是可以使用的,會自動call NSString的method幫忙轉成NSNumber,但須注意應避免此種狀況,減少效率成本。
若是struct則一定需要implement setter和getter,且setValue時需使用NSValue wrap C struct再傳入,傳入的值會自動unwrap,且在obsevser被call時的change parameter一樣要使用NSValue的getValue去unwrap。
(scalar type or struct) - https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/DataTypes.html#//apple_ref/doc/uid/20002171-BAJEAIEE
to-one :
Object。使用方法 ex: [Object setValue:@“” forKeyPath:@“firstName”];to-many : (NSArray, NSSet, NSDictionary)
1. collection不能使用nil當值,所以value不可直接傳nil,但是可以使用NSNull,會建立一個nil的instance放入。
2. 回傳的非單一值,是同identifier的allvalue
3. 分為indexed (Array )and unorder(set)
KVC implement detail:
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/SearchImplementation.html#//apple_ref/doc/uid/20000955-CJBBBFFA
原則:
1 .先找是否有@property auto-synthesize的 method(可以被override)或是否有自己根據rule而implement method
2. 若設定可以直接取值,則直接找是否有定義值並取值
3. 都沒有,auto invokes 找不到key的method
沒有留言:
張貼留言