日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

lecture 4 : More Objective-C

發(fā)布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lecture 4 : More Objective-C 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Creating Objects

這件事困擾我一點時間了,ObjC沒有一個Constructor的概念
而在Create Objects這件事上既有用過自己寫的-init,還return instancetype,大概這個,也有用過一些Class Method,就是明確知道是+id類型的,Slides總結了

  • alloc init來創(chuàng)建
NSMutableArray *cards = [[NSMutableArray alloc]init]
  • Class Method來創(chuàng)建
NSString *moltuae = [NSString stringWithFormat:@“%d”, 42];NSString *a = @"hello world";//不過一般應該都用這個吧
  • 也有init 和 Class Method共存的狀況
[NSString stringWithFormat:...] same as [[NSString alloc] initWithFormat:...]

老爺爺推薦alloc/init

  • 利用objects的方法創(chuàng)建新的objects
NSString’s - (NSString *)stringByAppendingString:(NSString *)otherString; NSArray’s - (NSString *)componentsJoinedByString:(NSString *)separator; NSString’s & NSArray’s - (id)mutableCopy;//這個很有意思,將一個immutable的object變成一個mutable的新的object
  • 這種則不是在創(chuàng)建object,雖然傳遞出object
NSArray’s - (id)lastObject; NSArray’s - (id)objectAtIndex:(int)index;

除非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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。