您的位置:首页 > 移动开发 > Objective-C

Objective-c 的一些基础知识,NSString,NSNumber,NSArray 等

2013-10-12 08:12 489 查看
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])

{

@autoreleasepool {



//-------------------NSNumber------------------

/*

//基本类型====>>对象

int num =1;

float num2 = 4.5;

BOOL isBool = NO;

char c = 'c';

//封装成对象 ---------------类方法实现

NSNumber *IntNumber = [NSNumber numberWithInt:num];

NSNumber *FloatNumber = [NSNumber numberWithFloat:num2];



// ---------------实例方法实现

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



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



//打印



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

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





//对象======>>基本类型

int d = [IntNumber intValue];

float f1 = [FloatNumber floatValue];

char c1 = [CNumber charValue];



NSLog(@"");



*/



//------------------------NSString-------------------

/*

//字符串常量

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);



//判断字符串是否相等

if([str4 isEqualToString:str5]){

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

}else{



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

}



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

//判断字符串

if(str4 == str5){

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

}







//基本数据类型 ===> 字符串

//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!!");

}



//可变长度的字符串

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



//动态的插入内容

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



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

*/





//--------------------------------NSArray---------------------------

/*

//定义数组并且初始化

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);



//数组的访问

//求长度

int len = [array2 count];

//访问元素

NSString *arrayObject = [array3 objectAtIndex:3];

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

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

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





//可变数组的使用

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(@"***mutableArray 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);



}



*/







//--------------------------------NSDictionary-----------------------

//----- 初始化

/*

NSNumber *numObj = [NSNumber numberWithInt:100];

//初始化一组数组

// 值 key

NSDictionary *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);





//------ 获取值



//获取长度

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);



//----- 可变字典

//----- 初始化

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];i++){



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



}

NSLog(@"-----------------------");



//2)高效的for

for (NSString *key in dic4){

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

}



NSLog(@"-----------------------");

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

NSEnumerator *enum1 = [dic4 keyEnumerator];

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

id key = [enum1 nextObject];



while (key) {



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



key = [enum1 nextObject];

}



*/



//----------------------------------NSSet-------------------

//----- 定义、初始化

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);

//访问方式

//----- 获取长度

int len = [set2 count];



NSString *s = [set2 anyObject];



NSLog(@"set2 length = %d,obj = %@",len,s);



}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: