Objective-C 程序设计读书笔记
第15章 数字、字符串和集合
字符串常用操作
NSString *str1 = @"This is String A";
NSString *str2 = [ str1 substringWithRange:NSMakeRange(8, 6)];
NSRange subRange = [str1 rangeOfString:str2];
if(subRange.location == NSNotFound)
{
NSLog(@"String not found");
}else{
NSLog(@"String is at index %lu,length is %lu",subRange.location,subRange.length);
}
NSMutableString *mstr = [NSMutableString stringWithString:str1];
NSUInteger number = [mstr replaceOccurrencesOfString:@"This is"
withString:@"An example of"
options:nil
range:NSMakeRange(0, mstr.length)];
数组常用操作
NSArray *books = @[@"book4",@"book1",@"book2",@"book3"];
NSArray *sortedBooks = [books sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
for (NSString *book in sortedBooks) {
NSLog(@"%@",book);
}
NSMutableArray * array = [@[@"1",@"2"] mutableCopy];
array[1] = @"3";
NSLog(@"%@",array[1]);
NSMutableDictionary * people = [@{@"k1":@"v1"} mutableCopy];
people[@"k1"] = @"v2";
NSLog(@"%@",people[@"k1"]);
第16章 使用文件
复制文件和使用NSProcessInfo
基本的文件操作:NSFileHandle
NSFileManager *fm = [NSFileManager defaultManager];
[NSBundle mainBundle]pathForResource:(NSString *) ofType:(NSString *)]
NSArray *images = [[NSBundle mainBundle]pathsForResourcesOfType:@"jpg" inDirectory:@"images"];
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
第17章 内存管理和自动引用计数
//当程序中出现了大量临时的对象
for (i=0; i<n ; ++i) {
@autoreleasepool {
… //与临时对象打交道
}
}
当ARC遇到方法调用时,如果方法名字以alloc,new,copy,mutableCopy或int这些词开头,它会假定方法返回对象的所有者给方法的调用者
disable ARC for a single file
Compiler Flags -fno-objc-arc
第18章 复制对象