分享

IOS软件开发揭秘--阅读笔记

 许多润泽 2012-11-29
1 ID
可以传递任何消息给id,但如果该id不支持这个消息就会返回一个运行时异常,通常就是:“unrecognisedselector sent to instance to XXX” 消息。

2 SEL
SEL 类型也可以用NSSelectorFromString(NSString *)函数创建

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 判断一个字符串是否为空
  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)anObject
b 增加、插入元素
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 使用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:@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];
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多