@implementation MyObject - (id)init { self = [super init]; // call our super’s designated initializer if (self) { // initialize our subclass here } return self; } @end为什么要给self赋值呢?因为这是一种协议机制,确保super的初始化在我们之前初始化,如果super初始化失败,那就返回nil。
@interface Vehicle - (void)move; @end @interface Ship : Vehicle - (void)shoot; @end Ship *s = [[Ship alloc] init]; [s shoot]; [s move]; Vehicle *v = s; [v shoot];当调用给v 发送shoot的消息时,虽然Vehicle没有shoot方法,但是程序不会崩溃,编译器会给个警告而已,运行时会找到v其实时有shoot方法的。
SEL shootSelector = @selector(shoot); SEL shootAtSelector = @selector(shootAt:); SEL moveToSelector = @selector(moveTo:withPenColor:);
+ (id)arrayWithObjects:(id)firstObject, ...; // nil-terminated arguments NSArray *primaryColors = [NSArray arrayWithObjects:@“red”, @“yellow”, @“blue”, nil]; + (id)arrayWithObject:(id)soleObjectInTheArray; // more useful than you might think! - (int)count; - (id)objectAtIndex:(int)index; - (id)lastObject; // returns nil (doesn’t crash) if there are no objects in the array - (NSArray *)sortedArrayUsingSelector:(SEL)aSelector; - (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)selectorArgument; - (NSString *)componentsJoinedByString:(NSString *)separator; - (BOOL)containsObject:(id)anObject; // could be slow, think about NSOrderedSet不能把nil放到数组中。NSNull都能放进去,但是它只是个占位符。
+ (id)arrayWithCapacity:(int)initialSpace; // initialSpace is a performance hint only + (id)array; - (void)addObject:(id)anObject; // at the end of the array - (void)insertObject:(id)anObject atIndex:(int)index; - (void)removeObjectAtIndex:(int)index; - (void)removeLastObject; - (id)copy;
+ (id)dictionaryWithObjects:(NSArray *)values forKeys:(NSArray *)keys; + (id)dictionaryWithObjectsAndKeys:(id)firstObject, ...; NSDictionary *base = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:2], @“binary”, [NSNumber numberWithInt:16], @“hexadecimal”, nil]; - (int)count; - (id)objectForKey:(id)key; - (NSArray *)allKeys; - (NSArray *)allValues;NSMutableDicationary
+ (id)dictionary; // creates an empty dictionary (don’t forget it inherits + methods from super) - (void)setObject:(id)anObject forKey:(id)key; - (void)removeObjectForKey:(id)key; - (void)removeAllObjects; - (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
+ (id)setWithObjects:(id)firstObject, ...; + (id)setWithArray:(NSArray *)anArray; - (int)count; - (BOOL)containsObject:(id)anObject; - (id)anyObject; - (void)makeObjectsPerformSelector:(SEL)aSelector;
- (void)addObject:(id)anObject; // does nothing if object that isEqual:anObject is already in - (void)removeObject:(id)anObject; - (void)unionSet:(NSSet *)otherSet; - (void)minusSet:(NSSet *)otherSet; - (void)intersectSet:(NSSet *)otherSet;
- (int)indexOfObject:(id)anObject; - (id)objectAtIndex:(int)anIndex; - (id)firstObject; and - (id)lastObject; - (NSArray *)array; - (NSSet *)set;NSMutableOrderSet
- (void)insertObject:(id)anObject atIndex:(int)anIndex; - (void)removeObject:(id)anObject; - (void)setObject:(id)anObject atIndex:(int)anIndex;
NSSet *mySet = ...; for (id obj in mySet) { if ([obj isKindOfClass:[NSString class]]) { }
NSDictionary *myDictionary = ...; for (id key in myDictionary) { // do something with key here id value = [myDictionary objectForKey:key]; // do something with value here }
- (void)setDouble:(double)aDouble forKey:(NSString *)key; - (NSInteger)integerForKey:(NSString *)key; // NSInteger is a typedef to 32 or 64 bit int
- (void)setObject:(id)obj forKey:(NSString *)key; // obj must be a Property List - (NSArray *)arrayForKey:(NSString *)key; // will return nil if value for key is not用
容芳志 (http://blog.csdn.net/totogo2010)
本文遵循“署名-非商业用途-保持一致”创作公用协议