总结程序启动的过程如下:
1. 程序入口main函数创建UIApplication实例和UIApplication代理实例。
2. 在UIApplication代理实例中重写启动方法,设置第一ViewController。
3. 在第一ViewController中添加控件,实现应用程序界面。
1.调用方法用[],继承用:
2.实例对象
一、NSString* string1 = [NSString string]; //自动释放
二、NSString* string2 = [[NSString alloc] init]; //用完后要手动释放,alloc分配内存和实例化,init初始化
[string2 release] ;
3.创建对象
在Objective-C的语法中,一个类通常分为两部分。h文件声明,m文件实现。
方法都是public的,成员变量都是protect的,(方法前面用-减号,是public,+号即static方法)
protocol相当于java的接口
h文件,提供get,set访问器:
#import <Cocoa/Cocoa.h> @interface Photo : NSObject { NSString* caption; NSString* photographer; } @property (retain) NSString* caption; @property (retain) NSString* photographer; @end
m文件,@synthesize指令为我们主动生成了setter和getter,所以我们必须要做的就
只有实现dealloc方法了。
只有当访问器不存在的时候,@synthesize才会自动生成访问器,所以,即使是使
用@synthesize声明了一个属性,你仍然可以实现自定义的getter和setter。
#import "Photo.h" @implementation Photo @synthesize caption; @synthesize photographer; - (void) dealloc { [caption release]; [photographer release]; [super dealloc]; } @end
‐ (void) dealloc { self.caption = nil; self.photographer = nil; [super dealloc]; }
4.nil
相当于其他语言的null指针,不同在于在nil上调用方法不会导致异常
5.类目(Category)
类似于重写(重点是 @implementation 跟 @interface 这 两 行的括号)
#import <Cocoa/Cocoa.h> @interface NSString (Utilities) - (BOOL) isURL; @end
#import "NSString-Utilities.h" @implementation NSString (Utilities) - (BOOL) isURL { if ( [self hasPrefix:@"http://"] ) return YES; else return NO; } @end
通过在变量前增加IBOutlet来说明该变量将与界面上的某个UI对象对应,
在方法前增加IBAction来说明该方法将与界面上的事件对应.
7.异常(异常处理只有 Mac OS X 10.3 以上才支持)
@try{}
@catch{Exception *e}
@finally{}
8.id类型,不用知道上面类型,有此方法就响应,不用像java那样必须转换类型才能调用方法
9.迭代器NSEnumerator (NSArray不能改变长度,NSMutableArray可以)
NSDictionary相当于java的map
NSArray *array = [NSArray array ]; NSEnumerator *enumerator = [array objectEnumerator]; id obj; while ( obj = [enumerator nextObject] ) { printf( "%s\n", [[obj description] cString] ); }
原文:http://blog.csdn.net/maple1320/article/details/19155093