第七章:循环
1:for
循环次数必须是整数,因为你没有办法循环2.7次
int x; for (x = 1; x <= 10; x++) { NSLog(@"Julia is a pretty actress."); } NSLog(@"The value of x is %d", x);
2:while () { }和do {} while ()
int counter = 1; while (counter <= 10) { NSLog(@"Julia is a pretty actress.\n"); counter = counter + 1; } NSLog(@"The value of counter is %d", counter);
int counter = 1; do { NSLog(@"Julia is a pretty actress.\n"); counter = counter + 1; } while (counter <= 10); NSLog(@"The value of counter is %d", counter);
第八章:带有图形界面的程序
Objective-C语言信息传递的一般格式:
不带参数
[receiver message];
带参数的形式。
[receiver message:argument];
所有的工作部分都放在中括号里面,而且要在结尾加上那个永恒不变的分号。在括号里,接收对象的名称放在首位,紧随其后的是方法,用“:”后面加参数
你编写的窗口的类如果没有包含常规行为的代码时,信息会自动地上溯到它所继承功能的原始的类那里(称作“父类superclass”)
如何去执行你自己定义的行为而不是从父类那里继承来的方法??
调用父类提供的方法
[super close];
第九章寻找方法
第十章:awakeFromNib方法
第十一章:指针
第十二章:字符串
NSString *favoriteActress = @"Julia";
指针变量favoriteActress指向内存中的一个位置,这个位置存储着字符串“Julia”。
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSMutableString *foo; foo = [@"Julia!" mutableCopy]; [foo appendString:@" I am happy"]; NSLog(@"Here is the result: %@.", foo); [pool release]; return 0; }
Objective-C语言并不直接操作对象,而总是借助指针。这就是为什么???
第十三章:数组
通过执行下面这个步骤可以创建数组:
[NSMutableArray array]
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSMutableArray *myArray = [NSMutableArray array]; [myArray addObject:@"first string"]; [myArray addObject:@"second string"]; [myArray addObject:@"third string"]; int count = [myArray count]; NSLog(@"There are %d elements in my array", count); [pool release]; return 0; } 运行结果如下: There are 3 elements in my array
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSMutableArray *myArray = [NSMutableArray array]; [myArray addObject:@"first string"]; [myArray addObject:@"second string"]; [myArray addObject:@"third string"]; NSString *element = [myArray objectAtIndex:0]; // [2.13] NSLog(@"The element at index 0 in the array is: %@", element); [pool release]; return 0; }
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSMutableArray *myArray = [NSMutableArray array]; [myArray addObject:@"first string"]; [myArray addObject:@"second string"]; [myArray addObject:@"third string"]; int i; int count; for (i = 0, count = [myArray count]; i < count; i = i + 1) { NSString *element = [myArray objectAtIndex:i]; NSLog(@"The element at index %d in the array is: %@", i, element); } [pool release]; return 0; } 运行结果如下: The element at index 0 in the array is: first string The element at index 1 in the array is: second string The element at index 2 in the array is: third string
需要注意的是数组不仅仅可以用于字符串操作。它可以用于任何你希望用数组操作的对象。
第十四章内存管理
Cocoa的内存管理技术就是通常所说的“援引计数”
第十五章信息资源