您的位置:首页 > 其它

oc基础知识4:NSNumber、NSString、NSArray等

2013-11-28 20:28 288 查看
Foundation类框架中含有很多有用的、面向数据的低级类和数据类型,现在简单的列出几项:

1、NSNumber类:用来包装基本数据类型,如int、float等。

(1)把基本数据类型封装成NSNumber类的对象。

Int num =1;

float num2 = 4.5;

BOOL isBool = NO;

char c = 'c';

1)通过类方法实现

NSNumber *IntNumber = [NSNumber numberWithInt:num];

NSNumber *FloatNumber = [NSNumber numberWithFloat:num2];

2)通过实例方法实现

NSNumber *isBoolNumber = [[NSNumber alloc] initWithBool:isBool];

NSNumber *CNumber = [[NSNumber alloc] initWithChar:c];

打印输出格式:

NSLog(@"IntNumber : %@",IntNumber);

NSLog(@"isBoolNumber : %@",isBoolNumber);

(2)可以把NSNumber对象变成基本数据类型;

int d = [IntNumber intValue];

float f1 = [FloatNumber floatValue];

char c1 = [CNumber charValue];

2、NSString类:处理字符串的类

(1)创建字符串

创建字符串常量: NSString *str = @"good!";

创建字符串变量:

1)空字符串创建

NSString *str2 = [[NSString alloc] init];

str2 = @"test";

NSString *str3 = [NSString string];

NSLog(@"str = %@,str2=%@,str3=%@",str,str2,str3);

2)快速创建字符串

NSString *str4 = [[NSString alloc] initWithString:@"pk"];

NSString *str5 = [NSString stringWithString:@"pk"];

NSLog(@"str4 = %@,str5=%@",str4,str5);

3)格式化创建字符串

NSString *str6 = [[NSString alloc] initWithFormat:@"%d_%d_%d_%d_%d_%@",1,2,3,4,5,str4];

NSLog(@"str6=%@",str6);

NSString *str7=[NSString stringWithFoemat:@"hello world,i'm %d years old!",9];

(2)基本操作:

1)判断字符串是否相等

if([str4 isEqualToString:str5]){

NSLog(@"字符串相等");

}

else{

NSLog(@"字符串不相等");

}

NSLog(@"%p,%p",str4,str5);

2)判断字符串是否同一个对象

if(str4 == str5){

NSLog(@"是同一个对象!");

}

3)基本数据类型

封装成字符串对象

str = [[NSString alloc] initWithFormat:@"%d",5];

字符串对象 ===> 基本数据类型

NSString *str7 = [NSString stringWithFormat:@"%d",56];

int num7 = [str7 intValue];

字符串转整型

NSLog(@"num7+1 =%d",num7+1);

字符串转换 ======>> 数组

NSArray *array = [str6 componentsSeparatedByString:@"_"];

NSLog(@"%@",array);

字符串截取

NSLog(@"substring to 2 :%@",[str6 substringToIndex:5]);

NSLog(@"substring to 2 :%@",[str6 substringFromIndex:5]);

截取某一个范围的字符串

NSRange rang;

rang.length = 4;

rang.location = 2;

截取的时候,包含起始位置

NSLog(@"substring to 2 :%@",[str6 substringWithRange:rang]);

字符串查找(查找子串,并返回范围)

NSString *str8=@"hello01.txt";

NSRange rang2 = [str8 rangeOfString:@"."];

if(rang2.location != NSNotFound){

NSLog(@"sub str location =%ld ,length=%ld",rang2.location,rang2.length);}

else{ NSLog(@"NSNotFound!!");

}

NSString----》NSData

NSData *pData = [@"hello world" dataUsingEncoding:NSUTF8StringEncoding];

3)可变长度的字符串:NSMutableString

NSMutableString *mutableStr = [NSMutableString stringWithString:@"爱大米"];

动态的插入内容

[mutableStr insertString:@"老鼠" atIndex:0];

NSLog(@"mubableStr:%@",mutableStr);

3、NSArray:用来存储对象的类,对象可以是任意类型的。

(1定义数组并且初始化

定义只有一个元素的数组:NSArray *array1 = [NSArray arrayWithObject:@"one"];

定义有多个元素的数组: NSArray *array2 = [NSArray arrayWithObjects:@"one",@"two",@"three",@"four",nil];

定义包含另一数组元素的数组: NSArray *array3 = [NSArray arrayWithArray:array2];

NSLog(@"array1 = %@, array2 = %@, array3 = %@",array1,array2,array3);

(2) 数组的操作

1)求长度

