for (int i = 0; i < someLargeNumber; i++) { NSString *string = ...; string = [string lowercaseString]; string = [string stringByAppendingString:...]; NSLog(@“%@”, string); }
for (int i = 0; i < someLargeNumber; i++) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *string = ...; string = [string lowercaseString]; string = [string stringByAppendingString:...]; NSLog(@“%@”, string); [pool release]; }
NSString *stringToReturn = nil; for (int i = 0; i < someLargeNumber; i++) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *string = ...; string = [string stringByAppendingString:...]; if ([string someCondition]) { stringToReturn = [string retain]; } [pool release]; if (stringToReturn) break; } return [stringToReturn autorelease];
2009-06-02 15:21:21, 482 reviews
send to mailbox
#import "Foundation/Foundation.h" int main(int argc, const char * argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //a1是用原始方法初始化创建的,因此a1的内存管理由你负责,下面的变量也是如此, 基本上所有通过init之类方法初始化的变量都需要 自己维护变量的引用计数器,用完就要release NSArray *a1 = [[NSArray alloc] initWithObjects:@"NSArray a1", nil]; //只是把指针赋予a2, 该指针的类型是NSArray, 现在a1和a2所指向的是同一块内存单元,就是指向的数值一样,只是引用不一样,就相当于一个文件你创建了两个快捷方式(或者是linux上的soft link), 因此你修改任何一个值等于同时修改另一个的值 NSArray *a2 = a1; //这里输出结果一样 NSLog([a1 description]); NSLog([a2 description]); NSMutableArray *a3 = [[NSMutableArray alloc] initWithObjects:@"NSMutableArray: a3", nil]; NSMutableArray *a4 = a3; [a3 addObject:@"element2"]; //输出结果一样 NSLog([a3 description]); NSLog([a4 description]); //这里才是像php, ruby中的赋值一样,把a3赋予a5, 不同的指针不同的内存地址引用,完全copy NSMutableArray *a5 = [a3 copy]; [a3 addObject:@"element3"]; //只有a5中有element3 NSLog([a3 description]); NSLog([a5 description]); NSMutableArray *a6 = [[NSMutableArray alloc] init]; //把a3中的元素放入a6, 这里只是数据的放入,不会保留mutable和immutable信息 [a6 addObjectsFromArray:a3]; //注意下面的autorelease, 即把含有a, b的数组加入a6中后,自行释放,就是交给程序去管理了 [a6 addObject:[[[NSArray alloc] initWithObjects:@"a", @"b", nil] autorelease]]; NSLog([a6 description]); NSLog([[a6 objectAtIndex:[a6 count] - 1] description]); //创建a6的可变拷贝赋给a7, 只有有mutable, immutable区分的对象才有mutableCopy方法, 即如果这个地方你用的是copy即使a6是mutable, a7也是immutable NSMutableArray *a7 = [a6 mutableCopy]; [a6 insertObject:@"element4" atIndex:3]; NSLog([a6 description]); [a7 insertObject:@"first" atIndex:0]; //this will be error, because the last element is not mutable array //[[a7 objectAtIndex:[a7 count] - 1] addObject:@"c"]; //this will create the mutable copy //上面注释掉的操作是会出错的,因为a, b数组是immutable, 下面就是创建它的mutable拷贝, 也就是iPhone基础开发教程 8.7开始那个函数实际要做的,只是它是递归调用的 NSMutableArray *ma = [[a6 objectAtIndex:[a6 count] -1] mutableCopy]; [a7 replaceObjectAtIndex:[a7 count] - 1 withObject:ma]; //现在就可以操作了 [[a7 objectAtIndex:[a7 count] - 1] addObject:@"c"]; NSLog([a7 description]); //上面的变量哪些要手工release, 哪些由release pool管理,就不多说了 [a1 release]; [a3 release]; [a5 release]; [a6 release]; [a7 release]; [ma release]; [pool release]; return 0; }
2009-05-14 14:40:47, 427 reviews
由于暂时还没有Mac的本本,只能现在Linux上练习Objective-C的语法了,安装的过程并不顺利,这里采用的平台依旧是Debian, Windows也是可以的,
关于GNUstep就不多说了,自己去官网看吧,总之就是一个跨平台的Objective-C的编译运行环境,下面开始安装:
#apt-get install gnustep
#apt-get install gnustep-devel
#apt-get install libgnustep-base-dev
#apt-get install gnustep-games
这样就安装完毕了,(上面的安装包可以放在一起,一次安装完的,不要小看了那几个包,shitou可是花了很大功夫才找到的,为了表示寻找的痛苦,还是写长点吧,^_^)
然后就是配置了,Firstly:
#echo 'export LIBRARY_PATH=/usr/lib/GNUstep/System/Library/Libraries' >> ~/.profile
#echo 'export OBJC_INCLUDE_PATH=/usr/include/GNUstep/Headers' >> ~/.profile
#source ~/.profile
然后执行GNUstep.sh的脚本,自动加载其他环境PATH:
#chmod +x /usr/share/GNUstep/Makefiles/GNUstep.sh
#source /usr/share/GNUstep/Makefiles/GNUstep.sh
也加入.profile文件, 以便用户登陆系统时加载
#echo 'source /usr/share/GNUstep/Makefiles/GNUstep.sh' >> ~/.profile
这样就设置完毕了,然后就是写个ObjC的程序测试下了,在测试时可能会报的错是NXConstantString之类的错误, 办法就是:
#gcc -o hello hello.m -fconstant-string-class=NSConstantString -lobjc -lgnustep-base
这样就大功告成了, 好了,下面开始正式学习Objective-C...........
2009-03-11 15:59:20, 1394 reviews
放弃Erlang, 转投iPhone开发了,怎样让Linux的gcc支持编译objective-c, 看下面:
这里平台是debian, 首先安装gcc的支持,摸我, 然后安装支持objective-c的包就行了
#apt-get install gobjc
这样就OK了, 使用:
#gcc -x objective-c hello.m -o hello
什么时候才能有米买个Mac的本本啊....
修改: 上面的方法发并不能支持在linux上编译objective-c, 正确的做法应该是这里
2009-03-06 16:12:17, 872 reviews
just DO NOT support IE