//将NSData转化为NSString
NSString* str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
//将NSString 转化为NSData
(NSString.h)
- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding;
//载一个字符串中删除一个字符或字符串
[_display deleteCharactersInRange:NSMakeRange(index_of_char_to_remove, 1)];
数学转换为字符串
NSString *returnStr;
returnStr = [[NSNumber numberWithInt:row] stringValue];
NSString
*******************************************************************************************/
//一、NSString
/*----------------创建字符串的方法----------------*/
//1、创建常量字符串。
NSString *astring = @"This is a String!";
//2、创建空字符串,给予赋值。
NSString *astring = [[NSString alloc] init];
astring = @"This is a String!";
[astring release];
NSLog(@"astring:%@",astring);
//3、在以上方法中,提升速度:initWithString方法
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
[astring release];
//4、用标准c创建字符串:initWithCString方法
char *Cstring = "This is a String!";
NSString *astring = [[NSString alloc] initWithCString:Cstring];
NSLog(@"astring:%@",astring);
[astring release];
//5、创建格式化字符串:占位符(由一个%加一个字符组成)
int i = 1;
int j = 2;
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];
NSLog(@"astring:%@",astring);
[astring release];
//6、创建临时字符串
NSString *astring;
astring = [NSString stringWithCString:"This is a temporary string"];
NSLog(@"astring:%@",astring);
/*----------------从文件读取字符串:initWithContentsOfFile方法----------------*/
NSString *path = @"astring.text";
NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"astring:%@",astring);
[astring release];
/*----------------写字符串到文件:writeToFile方法----------------*/
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
NSString *path = @"astring.text";
[astring writeToFile: path atomically: YES];
[astring release];
/*----------------比较两个字符串----------------*/
//用C比较:strcmp函数
char string1[] = "string!";
char string2[] = "string!";
if(strcmp(string1, string2) = = 0)
{
NSLog(@"1");
}
//isEqualToString方法
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 isEqualToString:astring02];
NSLog(@"result:%d",result);
//compare方法(comparer返回的三种值)
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedSame;
NSLog(@"result:%d",result);
//NSOrderedSame判断两者内容是否相同
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"this is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedAscending;
NSLog(@"result:%d",result);
//NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedDescending;
NSLog(@"result:%d",result);
//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)
//不考虑大小写比较字符串1
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;
NSLog(@"result:%d",result);
//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)
//不考虑大小写比较字符串2
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02
options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame;
NSLog(@"result:%d",result);
//NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。
/*----------------改变字符串的大小写----------------*/
NSString *string1 = @"A String";
NSString *string2 = @"String";
NSLog(@"string1:%@",[string1 uppercaseString]);//大写
NSLog(@"string2:%@",[string2 lowercaseString]);//小写
NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小
/*----------------在串中搜索子串----------------*/
NSString *string1 = @"This is a string";
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
int location = range.location;
int leight = range.length;
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];
NSLog(@"astring:%@",astring);
[astring release];
/*----------------抽取子串 ----------------*/
//-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:3];
NSLog(@"string2:%@",string2);
//-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringFromIndex:3];
NSLog(@"string2:%@",string2);
//-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];
NSLog(@"string2:%@",string2);
//扩展路径
NSString *Path = @"~/NSData.txt";
NSString *absolutePath = [Path stringByExpandingTildeInPath];
NSLog(@"absolutePath:%@",absolutePath);
NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
//文件扩展名
NSString *Path = @"~/NSData.txt";
NSLog(@"Extension:%@",[Path pathExtension]);
/*******************************************************************************************
NSMutableString
*******************************************************************************************/
/*---------------给字符串分配容量----------------*/
//stringWithCapacity:
NSMutableString *String;
String = [NSMutableString stringWithCapacity:40];
/*---------------在已有字符串后面添加字符----------------*/
//appendString: and appendFormat:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
//[String1 appendString:@", I will be adding some character"];
[String1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]];
NSLog(@"String1:%@",String1);
*/
/*--------在已有字符串中按照所给出范围和长度删除字符------*/
/*
//deleteCharactersInRange:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 deleteCharactersInRange:NSMakeRange(0, 5)];
NSLog(@"String1:%@",String1);
/*--------在已有字符串后面在所指定的位置中插入给出的字符串------*/
//-insertString: atIndex:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];
NSLog(@"String1:%@",String1);
/*--------将已有的空符串换成其它的字符串------*/
//-setString:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 setString:@"Hello Word!"];
NSLog(@"String1:%@",String1);
/*--------按照所给出的范围,和字符串替换的原有的字符------*/
//-setString:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];
NSLog(@"String1:%@",String1);
/*-------------判断字符串内是否还包含别的字符串(前缀,后缀)-------------*/
//01:检查字符串是否以另一个字符串开头- (BOOL) hasPrefix: (NSString *) aString;
NSString *String1 = @"NSStringInformation.txt";
[String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
[String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
//02:查找字符串某处是否包含其它字符串 - (NSRange) rangeOfString: (NSString *) aString,这一点前面在串中搜索子串用到过;
/*******************************************************************************************
NSArray
*******************************************************************************************/
/*---------------------------创建数组------------------------------*/
//NSArray *array = [[NSArray alloc] initWithObjects:
@"One",@"Two",@"Three",@"Four",nil];
self.dataArray = array;
[array release];
//- (unsigned) Count;数组所包含对象个数;
NSLog(@"self.dataArray cound:%d",[self.dataArray count]);
//- (id) objectAtIndex: (unsigned int) index;获取指定索引处的对象;
NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);
/*--------------------------从一个数组拷贝数据到另一数组(可变数级)----------------------------*/
//arrayWithArray:
//NSArray *array1 = [[NSArray alloc] init];
NSMutableArray *MutableArray = [[NSMutableArray alloc] init];
NSArray *array = [NSArray arrayWithObjects:
@"a",@"b",@"c",nil];
NSLog(@"array:%@",array);
MutableArray = [NSMutableArray arrayWithArray:array];
NSLog(@"MutableArray:%@",MutableArray);
array1 = [NSArray arrayWithArray:array];
NSLog(@"array1:%@",array1);
//Copy
//id obj;
NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSArray *oldArray = [NSArray arrayWithObjects:
@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
NSLog(@"oldArray:%@",oldArray);
for(int i &#61; 0; i <[oldArray count]; i&#43;&#43;)
{
obj &#61; [[oldArray objectAtIndex:i] copy];
[newArray addObject: obj];
}
//
NSLog(&#64;"newArray:%&#64;", newArray);
[newArray release];
//快速枚举
//NSMutableArray *newArray &#61; [[NSMutableArray alloc] init];
NSArray *oldArray &#61; [NSArray arrayWithObjects:
&#64;"a",&#64;"b",&#64;"c",&#64;"d",&#64;"e",&#64;"f",&#64;"g",&#64;"h",nil];
NSLog(&#64;"oldArray:%&#64;",oldArray);
for(id obj in oldArray)
{
[newArray addObject: obj];
}
//
NSLog(&#64;"newArray:%&#64;", newArray);
[newArray release];
//Deep copy
//NSMutableArray *newArray &#61; [[NSMutableArray alloc] init];
NSArray *oldArray &#61; [NSArray arrayWithObjects:
&#64;"a",&#64;"b",&#64;"c",&#64;"d",&#64;"e",&#64;"f",&#64;"g",&#64;"h",nil];
NSLog(&#64;"oldArray:%&#64;",oldArray);
newArray &#61; (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);
NSLog(&#64;"newArray:%&#64;", newArray);
[newArray release];
//Copy and sort
//NSMutableArray *newArray &#61; [[NSMutableArray alloc] init];
NSArray *oldArray &#61; [NSArray arrayWithObjects:
&#64;"b",&#64;"a",&#64;"e",&#64;"d",&#64;"c",&#64;"f",&#64;"h",&#64;"g",nil];
NSLog(&#64;"oldArray:%&#64;",oldArray);
NSEnumerator *enumerator;
enumerator &#61; [oldArray objectEnumerator];
id obj;
while(obj &#61; [enumerator nextObject])
{
[newArray addObject: obj];
}
[newArray sortUsingSelector:&#64;selector(compare:)];
NSLog(&#64;"newArray:%&#64;", newArray);
[newArray release];
/*---------------------------切分数组------------------------------*/
//从字符串分割到数组&#xff0d; componentsSeparatedByString:
NSString *string &#61; [[NSString alloc] initWithString:&#64;"One,Two,Three,Four"];
NSLog(&#64;"string:%&#64;",string);
NSArray *array &#61; [string componentsSeparatedByString:&#64;","];
NSLog(&#64;"array:%&#64;",array);
[string release];
//从数组合并元素到字符串- componentsJoinedByString:
NSArray *array &#61; [[NSArray alloc] initWithObjects:&#64;"One",&#64;"Two",&#64;"Three",&#64;"Four",nil];
NSString *string &#61; [array componentsJoinedByString:&#64;","];
NSLog(&#64;"string:%&#64;",string);
/*******************************************************************************************
NSMutableArray
*******************************************************************************************/
/*---------------给数组分配容量----------------*/
//NSArray *array;
array &#61; [NSMutableArray arrayWithCapacity:20];
/*--------------在数组末尾添加对象----------------*/
//- (void) addObject: (id) anObject;
//NSMutableArray *array &#61; [NSMutableArray arrayWithObjects:
&#64;"One",&#64;"Two",&#64;"Three",nil];
[array addObject:&#64;"Four"];
NSLog(&#64;"array:%&#64;",array);
/*--------------删除数组中指定索引处对象----------------*/
//-(void) removeObjectAtIndex: (unsigned) index;
//NSMutableArray *array &#61; [NSMutableArray arrayWithObjects:
&#64;"One",&#64;"Two",&#64;"Three",nil];
[array removeObjectAtIndex:1];
NSLog(&#64;"array:%&#64;",array);
/*-------------数组枚举---------------*/
//- (NSEnumerator *)objectEnumerator;从前向后
//NSMutableArray *array &#61; [NSMutableArray arrayWithObjects:
&#64;"One",&#64;"Two",&#64;"Three",nil];
NSEnumerator *enumerator;
enumerator &#61; [array objectEnumerator];
id thingie;
while (thingie &#61; [enumerator nextObject]) {
NSLog(&#64;"thingie:%&#64;",thingie);
}
//- (NSEnumerator *)reverseObjectEnumerator;从后向前
//NSMutableArray *array &#61; [NSMutableArray arrayWithObjects:
&#64;"One",&#64;"Two",&#64;"Three",nil];
NSEnumerator *enumerator;
enumerator &#61; [array reverseObjectEnumerator];
id object;
while (object &#61; [enumerator nextObject]) {
NSLog(&#64;"object:%&#64;",object);
}
//快速枚举
//NSMutableArray *array &#61; [NSMutableArray arrayWithObjects:
&#64;"One",&#64;"Two",&#64;"Three",nil];
for(NSString *string in array)
{
NSLog(&#64;"string:%&#64;",string);
}
/*******************************************************************************************
NSDictionary
*******************************************************************************************/
/*------------------------------------创建字典------------------------------------*/
//- (id) initWithObjectsAndKeys;
//NSDictionary *dictionary &#61; [[NSDictionary alloc] initWithObjectsAndKeys:&#64;"One",&#64;"1",&#64;"Two",&#64;"2",&#64;"Three",&#64;"3",nil];
NSString *string &#61; [dictionary objectForKey:&#64;"One"];
NSLog(&#64;"string:%&#64;",string);
NSLog(&#64;"dictionary:%&#64;",dictionary);
[dictionary release];
/*******************************************************************************************
NSMutableDictionary
*******************************************************************************************/
/*------------------------------------创建可变字典------------------------------------*/
//创建
NSMutableDictionary *dictionary &#61; [NSMutableDictionary dictionary];
//添加字典
[dictionary setObject:&#64;"One" forKey:&#64;"1"];
[dictionary setObject:&#64;"Two" forKey:&#64;"2"];
[dictionary setObject:&#64;"Three" forKey:&#64;"3"];
[dictionary setObject:&#64;"Four" forKey:&#64;"4"];
NSLog(&#64;"dictionary:%&#64;",dictionary);
//删除指定的字典
[dictionary removeObjectForKey:&#64;"3"];
NSLog(&#64;"dictionary:%&#64;",dictionary);
/*******************************************************************************************
NSValue&#xff08;对任何对象进行包装&#xff09;
*******************************************************************************************/
/*--------------------------------将NSRect放入NSArray中------------------------------------*/
//将NSRect放入NSArray中
NSMutableArray *array &#61; [[NSMutableArray alloc] init];
NSValue *value;
CGRect rect &#61; CGRectMake(0, 0, 320, 480);
value &#61; [NSValue valueWithBytes:&rect objCType:&#64;encode(CGRect)];
[array addObject:value];
NSLog(&#64;"array:%&#64;",array);
//从Array中提取
value &#61; [array objectAtIndex:0];
[value getValue:&rect];
NSLog(&#64;"value:%&#64;",value);
/*******************************************************************************************
从目录搜索扩展名为jpg的文件
*******************************************************************************************/
//NSFileManager *fileManager &#61; [NSFileManager defaultManager];
NSString *home;
home &#61; &#64;"../Users/";
NSDirectoryEnumerator *direnum;
direnum &#61; [fileManager enumeratorAtPath: home];
NSMutableArray *files &#61; [[NSMutableArray alloc] init];
//枚举
NSString *filename;
while (filename &#61; [direnum nextObject]) {
if([[filename pathExtension] hasSuffix:&#64;"jpg"]){
[files addObject:filename];
}
}
//快速枚举
//for(NSString *filename in direnum)
//{
// if([[filename pathExtension] isEqualToString:&#64;"jpg"]){
// [files addObject:filename];
// }
//}
NSLog(&#64;"files:%&#64;",files);
//枚举
NSEnumerator *filenum;
filenum &#61; [files objectEnumerator];
while (filename &#61; [filenum nextObject]) {
NSLog(&#64;"filename:%&#64;",filename);
}
//快速枚举
//for(id object in files)
//{
// NSLog(&#64;"object:%&#64;",object);
//}
#import
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool &#61; [[NSAutoreleasePool alloc] init];
//创建字符串
NSString *height;
/**类方法&#xff1a;
&#43;(id) stringWithFormat: (NSString *) format,...
通过格式字符串和参数来创建NSString
省略号&#xff08;。。。&#xff09;&#xff1a;可以接受多个以逗号分割的参数。
这声明方法时添加加号&#xff08;&#xff0b;&#xff09;&#xff0c;那么这个方法为类方法&#xff0c;不需要创建实例就可以调用&#xff0c;通常用于创建心的实例&#xff0c;我们称用来创建新对象的类方法为工厂方法。
&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;
objective&#xff0d;c运行时生成一个类的时候&#xff0c;它会创建一个代表该类的类对象。类对象包含了指向超类的指针&#xff0c;类名&#xff0c;和指向类方法列表的指针。类对象还包含一个long型数据&#xff0c;为新创建的类实例对象指定大小&#xff08;以字节为单位&#xff09;
类方法可以用来访问全局数据。
实例方法要用前导减号&#xff08;&#xff0d;&#xff09;来开始声明
*/
height&#61;[NSString stringWithFormat:&#64;"you heigh is %d feet,%d inches",5,11];
NSLog(height);
//length 返回字符串中字符的个数。&#xff0d;&#xff08;unsigned int) length;
if([height length]>5){
NSLog(&#64;"height length ------");
}
//字符串比较
/**
isEqualToString :可以用来比较接收方和当作参数传递来的字符串的内容是否相同&#xff0c;返回yes和no
*/
NSString *thing1&#61;&#64;"hello";
NSString *thing2&#61;[[NSString alloc] initWithString:&#64;"hello"];
if([thing1 isEqualToString:thing2]){
NSLog(&#64;"they are same");
}
/**
&#61;&#61;:只判断指针数值&#xff0c;而不是它们所指向的内容
*/
if(thing1&#61;&#61;thing2){
NSLog(&#64;"&#61;&#61; same");
}
/*
compare:比较两个字符串。区分大小写
compare将接收对象和传递来的字符串逐个字符的进行比较&#xff0c;它返回一个NSComparisonResult(枚举类型)来显示结果。
typedef enum _NSComparisonResult{
NSOrderedAscending&#61;-1;
NSOrderedsame;
NSOrderedDescending;
} NSComparisonResult;
*/
[thing1 compare:thing2];
if(NSOrderedSame&#61;&#61;[thing1 compare:thing2]){
NSLog(&#64;"compare same");
}
//compare&#xff1a;options:
/***
-(NSComparisonResult) compare:(NSString *) string
options:(unsinged) mask;
options 是一个位掩码&#xff0c;可以使用|添加选项标记
选项&#xff1a;
NSCaseInsensitiveSearch:不区分大小写字符
NSLiteralSearch:进行完全比较&#xff0c;区分大小写
NSNumbericSearch:比较字符串的字符个数&#xff0c;而不是字符值
*/
if([thing1 compare:thing2 options:NSCaseInsensitiveSearch|
NSNumericSearch]&#61;&#61;NSOrderedSame){
NSLog(&#64;"they match");
}
/**
以某个字符串开始或结尾
-(BOOL) hasPrefix:(NSString *) aString;
-(BOOL) hasSuffix:(NSString *) aString;
*/
NSString *fileName&#61;&#64;"aabbbcc";
if([fileName hasPrefix:&#64;"aa"]){
NSLog(&#64;"begin with aa");
}
if([fileName hasSuffix:&#64;"cc"]){
NSLog(&#64;"end with cc");
}
//NSMutableString 可变字符串
//SString 是不可变的&#xff0c;一旦NSString 被创建了&#xff0c;我们就不能改变它。
//&#43;(id) stringWithCapacity:(unsinged) capacity; capacity:是给NSMutableString的一个建议&#xff0c;字符串的大小并不局限于所提供的大小&#xff0c;这个容量仅是个最优值。
NSMutableString *str&#61;[NSMutableString stringWithCapacity:40];
[str appendFormat:&#64;"sdfsdf%d",5];
[str appendString:&#64;"ssssssss"];
NSLog(str);
//删除字符串
//-(void) deleteCharactersInRange:(NSRange) range;
NSMutableString *ms;
ms&#61;[NSMutableString stringWithCapacity:50];
[ms appendString:&#64;"aabbccdd"];
NSRange range;
range&#61;[ms rangeOfString:&#64;"cc"];
[ms deleteCharactersInRange:range];
NSLog(ms);
//与实例方法一样&#xff0c;继承对类方法也同样适用
//------------------集合&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;
//NSArray ,NSDictionary
/**
NSArray 是一个cocoa类&#xff0c;用来存储对象的有序列表。
NSArray有两个限制&#xff1a;1&#xff0c;它只能存储objective&#xff0d;c的对象&#xff0c;而不能存储c语言中的基本数据类型如int&#xff0c;float&#xff0c;enum&#xff0c;struct&#xff0c;或者nsarray中的随机指针。2&#xff0c;不能这nsarray中存储nil
类方法&#xff1a;
arrayWithObjects:创建一个新的nsarray。发送一个以逗号分割的对象列表&#xff0c;这列表结尾添加nil代表列表结束&#xff0c;&#xff08;这就是不能这nsarray中存储nil的原因&#xff09;
*/
NSArray *array&#61;[NSArray arrayWithObjects:&#64;"aa",&#64;"bb",&#64;"cc",nil];
//-(unsigned) count; 取得包含对象的个数
//-(id) objectAtIndex:(unsigned int) index; 取得索引位置的对象
int i;
for (i&#61;0; i<[array count]; i&#43;&#43;) {
NSLog(&#64;"index %d has %&#64;",i,[array objectAtIndex:i]);
}
//------------切分数组
//-componentsSeparatedByString:
NSString *ns&#61;&#64;"sdf,dsfs,dfd,fdf,df,dd";
NSArray *comArr&#61;[ns componentsSeparatedByString:&#64;","];
for(int i&#61;0;i<[comArr count];i&#43;&#43;){
NSLog(&#64;"componentsSeparatedByString&#61;&#61;&#61;%&#64;",[comArr objectAtIndex:i]);
}
//componentJoinedByString: 合并nsarray中的元素来创建字符串
NSString *joinedStr&#61;[comArr componentsJoinedByString:&#64;"-->"];
NSLog(&#64;"joined---&#61; %&#64;",joinedStr);
//可变数组
NSMutableArray *mutableArr&#61;[NSMutableArray arrayWithCapacity:40];
[mutableArr addObject:&#64;"aa"];
[mutableArr addObject:&#64;"bb"];
[mutableArr addObject:&#64;"cc"];
[mutableArr addObject:&#64;"dd"];
for(int i&#61;0;i<[mutableArr count];i&#43;&#43;){
NSLog(&#64;"mutableArr&#61;&#61;%&#64;",[mutableArr objectAtIndex:i]);
}
//----- -(void) removeObjectAtIndex:(unsinged) index; 删除指定索引的对象,
//删除一个对象之后&#xff0c;数组中并没有留下漏洞&#xff0c;被删除对象后面的数组元素的哦被前移来填补空缺
[mutableArr removeObjectAtIndex:2];
for(int i&#61;0;i<[mutableArr count];i&#43;&#43;){
NSLog(&#64;"removeObjectAtIndex &#61;&#61; %&#64;",[mutableArr objectAtIndex:i]);
}
//枚举
//NSEnumerator ,它是cocoa用来描述这种集合迭代运输的方法
//-(NSEnumerator *) objectEnumerator;
NSEnumerator *enumerator&#61;[mutableArr objectEnumerator];
id thingie;
while(thingie&#61;[enumerator nextObject]){
NSLog(&#64;"i found %&#64;",thingie);
}
//快速枚举
for(NSString *string in mutableArr){
NSLog(&#64;"for in &#61;&#61; %&#64;",string);
}
//NSDictionary 字典
/*
NSDictionary 是在给定的关键字&#xff08;通常是一个NSString字符串&#xff09;下存储一个数值&#xff08;可以是任何类型的对象&#xff09;。然后你可以用这个关键字来查找相应的数值。
NSDictionary 是键查询的优化存储方式。它可以立即找出要查询的数据&#xff0c;而不需要遍历整个数组进行查找。
&#xff0b;&#xff08;id) dictionaryWithObjectAndKeys:(id) firstObject,....;
该方法接收对象和关键字交替出现的系列&#xff0c;以nil值作为终止符号。
**/
NSDictionary *dic&#61;[NSDictionary dictionaryWithObjectsAndKeys:&#64;"aaa",&#64;"a",&#64;"bbb",&#64;"b",nil];
NSString *dicStr&#61;[dic objectForKey:&#64;"a"];
if([dicStr isEqualToString:&#64;"aaa"]){
NSLog(&#64;"------------00000000000000000");
}
//可变字典
NSMutableDictionary *mutableDic&#61;[NSMutableDictionary dictionaryWithCapacity:50];
[mutableDic setObject:&#64;"1111" forKey:&#64;"1"];
[mutableDic setObject:&#64;"222" forKey:&#64;"2"];
//删除 -(void) removeObjectForKe:(id) key;
[mutableDic removeObjectForKey:&#64;"2"];
NSArray *keyArr&#61;[mutableDic allKeys];
for(NSString *str in keyArr){
NSLog(&#64;"key&#61;&#61; %&#64;",str);
NSLog(&#64;"value&#61;&#61; %&#64;",[mutableDic objectForKey:str]);
}
//各种数值&#xff0c;NSNumber NSValue
/*
cocoa 提供了NSNumber类来包装基本数据类型
&#43;(NSNumber *) numberWithChar:(char) value;
&#43;(NSNumber *) numberWithInt:(int) value;
&#43;(NSNumber *) numberWithFloat:(float) value;
&#43;(NSNumber *) numberWthiBool:(BOOL) value;
-(char) charValue;
-(int) intVlaue;
-(float) floatValue;
-(BOOL) boolValue;
-(NSString *) stringValue;
**/
NSNumber *number;
number&#61;[NSNumber numberWithInt:3];
[mutableDic setObject:number forKey:&#64;"int"];
int num&#61;[[mutableDic objectForKey:&#64;"int"] intValue];
NSLog(&#64;"int object value&#61;&#61; %d",num);
//NSValue .NSNumber实际上是NSValue的子类&#xff0c;NSValue可以包装任意值
/**
&#43;(NSValue *) valueWithBytes:(const void *) value objCType:(const char *) type;
传递的参数是你想要包装的数值的地址&#xff0c;通常&#xff0c;得到的是你想要存储的变量的地址&#xff08;在c语言里适用操作符 & &#xff09;&#xff0c;你也可以提供一个描述这个数据类型的字符串&#xff0c;通常用来说明struct中实体的类型和大小。你不用自己写代码
来生成这个字符串&#xff0c;&#64;encode编译器指令可以接受数据类型的名称并为你生成合适的字符串
*/
NSRect rect&#61; NSMakeRect(1, 2, 30, 40);
NSValue *value;
value&#61;[NSValue valueWithBytes:&rect objCType:&#64;encode(NSRect)];
NSMutableArray *mr&#61;[NSMutableArray arrayWithCapacity:50];
[mr addObject:value];
//getValue 提取数据
/**
-(void) getValue:(void *) value; 要传递的是存储这个数值的变量的地址
*/
/***
value&#61;[mr objectAtIndex:0];
NSRect r;
NSLog(&#64;"00000 &#61;&#61;&#61;%&#64;",r);
[value getValue:&r];
NSLog(&#64;"111&#61;&#61; %&#64;",r);
*/
/**
&#43;(NSValue *) valueWithPoint:(NSPoint) point;
&#43;(NSValue *) valueWithSize:(NSSize) size;
&#43;(NSValue *) valueWithRect:(NSRect) rect;
-(NSPoint) pointValue;
-(NSSize) sizeValue;
-(NSRect) rectValue;
*/
//NSNull
/*
*&#43;(NSNull *) null;
*/
[mutableDic setObject:[NSNull null] forKey:&#64;"fax"];
id fax;
fax&#61;[mutableDic objectForKey:&#64;"fax"];
if(fax&#61;&#61;[NSNull null]){
NSLog(&#64;"pppppppppppppppppp");
}
[pool drain];
return 0;
}