Objective-C 的基本数据类型、数字、字符串和集合等介绍

基本类型

Objective-C中的基本类型和C语言中的基本类型一样.主要有:int,long,float,double,char,void, bool等.

在Foundation中,也为些数据定义了别名,如:NSInteger为long,CGFloat为double,BOOL等.

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

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

NSNumber

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

 
  
1 NSNumber * number = [NSNumber numberWithInt: 123 ];
2 NSLog( @" %i " ,[number intValue]);
3 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

 

NSStringNSMutableString

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 str
15   2010 - 12 - 30 11 : 43 : 13.521 HelloWorld[ 2119 :a0f] this is strsss
16
17 可以看出str2不是指向str1的,而是新的对象!!

 

 

NSArrayNSMutableArray

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] 1
10   2010 - 12 - 29 13 : 36 : 16.833 HelloWorld[ 3325 :a0f] Januay
11   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] 1
10 2010 - 12 - 29 13 : 41 : 34.928 HelloWorld[ 3415 :a0f] Januay
11 2010 - 12 - 29 13 : 41 : 34.930 HelloWorld[ 3415 :a0f] Sep

NSSetNSMutableSet

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

代码
 
   
1 NSSet * set = [NSSet setWithObjects:[NSNumber numberWithInt: 10 ], @" bb " , @" aa " , @" bb " , @" aa " ,nil];
2 for (id * obj in set ){
3 NSLog( @" %@ " ,obj);
4 }
5 NSLog( @" %i " ,[ set count]);
6 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


NSDictionaryNSMutableDictionary

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

代码
 
   
1 NSDictionary * dict = [NSDictionary
2 dictionaryWithObjects:[NSArray arrayWithObjects: @" val1 " , @" val2 " ,nil]
3 forKeys:[NSArray arrayWithObjects: @" key2 " , @" key1 " ,nil]];
4
5 for (NSString * key in dict){
6 NSLog( @" %@ " ,[dict objectForKey:key]);
7 }
8 NSLog( @" %i " ,[dict retainCount]);
9 [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排序的。

转载于:https://www.cnblogs.com/pengxl/archive/2010/12/29/1920487.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值