int len = [array2 count];

访问元素

NSString *arrayObject = [array3 objectAtIndex:3];

将数组元素连接成一个字符串

NSString *newStr = [array2 componentsJoinedByString:@"_"];

NSLog(@"array2 length:%d,index 3=%@,joinStr = %@",len,arrayObject,newStr);

2)可变数组的使用

---定义可变数组

NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"one", nil];

---在数组末尾添加元素

[mutableArray addObject:@"two"];

[mutableArray addObject:@"three"];

[mutableArray addObject:@"four"];

--在数组末尾添加一个数组

[mutableArray addObjectsFromArray:array2];

---计算可变数组长度

int length = [mutableArray count];

NSLog(@"mutableArray length=%d,countent:%@",length,mutableArray);

---- 移除数组最后一个元素

[mutableArray removeLastObject];

---- 移除指定位置的数据

[mutableArray removeObjectAtIndex:0];

length = [mutableArray count];

NSLog(@"***mutableArra length=%d,countent:%@",length,mutableArray);

数组的遍历方式:传统方式 高效方式

//----- 传统方式

for (int i=length-1; i>=0; i--)

{

NSLog(@"%d = %@",i,[mutableArray objectAtIndex:i]);

}

//----- 高效方式

for(NSString *str in mutableArray){

NSLog(@"obj =%@",str);

}

4、NSDictionary:关键字及其定义的集合在给定的关键字下存储一个值(key-value)。

(1)---- 初始化

初始化一组数组

Dictionary *dic1 = [NSDictionary dictionaryWithObject:numObj forKey:@"key1"];

初始化多组数据

NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"hello",@"key2",@"world",@"key3",@"csdn",@"key4", nil];

用一个字典初始化另外一个字典

NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic2];

打印输出

NSLog(@"dic1 : %@,dic2 : %@, dic3 : %@",dic1,dic2,dic3);

(2)--- 获取值

//获取长度

int len = [dic2 count];

NSLog(@"dic2 length = %d",len);

据key获取key所对应的value

NSLog(@"key3 value = %@",[dic2 objectForKey:@"key3"]);

可以获取所有的keys

NSArray *allkeys = [dic3 allKeys];

NSLog(@"NSarray allkey = %@",allkeys);

可以获取所有的values

NSArray *allvalues = [dic3 allValues];

NSLog(@"NSarray allvalues = %@",allvalues);

(3)-- 可变字典

---- 初始化

NSMutableDictionary *dic4 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"key4",@"two",@"key5", nil];

NSLog(@"dic4 : %@",dic4);

定义成空字典

NSMutableDictionary *dic5 = [NSMutableDictionary dictionary];

将字典dic2整体添加到dic4钟

[dic4 addEntriesFromDictionary:dic2];

NSLog(@"addEntriesFromDictionary dic2 : %@",dic4);

添加一个元素

[dic4 setValue:@"three" forKey:@"key6"];

NSLog(@"dic4 setValue : %@",dic4);

根据key获取value

NSLog(@"key6 = %@",[dic4 objectForKey:@"key6"]);

----- 字典的遍历

//1)一般的遍历

NSArray *keys4 = [dic4 allKeys];

for(int i=0;i<[dic4 count];

{ NSLog(@"dic4 key = %@,value=%@",[keys4 objectAtIndex:i],[dic4 objectForKey:[keys4 objectAtIndex:i]]);

}

//2)高效的for

for (NSString *key in dic4)

{

NSLog(@"dic4 key = %@ ,value = %@",key,[dic4 objectForKey:key]);

}

//3)使用枚举进行遍历

NSEnumerator *enum1 = [dic4 keyEnumerator];

//获取key,如果不为空,则进行偏移

id key = [enum1 nextObject];

while (key)

{ NSLog(@"key = %@ ,value = %@ ",key,[dic4 objectForKey:key]);

key = [enum1 nextObject];

}

5、NSSet

(1)--- 定义、初始化

NSSet *set = [[NSSet alloc] initWithObjects:@"one",@"one",@"two",nil];

//用数组定义NSSet;

NSArray *arrayset = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];

NSSet *set2 = [NSSet setWithArray:arrayset];

NSLog(@"set1 = %@,set2 = %@",set,set2);

(2)//访问方式

//----- 获取长度

int len = [set2 count;

NSString *s = [set2 anyObject];

NSLog(@"set2 length = %d,obj = %@",len,s);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: