第二課名稱是:Objective-C
回顧上節課的內容:
- 創建了單個MVC模式的項目
- 顯示項目的各個文件,顯示或隱藏導航,Assistant Editor, Console, Object Library, Inspector等功能的使用
- 在故事版上編輯視圖,通過Ctrl+拖拽把view連接到Controller的outlet。
- 創建新的類,比如 CalculatorBrain
- 使用@synthesize
- 延遲實例化實現getter
- [ ]中括號的使用
- 私有方法在.m文件中定義
- 使用strong weak屬性
- 處理代碼中的警告和錯誤
- 相關Obj-c的語法知識,比如NSString 的使用
1、爲什麼用property,理由有兩個:
- 實體變量的安全性和繼承能力
- 提供延遲實例化,比如:UI更新,一次性檢測。
2、爲什麼用.號
- 美觀,可讀性增強
- 可以和C語言的結構體配合
4、 nil =0.
5、類方法和實例方法
6、實例化
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;
- @implementation MyObject
- - (id)init
- {
- self = [super init]; // call our super’s designated initializer
- if (self) {
- // initialize our subclass here
- }
- return self;
- }
- @end
- @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];
8、內省
- SEL shootSelector = @selector(shoot);
- SEL shootAtSelector = @selector(shootAt:);
- SEL moveToSelector = @selector(moveTo:withPenColor:);
[obj performSelector:shootAtSelector withObject:coordinate];有一個參數的SEL。
9、foundation 框架
-(NSString*)description ,用在NSLog,%@。
- + (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
- + (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;
- + (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;
- - (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
- }
10、property List
- - (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)
本文遵循“署名-非商業用途-保持一致”創作公用協議
原文地址:http://blog.csdn.net/totogo2010/article/details/8213958