lecture 4 : More Objective-C
Creating Objects
這件事困擾我一點時間了,ObjC沒有一個Constructor的概念
而在Create Objects這件事上既有用過自己寫的-init,還return instancetype,大概這個,也有用過一些Class Method,就是明確知道是+id類型的,Slides總結了
- alloc init來創(chuàng)建
- Class Method來創(chuàng)建
- 也有init 和 Class Method共存的狀況
老爺爺推薦alloc/init
- 利用objects的方法創(chuàng)建新的objects
- 這種則不是在創(chuàng)建object,雖然傳遞出object
除非NSArray不存在才會創(chuàng)建出NSArray,但是也是nil吧
nil
nil很好用,只需注意struct狀況
id
id是一個很特殊很特殊的pointer, It means “pointer to an object of unknown/unspecified” type.
所以其實之前寫的
NSString *a = [NS....];能被完全寫成
id a = [NS....];id很有用,但是有危險。
因為給的信息很通用,則complier難以察覺出語法上的問題。
但是運行的時候可能就崩潰!
Really all object pointers (e.g. NSString *) are treated like id at runtime.
But at compile time, if you type something NSString * instead of id, the compiler can help you.
Introspection
如果出現(xiàn)id,可以使用一下方法來保護自己:
isKindOfClass (inheritance included) isMemberOfClass (no inheritance) respondsToSelector Class testing methods take a Class You get a Class by sending the class method class to a class (not the instance method class). 這里可以這樣做,感覺就是把NSString class生成為一個class,然后再來測試,因為classes也是objects,老爺爺怕大家迷茫,讓大家記住這個用法就好了if ([obj isKindOfClass:[NSString class]]) {  NSString *s = [(NSString *)obj stringByAppendingString:@”xyzzy”]; }以下
這里的第一個shoot是沒有參數(shù)的
第二個shootAt有一個參數(shù),對于obj使用了target
Method有多個參數(shù)需要寫成addCard:AtTop:類似
if ([obj respondsToSelector:@selector(shoot)]) {[obj shoot]; } else if ([obj respondsToSelector:@selector(shootAt:)]) {[obj shootAt:target]; } SEL is the Objective-C “type” for a selector SEL shootSelector = @selector(shoot); SEL shootAtSelector = @selector(shootAt:); SEL moveToSelector = @selector(moveTo:withPenColor:);個人感覺這里的code將會有很大的用處,不過我目前還是暫時都不知了的狀態(tài)
If you have a SEL, you can also ask an object to perform it ...Using the performSelector: or performSelector:withObject: methods in NSObject [obj performSelector:shootSelector]; [obj performSelector:shootAtSelector withObject:coordinate];Using makeObjectsPerformSelector: methods in NSArray [array makeObjectsPerformSelector:shootSelector]; // cool, huh? [array makeObjectsPerformSelector:shootAtSelector withObject:target]; // target is an id  In UIButton, - (void)addTarget:(id)anObject action:(SEL)action ...; [button addTarget:self action:@selector(digitPressed:) ...];Foundations
+----Primitive Data Types ---+-----Foundation Data Structures--------+ | int | NSNumber - generic | | double | NSString | | float | NSArray | | struct | NSSet | | struct | NSDictionary | | ... | ... | +----------------------------+---------------------------------------+大概就是這樣....
還有很多的Mutable版本,NSDate還有NSData...
再舉了一些常用的Class Method
Colors & Fonts
因為木有例子存在,一直都聽的磕磕碰碰,充滿了困意
讓我假裝直到每個有關的幾件事吧
UIColor
- 可以設置alpha,我喜歡,透明度
- 可以用RGB,HSB,甚至圖片patten來定義color
- [UIColor greenColor]有此種
- [UIColor lightTextColor]也有此種
Fonts
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];//用到user's contents里+ (UIFont *)systemFontOfSize:(CGFloat)pointSize; + (UIFont *)boldSystemFontOfSize:(CGFloat)pointSize;//用到button之類的困意不斷,我決定把NSMutableString以及這些相關的在demo時候再來復習/學習....
就是這么一個愛貪有趣的人....
總結
以上是生活随笔為你收集整理的lecture 4 : More Objective-C的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: excel删除行闪退_excel打开闪退
- 下一篇: js解释器rhino查看执行环境