標籤:
nil是一個對象指標為空白,Nil是一個類指標為空白,NULL是基礎資料型別 (Elementary Data Type)為空白。這些可以理解為nil,Nil, NULL的區別吧。
iOS剪下板
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = @"要賦給剪下板的字串";
1 ID
可以傳遞任何訊息給id,但如果該id不支援這個訊息就會返回一個運行時異常,通常就是:“unrecognisedselector sent to instance to XXX” 訊息。 2 SELSEL 類型也可以用NSSelectorFromString(NSString *)函數建立 3 nil 用來給對象賦值,NULL 則給任何指標賦值,NULL 和 nil 不能互換,nil 用於類指標賦值,而NSNull 則用於集合賦值,如:a.if (object == nil) {}//判斷對象為空白 b.UIViewController *controller = [NSArray objectAtIndex:i];//判斷數組元素是否為空白if ((NSNull *)controller == [NSNull null]) {//...} c.NSString *userId = [NSDictionary objectForKey:@"UserID"];//判斷字典對象的元素是否為空白if (userId == [NSNull null]) {}4 預先處理宏a 關閉調試資訊:#define DLog();b
列印檔案名,行號,函數詳情,函數名資訊,
NSLog(@"%s %d %s",__FILE__, __LINE__,__PRETTY_FUNCTION__,__FUNCTION__);#ifdef DEBUG# define DLog(fmt,...) NSLog((@"%s [Line %d]" fmt), __PRETTY_FUNCTION__,__LINE__,##__VA_ARGS__);#else# define DLog(...);#endif 5 自動釋放池(AutoReleasePool)在程式中,當有大量的自動變數需要管理時,你就需要自行建立 NSAutoreleasePool來管理;在建立線程或者使用NSOperation時,也需要建立獨立的NSAutoreasePool 來管理線程;另外,重載didReceiveMemoryWarning()函數是一個好的編程習慣; 6 程式執行流程所以流程應該是這樣:
(loadView/nib檔案)來載入view到記憶體 ——>viewDidLoad函數進一步初始化這些view ——>記憶體不足時,調用viewDidUnload函數釋放views
—->當需要使用view時有回到第一步
如此迴圈7 ASIHttpRequest http://www.dreamingwish.com/dream-2011/apples-third-party-development-libraries-asihttprequest.html 8 判斷一個字串是否為空白 if (str == nil) if ([str length] == 0) 9 處理數值對象 a. NSInteger ------- int NSNumber *numObj = [NSNumber numberWithInt:2]; NSInteger myInteger = [numObj integerValue]; int a = [myInteger intValue]; b. 浮點數值使用CGFloat。NSDecimalNumber 對象進行處理NSDecimalNumber *myDecimalObj = [[NSDecimalNumber allo] initWithString:@"23.39"];NSLog(@"myDecimalObj doubleValue = %6.3f",[myDecimalObj doubleValue]);CGFloat myCGFloatValue = 43.4;NSDecimalNumber *myOtherDecimalObj = [[NSDecimalNumber alloc] initWithFloat:myCGFloatValue];NSLog(@"myOtherDecimalObj doubleValue=%6.3f",[myOtherDecimalObj doubleValue]); 10 處理日期時間NSDate a. 擷取當前日期時間的代碼如下NSDate *dateToDay = [NSDate date];NSDateFormatter *df = [[NSDateFormatter alloc] init];[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];NSLocale *locale = [[NSlocale alloc] initWithLocalIdentifier:@"en_US"];[df setLocale:locale]; b. 從字串產生日期對象的代碼如下NSString *myDateString = @"2009-09-15 18:30:00";NSDate *myDate = [df dateFromString: myDateString]; c. 日期比較的代碼switch ([dateToDay compare:myDate]) {case NSOrderedSame:break;case NSOrderedAscending:break;case NSOrderedDescending:break;default:break;} 11 常用數組操作 a 判斷數組中是否包含某個對象 - (BOOL)containsObject:(id)anObjectb 增加、插入元素NSMutableArray *array = [NSMutableArray alloc] init];[array addObject:anObject];[array insertObject:anObject atIndex:2];[array addObjectsFromArray:anotherArray];c 擷取某個元素的索引值NSInteger idx = [array indexOfObject:anObject];d 更新數組元素 [mutableArray replaceObjectAtIndex:idx withObject:[NSNumber numberWithInt:9]]e 數組遍曆1.使用枚舉for (NSString *str in array) {}2 使用NSEnumeratorNSEnumerator *enumerator = [array objectEnumerator];id obj;for ( obj == [enumerator nextObject]) {}3.使用for迴圈for (int i = 0; i < [array count]; i++) {[array objectAtIndex:i];} 12 字串數組排序a. NSArray *sortedArray = [array sortedArrayusingSelector:@selector(caseInsensitiveCompare:)];b. NSCountedSet *cset = [[NSCountedSet alloc] initWithArray: array]; NSArray *sorted = [[cset allObjects] sortedArrayUsingSelector:@selector(compare:)]; 13 OC中產生隨機數的方法srandom(time(NULL));arc4random()%n; 14 數組map操作(-makeObjectsPerformSelector())該函數可以將一個操作作用在數組中的所有元素上,如;NSArray *fighters = ...;[fighters makeObjectsPerformSelector:@selector(fly:)]; - (void)fly:(id)sender {} 15 對象數組排序(使用NSSortDescriptor) 16 對象數組過濾 (使用 NSPredicate)NSPredicate *aPredicate = [NSpredicate predicateWithFormat:@"SELF.lastName beginswith[c] ‘a‘"];NSArray *array = [array filteredArrayUsingPredicate:aPredicate]; 17 刪除數組中元素一種更安全的方法,將滿足條件的元素放進一個臨時數組,再將這個數組返回,代碼如下:- (NSArray *) filterPersonWithLastName:(NSString *)filterText {Person *person = [Person alloc ] init];NSMutableArray *personList = [person creatTempraryList];NSLog(@"before");NSMutableArray *personsToRemove = [NSMutableArray array];for (Person *person in personList) {if (filterText && [filterText rangeOfString:person.laseName options:NSLiteralSearch | NSCaseInsensitiveSearch].length == 0)[personsToRemove addObject:person];}[personList removeObjectsInArray:personsToRemove];}
ios開發理解nil,Nil, NULL