ObjectC类类型.docx

上传人:b****3 文档编号:6314722 上传时间:2023-05-09 格式:DOCX 页数:5 大小:16.70KB
下载 相关 举报
ObjectC类类型.docx_第1页
第1页 / 共5页
ObjectC类类型.docx_第2页
第2页 / 共5页
ObjectC类类型.docx_第3页
第3页 / 共5页
ObjectC类类型.docx_第4页
第4页 / 共5页
ObjectC类类型.docx_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

ObjectC类类型.docx

《ObjectC类类型.docx》由会员分享,可在线阅读,更多相关《ObjectC类类型.docx(5页珍藏版)》请在冰点文库上搜索。

ObjectC类类型.docx

ObjectC类类型

Object-c中的数据类型

导航:

基本类型

ID

对象类型常见的有

对象类型

-NSLog

-NSNumber

-NSString和NSMutableString

-NSArray和NSMutableArray

-NSSet和NSMutableSet

-NSDictionary和NSMutableDictionary

基本类型:

Objective-C中的基本类型和C语言中的基本类型一样.主要有:

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

在Foundation中,也为些数据定义了别名,如:

NSInteger为long,CGFloat为double,BOOL等.

Objective-C也可以用C语言的构造类型,如数组、结构体、同用体等。

对于基本类型变量,不需要用指针,也不用手动回收,方法执行结束会自动回收。

ID:

在object-c中,对象标识被作为一个特殊的数据类型:

id。

这个数据类型定义为引用对象的指针。

实际上是指向对象实例变量的指针。

对象类型常见的有:

NSlog

NSString

NSInteger

NSURL

NSImage

NSNumber

NSLog

格式如下

%@对象

%d,%i整数

%u无符整形

%f浮点/双字

%x,%X二进制整数

%zusize_t%p指针

%e浮点/双字

%g浮点/双字

%sC字符串

%*sPascal字符串

%c字符

%Cunicha

%lld64位长整数

(longlong)%llu无符64位长整数

%Lf64位双字

NSNumber

NSNumber是Objective-c的数字对象。

需求考虑内存释放问题。

1 NSNumber*number=[NSNumbernumberWithInt:

123];

2 NSLog(@"%i",[numberintValue]);

3 NSLog(@"%i",[numberretainCount]);

//输出

2010-12-2916:

02:

35.040HelloWorld[4710:

a0f]123

2010-12-2916:

02:

35.042HelloWorld[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...]”方法初始化,需要考虑手动释放。

1     NSString *str = @"this is str...";2     NSMutableString *mstr = [NSMutableString stringWithString:

str];3     str = @"sss";4     NSLog(@"%@",mstr);5     NSLog(@"%@",str);

输出:

1 this is str...2 sss

注:

因为NSMutableString是NSString的子类,实际应用中很可以把NSMutableString变量赋给NSString。

所以若用NSString做类的属性,也会用手动释放的方式:

 1 //接口文件 2  @interface TestProperty :

 NSObject { 3     NSString *name; 4     NSInteger myInt; 5 } 6  7 @property (copy,nonatomic) NSString *name; 8 @property NSInteger myInt; 9 10 @end

 1 //实现类 2  @implementation TestProperty 3 @synthesize name; 4 @synthesize myInt; 5   6  -(void) dealloc{ 7     self.name = nil; 8     [super dealloc]; 9 }10  11 @end

例:

代码

 1 int main (int argc, const char * argv[]) { 2   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 3    4   NSMutableString *str1 = [NSMutableString stringWithString:

@"this is str"]; 5   NSMutableString *str2 = [NSMutableString stringWithString:

str1]; 6   [str2 appendString:

@"sss"]; 7   NSLog(@"%@",str1); 8   NSLog(@"%@",str2); 9   [pool drain];10   return 0;11 }12 13  //输出14  2010-12-30 11:

43:

13.511 HelloWorld[2119:

a0f] this is str15  2010-12-30 11:

43:

13.521 HelloWorld[2119:

a0f] this is strsss16 17 可以看出str2不是指向str1的,而是新的对象!

 

NSArray和NSMutableArray

NSArray是不可变数组,一般用于保存固定数据。

和NSString不同的是,NSArray有retainCount,所以释放问题。

NSMubleArray是变数组,可以直接对其值进行操作。

也可考虑释放问题。

NSMubleArray是NSArray的子类。

 1     NSArray *arr = [NSArray arrayWithObjects:

@"Sep",@"Januay",@"",nil]; 2     NSArray *arr_ = [arr sortedArrayUsingSelector:

@selector(compare:

)]; 3     NSLog(@"%i",[arr retainCount]); 4     for(NSString *name in arr_){ 5         NSLog(@"%@",name); 6     } 7  8  //输出 9  2010-12-29 13:

36:

16.830 HelloWorld[3325:

a0f] 110  2010-12-29 13:

36:

16.833 HelloWorld[3325:

a0f] Januay11  2010-12-29 13:

36:

16.833 HelloWorld[3325:

a0f] Sep

代码

 1     NSMutableArray *arr = [NSMutableArray   arrayWithObjects:

@"Sep",@"Januay",@"",nil]; 2     [arr sortUsingSelector:

@selector(compare:

)]; 3     NSLog(@"%i",[arr retainCount]); 4     for(NSString *name in arr){ 5         NSLog(@"%@",name); 6     } 7  8  //输出 9 2010-12-29 13:

41:

34.925 HelloWorld[3415:

a0f] 110 2010-12-29 13:

41:

34.928 HelloWorld[3415:

a0f] Januay11 2010-12-29 13:

41:

34.930 HelloWorld[3415:

a0f] Sep

NSSet和NSMutableSet

 NSSet和NSMutableSet分别是不可变集合和可变集合。

集合是一组单值的操作。

NSSet和NSMutableSet都需要考虑释放问题。

代码

1  NSSet*set=[NSSetsetWithObjects:

[NSNumbernumberWithInt:

10],@"bb",@"aa",@"bb",@"aa",nil];

2  for(id*objinset){

3    NSLog(@"%@",obj);

4  }

5  NSLog(@"%i",[setcount]);

6  NSLog(@"%i",[setretainCount]);

//输出

2010-12-2913:

56:

08.397HelloWorld[3709:

a0f]10

2010-12-2913:

56:

08.400HelloWorld[3709:

a0f]aa

2010-12-2913:

56:

08.401HelloWorld[3709:

a0f]bb

2010-12-2913:

56:

08.401HelloWorld[3709:

a0f]3

2010-12-2913:

56:

08.402HelloWorld[3709:

a0f]1

NSDictionary和NSMutableDictionary

dictionary是由键-对象对组成的数据集合。

NSDictionay和NSMutableDicionary都需要考虑内存释放问题。

代码

1  NSDictionary*dict=[NSDictionary 

2             dictionaryWithObjects:

[NSArrayarrayWithObjects:

@"val1",@"val2",nil] 

3             forKeys:

[NSArrayarrayWithObjects:

@"key2",@"key1",nil]];

4   

5  for(NSString*keyindict){

6    NSLog(@"%@",[dictobjectForKey:

key]);

7  }

8  NSLog(@"%i",[dictretainCount]);

9  [pooldrain];

//输出

2010-12-2915:

37:

42.745HelloWorld[4085:

a0f]val2

2010-12-2915:

37:

42.748HelloWorld[4085:

a0f]val1

2010-12-2915:

37:

42.749HelloWorld[4085:

a0f]1

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

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > PPT模板 > 图表模板

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2