Datatype

基本类型

void, char bool, int, long, float, double

在Foundation中为这些数据定义了别名,NSInteger为long, CGFloat为double,BOOL等。

基本对象类型

id

引用对象实例变量的指针

常见对象类型

NSLog

NSNumber

NSString, NSMutableString

NSArray, NSMutableArray

NSSet, NSMutableSet

NSDictionary, NSMytableDictionary

NSURL, NSImage

NSNumber

NSNumber是Objective-c的数字对象。需求考虑内存释放问题。

   NSNumber *number = [NSNumber numberWithInt:123];
   NSLog(@"%i",[number intValue]);
   NSLog(@"%i",[number retainCount]);

//输出
2010-12-29 16:02:35.040 HelloWorld[4710:a0f] 123
2010-12-29 16:02:35.042 HelloWorld[4710:a0f] 1

NSString和NSMutableString

NSString是不可变字符串(NSContantString),其变量和其本类型一样不需要手动释放(它的retainCount为-1)。

NSString赋值:

NSString *str1 = @"str....";  //(不需要手动释放)
NSString *str2 = [[NSString alloc] initWithString:@"str..."]; //不需要手动释放

因为对NSString赋值,会产生成的对象,所在方法中用NSString作临时对象,也要考虑内存开消问题。

NSMutableString是可变字符串,若用 “[[NSMutableString alloc] init...]”方法初始化,需要考虑手动释放。

    NSString *str = @"this is str...";
    NSMutableString *mstr = [NSMutableString stringWithString:str];
    str = @"sss";
    NSLog(@"%@",mstr);
    NSLog(@"%@",str);
输出:

 this is str...
 sss

注:因为NSMutableString是NSString的子类,实际应用中很可以把NSMutableString变量赋给NSString。所以若用NSString做类的属性,也会用手动释放的方式:

 //接口文件
 @interface TestProperty : NSObject {
     NSString *name;
     NSInteger myInt;
 }
 
 @property (copy,nonatomic) NSString *name;
 @property NSInteger myInt;
 
 @end
 //实现类
 @implementation TestProperty
 @synthesize name;
 @synthesize myInt;
  
  -(void) dealloc{
     self.name = nil;
     [super dealloc];
 }
  
 @end
 //实例
 int main (int argc, const char * argv[]) {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   NSMutableString *str1 = [NSMutableString stringWithString:@"this is str"];
   NSMutableString *str2 = [NSMutableString stringWithString:str1];
   [str2 appendString:@"sss"];
   NSLog(@"%@",str1);
   NSLog(@"%@",str2);
   [pool drain];
   return 0;
 }
 
  //输出
  2010-12-30 11:43:13.511 HelloWorld[2119:a0f] this is str
  2010-12-30 11:43:13.521 HelloWorld[2119:a0f] this is strsss
 
 可以看出str2不是指向str1的,而是新的对象!!

NSArray和NSMutableArray

NSArray是不可变数组,一般用于保存固定数据。和NSString不同的是,NSArray有retainCount,所以释放问题。

NSMubleArray是变数组,可以直接对其值进行操作。也可考虑释放问题。

NSMubleArray是NSArray的子类。

     NSArray *arr = [NSArray arrayWithObjects:@"Sep",@"Januay",@"",nil];
     NSArray *arr_ = [arr sortedArrayUsingSelector:@selector(compare:)];
     NSLog(@"%i",[arr retainCount]);
     for(NSString *name in arr_){
         NSLog(@"%@",name);
     }
 
  //输出
  2010-12-29 13:36:16.830 HelloWorld[3325:a0f] 1
  2010-12-29 13:36:16.833 HelloWorld[3325:a0f] Januay
  2010-12-29 13:36:16.833 HelloWorld[3325:a0f] Sep

