1 ID 可以传递任何消息给id,但如果该id不支持这个消息就会返回一个运行时异常,通常就是:“unrecognisedselector sent to instance to XXX” 消息。 2 SEL SEL 类型也可以用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./dream-2011/apples-third-party-development-libraries-asihttprequest.html 8 判断一个字符串是否为空 9 处理数值对象 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]); 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]; NSString *myDateString = @"2009-09-15 18:30:00"; NSDate *myDate = [df dateFromString: myDateString]; switch ([dateToDay compare:myDate]) { case NSOrderedSame: break; case NSOrderedAscending: break; case NSOrderedDescending: break; default: break; } 11 b 增加、插入元素 NSMutableArray *array = [NSMutableArray alloc] init]; [array addObject:anObject]; [array insertObject:anObject atIndex:2]; [array addObjectsFromArray:anotherArray]; c 获取某个元素的索引值 NSInteger idx = [array indexOfObject:anObject]; d 更新数组元素 e 数组遍历 1.使用枚举 for (NSString *str in array) { } 2 使用NSEnumerator NSEnumerator *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 b. NSCountedSet *cset = [[NSCountedSet alloc] initWithArray: array]; 13 OC中产生随机数的方法 srandom(time(NULL)); arc4random()%n; 14 数组map操作(-makeObjectsPerformSelect 该函数可以将一个操作作用在数组中的所有元素上,如; NSArray *fighters = ...; [fighters makeObjectsPerformSelect - (void)fly:(id)sender { } 15 对象数组排序(使用NSSortDescriptor) 16 对象数组过滤 (使用 NSPredicate) NSPredicate *aPredicate = [NSpredicate predicateWithFormat:@"SELF.lastName beginswith[c] 'a'"]; NSArray *array = [array filteredArrayUsingPredic 17 删除数组中元素 一种更安全的方法,将满足条件的元素放进一个临时数组,再将这个数组返回,代码如下: - (NSArray *) filterPersonWithLastName 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 } [personList removeObjectsInArray:personsToRemove]; }
|
|