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__沈阳SEO);#else# define DLog(...);#endif
5 自动释放池(AutoReleasePool)在程序中,当有大量的自动变量需要管理时,你就需要自行创建 NSAutoreleasePool来管理;在创建线程或者使用NSOperation时,也需要创建独立的NSAutoreasePool 来管理线程;另外,重载didReceiveMemoryWarning()函数是一个好的编程习惯;
6 程序执行流程所以流程应该是这样:(loadView/nib文件)来加载view到内存 ——>viewDidLoad函数进一步初始化这些view ——>内存不足时,调用viewDidUnload函数释放views
—->当需要使用view时有回到第一步
如此循环7 ASIHttpRequest
8 判断一个字符串是否为空if (str == nil)if ([str length] == 0)
9 处理数值对象a. NSInteger ------- intNSNumber *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 处理日期时间NSDatea. 获取当前日期时间的代码如下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. N高端网站设计SCountedSet *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];}