代码

     NSMutableArray *arr = [NSMutableArray   arrayWithObjects:@"Sep",@"Januay",@"",nil];
     [arr sortUsingSelector:@selector(compare:)];
     NSLog(@"%i",[arr retainCount]);
     for(NSString *name in arr){
         NSLog(@"%@",name);
     }
 
  //输出
 2010-12-29 13:41:34.925 HelloWorld[3415:a0f] 1
 2010-12-29 13:41:34.928 HelloWorld[3415:a0f] Januay
 2010-12-29 13:41:34.930 HelloWorld[3415:a0f] Sep

NSSet和NSMutableSet

NSSet和NSMutableSet分别是不可变集合和可变集合。集合是一组单值的操作。NSSet和NSMutableSet都需要考虑释放问题。

代码

    NSSet *set = [NSSet setWithObjects:[NSNumber numberWithInt:10],@"bb",@"aa",@"bb",@"aa",nil];
    for(id *obj in set){
        NSLog(@"%@",obj);
    }
    NSLog(@"%i",[set count]);
    NSLog(@"%i",[set retainCount]);

//输出
2010-12-29 13:56:08.397 HelloWorld[3709:a0f] 10
2010-12-29 13:56:08.400 HelloWorld[3709:a0f] aa
2010-12-29 13:56:08.401 HelloWorld[3709:a0f] bb
2010-12-29 13:56:08.401 HelloWorld[3709:a0f] 3
2010-12-29 13:56:08.402 HelloWorld[3709:a0f] 1

NSDictionary和NSMutableDictionary

dictionary是由键-对象对组成的数据集合。NSDictionay和NSMutableDicionary都需要考虑内存释放问题。

代码

    NSDictionary *dict = [NSDictionary 
                          dictionaryWithObjects:[NSArray arrayWithObjects:@"val1",@"val2",nil] 
                          forKeys:[NSArray arrayWithObjects:@"key2",@"key1",nil]];
    
    for(NSString *key in dict){
        NSLog(@"%@",[dict objectForKey:key]);
    }
    NSLog(@"%i",[dict retainCount]);
    [pool drain];

//输出
2010-12-29 15:37:42.745 HelloWorld[4085:a0f] val2
2010-12-29 15:37:42.748 HelloWorld[4085:a0f] val1
2010-12-29 15:37:42.749 HelloWorld[4085:a0f] 1

由上面结果可以看出Dicionary是按Key排序的。

数组

int matrix[4][3] ={ { 1, 2, 3 },{ 4, 5, 6 },{ 7, 8, 9 } };//第4行为0 . 
int matrix[4][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };//一样的定义
如果数组大小没有说明,则有初始化列表确定。

结构

struct point {
 float x;
 float y; 
} start = {100.0, 200.0};
struct point end = { .y = 500, .x = 200 };

struct entry {
 char *word;
 char *def; 
} dictionary[1000] = { 
 { ¨a〃, ¨first letter of the alphabet〃 },
 { ¨aardvark〃, ¨a burrowing African mammal〃 },
 { ¨aback〃, ¨to startle〃 } 
};

struct entry {
 char *word; 
 char *def; 
} dictionary[1000] = { 
 [0].word = ¨a〃, [0].def = ¨first letter of the alphabet〃,
 [1].word = ¨aardvark〃, [1].def = ¨a burrowing African mammal〃,
 [2].word = ¨aback〃, [2].def = ¨to startle〃 
};

struct entry {
 char *word; 
 char *def; 
} dictionary[1000] = { 
 {.word = ¨a〃, .def = ¨first letter of the alphabet〃 }, 
 {.word = ¨aardvark〃, .def = ¨a burrowing African mammal〃} ,
 {.word = ¨aback〃, .def = ¨to startle〃} 
};

联合

union shared {
 long long int l;
 long int w[2]; 
} swap = { 0xffffffff }; 
union shared swap2 = {.w[0] = 0x0, .w[1] = 0xffffffff};

线程

NSProcessInfo

反射

NSClassFromString

KVC (Key Value Coding)

- (void) setValue:(id)value forKey:(NSString *)property;
-(id) valueForKey:(NSString *)property;
KVO (Key Value Observing)

当指定的对象的属性被修改了,允许对象接受到通知的机制。