Creating Objects
这件事困扰我一点时间了,ObjC没有一个Constructor的概念
而在Create Objects这件事上既有用过自己写的-init,还return instancetype,大概这个,也有用过一些Class Method,就是明确知道是+id类型的,Slides总结了
- alloc init来创建
NSMutableArray *cards = [[NSMutableArray alloc]init]
- Class Method来创建
NSString *moltuae = [NSString stringWithFormat:@“%d”, 42];
NSString *a = @"hello world";//不过一般应该都用这个吧
- 也有init 和 Class Method共存的状况
[NSString stringWithFormat:...] same as [[NSString alloc] initWithFormat:...]
老爷爷推荐alloc/init
- 利用objects的方法创建新的objects
NSString’s - (NSString *)stringByAppendingString:(NSString *)otherString;
NSArray’s - (NSString *)componentsJoinedByString:(NSString *)separator;
NSString’s & NSArray’s - (id)mutableCopy;//这个很有意思,将一个immutable的object变成一个mutable的新的object
- 这种则不是在创建object,虽然传递出object
NSArray’s - (id)lastObject;
NSArray’s - (id)objectAtIndex:(int)index;
除非NSArray不存在才会创建出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
如果出现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是没有参数的
第二个shootAt有一个参数,对于obj使用了target
Method有多个参数需要写成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将会有很大的用处,不过我目前还是暂时都不知了的状态
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时候再来复习/学习....
就是这么一个爱贪有趣的人....