NSArra、NSMutableArray和NSDictionary、NSMutableDictionary和NSSting、NSMutableString的应用与总结

*****************************************************************************

NSArray

*******************************************************************************/

/*---------------------------创建数组------------------------------*/

NSArray *array = [[NSArray alloc] initWithObjects: @"One",@"Two",@"Three",@"Four",nil];
NSArray * array1 = [[NSArray alloc]initWithArray:array];
NSArray * array2 = [NSArray arrayWithArray:array];
NSArray *array3 = [[NSArray arrayWithObjects:
@"One",@"Two",@"Three",@"Four",nil];

[array release];
[array1 release];

//- (NSUInteger) Count;数组所包含对象(元素)个数;
NSLog(@"count:%d",[array count]);
//- (id) objectAtIndex: (NSUInteger) index;获取指定索引处的对象(元素);
NSLog(@"object :%@",[array objectAtIndex:2]);

/*查找:根据元素找到对应的索引*/

NSArray*array=[[NSArrayalloc]initWithObjects:@"one",@"two",@"three",@"one
", nil];
//返回找到的第一个的索引, 一切操作不要越界
NSUInteger index = [array indexOfObject:@"one"];

//在指定范围内查找
index = [array indexOfObject:@"one" inRange:NSMakeRange(1, 3)];
if (index != NSNotFound) {//找不到返回 NSNotFound

NSLog(@"index = %ld",index);
}

/*数组的抽取*/
NSArray * array1 = [array objectsAtIndexes:[NSIndexSet

indexSetWithIndexesInRange:NSMakeRange(1, 3)]];
//NSIndexSet 数字集合类
//[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)];创建一个数字
集合对象//该集合成员是数字 1,2,3;

/*枚举器*/

//创建一个正序枚举器
NSEnumerator * enume1 = [array objectEnumerator];
//枚举器是读数据的

while (obj = [enume1 nextObject]) {
NSLog(@"%@",obj);

}

//快速枚举(正序)

NSArray *array = [NSArray arrayWithObjects: @"a",@"b",@"c",@"d",nil];

for(id obj in array)
{

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

}

//创建一个逆序枚举器
NSEnumerator * enume2 = [array reverseObjectEnumerator];
while (obj = [enume2 nextObject]) {

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

/*---------------------------字符串分割到数组------------------------------*/

NSString * str = @" Yes,I am a good man ";
//以字符串整体作为分割条件
NSArray * array = [str componentsSeparatedByString:@" "];//以@” ” 作为分割
条件
NSArray*array1 = [str componentsSeparatedByCharactersInSet:[NSCharacterSet
characterSetWithCharactersInString:@" ,"]];//以字符作为分割条件
//NSCharacterSet 字符集合
//[NSCharacterSet characterSetWithCharactersInString:@" ,"];把字符串@" ,"转
化为一个字符集合
//这个集合的成员就是字符‘ ‘和字符‘,‘;
//返回值 是 NSArray 不要写成 NSMuatbaleArray

/*---------------------------数组元素拼接成字符串------------------------------*/

NSString * ptr = [array componentsJoinedByString:@"###"];//把数组元素拼接
成字符串
NSLog(@”ptr = %@”,ptr);

/******************************************************************************
NSMutableArray

*******************************************************************************/

/*创建一个可变数组(继承于 NSArray)*/

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:
@"One",@"Two",@"Three",@"Four",nil];
NSMutableArray * array1 = [[NSMutableArray alloc]initWithArray:array];
NSMutableArray * array2 = [NSMutableArray arrayWithArray:array];
NSMutableArray *array3 = [[NSMutableArray arrayWithObjects:
@"One",@"Two",@"Three",@"Four",nil];

[array release];
[array1 release];

//把不可变转化为一个新的可变数组
NSArray *array = [NSArray arrayWithObjects: @"a",@"b",@"c",@"d",nil];
NSMutableArray * array1 = [NSMutableArray arrayWithArray:array];

//增 add (insert)
[array addObject:@"four"];//在最后增加
[array insertObject:@"iOS" atIndex:1];//在指定索引插入一个元素

//删除(remove)
[array removeObjectAtIndex:1];//根据索引删除元素
[array removeObject:@"one"];//删除数组元素:有几个删几个
[array removeObject:@"one" inRange:NSMakeRange(0, 2)]; //在指定范围内删

//替换(replace):修改
[array replaceObjectAtIndex:0 withObject:@"qianfeng"];

//交换(exchange)
[array exchangeObjectAtIndex:1 withObjectAtIndex:2];

/*可变数组排序*/

//创建一个空的可变数组
//(首先设计一个 Dog 类)
NSMutableArray * array = [[NSMutableArray alloc]init];
while (i++ < 5) {

Dog * dog = [[Dog alloc]initWithAge:arc4random()%10];
[array addObject:dog];
}

[array sortUsingSelector:@selector(youngThanAge:)];

//sortUsingSelector 这是一个排序方法;已经实现了, //但是需要我们提供一个准则(就是一个函数)这个准则是(升序)左边 大于 右边 进行交换 或者是(降序)左边小于右边进行交换 //数组的元素是哪个类?那么这个准则就写在哪个类中
//这就是一个准则 左边 大于 右边 进行交换
//升序
-(BOOL)olderThanAge:(Dog *)_dog
{

return [self age] > [_dog age];

}

/*************************************************************************

NSDictionary

****************************************************************************/

//内容可以是任意的对象指针 //内容是一些键值对 key à?value //必须成对出现 一个 key 对应一个 value //key 是唯一的 不能出现多个相同的 key

1.创建一个不可变字典(1)
NSDictionary*dict=[[NSDictionary
alloc]initWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"three",@"4",
@"four",@"5",@"five", nil];//
创建一个不可变字典(2)
NSDictionary * dict1 = [NSDictionary dictionaryWithDictionary:dict];
创建一个不可变字典(3)
NSArray * values =[[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4", nil];
NSArray*keys=[[NSArray
alloc]initWithObjects:@"one",@"two",@"three",@"four", nil];
NSDictionary* dict2 = [[NSDictionary alloc]initWithObjects:values forKeys:keys];

//键值对的个数
NSLog(@"count = %ld",[dict2 count]);

//查找 通过 key 找到对应值
NSLog(@"%@",[dict objectForKey:@"four"]);
词典类的存在就是为了解决在大量数据中查找方便,因为它是通过 key 直接找到
value 所以速度很快,避免一个个的遍历寻找造成的效率低下,善用字典类会帮
你的程序提速。

//创建 key 的枚举器
NSEnumerator * keyenumer = [dict keyEnumerator];
while (obj = [keyenumer nextObject]) {

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

//快速枚举枚举的是 key
for (id od in dict) {

NSLog(@"od = %@",od);
}

/*******************************************************************************
NSMutableDictionary

*******************************************************************************/
NSMutableDictionary 是NSDictionary的子类,所以继承了NSDictionary的方法。

//创建可变字典

NSMutableDictionary*dict=[[NSMutableDictionary
alloc]initWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"three",@"4",
@"four", nil];
NSMutableDictionary*dict1 =[NSMutableDictionary dictionaryWithCapacity:10] :
创建一个可变词典初始指定它的长度为 10.,动态的添加数据如果超过 10 这个
词典长度会自动增加,所以不用担心数组越界。

//增加 删除 修改
[dict setObject:@"5" forKey:@"five"];//字典中不存在@“five”key 那么就是增
加键值对
[dict setObject:@"7" forKey:@"one"];//字典中存在@“one”key 那么就是修改@
“one”对应的值
[dict removeObjectForKey:@"one"];//删除键值对
- (void)removeAllObjects;//删除所有键值对
- (void)removeObjectsForKeys:(NSArray *)keyArray;//删除keys
数组对应的键值对
- (void)setDictionary:(NSDictionary *)otherDictionary;//修
改字典

!

/*******************************************************************************!

????NSString!

*******************************************************************************

NSString!*[email protected]"Welcome!to!1000phone";

//2???????????????? initWithString:
!
NSString!*astring!=![[NSString!alloc]!initWithString:@"!I!love!iOS!"];! ! ! ! ! !
NSLog(@"astring:%@",astring);

[astring!release];! ! ! ! ! !
!
//3???????? c ??????????: initWithCString:!encoding:????! ! ! ! ! !
!
const!char!*cString!=!"I!love!iphone";! ! ! ! ! !
NSString * aString = [[NSString alloc]initWithCString:cString
encoding:NSUTF8StringEncoding];! ! ! !
NSLog(@"astring:%@",aString);! ! ! ! ! !
[aString!release];! ! !
!!!!
???????? initWithUTF8String:????????????????????!
const!char!*p!=!"!Welcome!to!Beijing!";!
NSString!*string!=![[NSString!alloc]initWithUTF8String:p];!
!
//4??????????????????:??????????????%????????????????! ! ! ! ! !
!!!!!
int! ! age!=!23;!
NSString!*astring!=![[NSString!alloc]!initWithFormat:@”I!am!%d”,age]];! ! ! ! ! !
NSLog(@"astring:%@",astring);! ! ! ! ! !
[astring!release];! ! ! ! ! !
!
//5????????????????????????????!
!
NSString!*!str1!=![NSString!stringWithString:@"I!love!programming!"];!
NSString!*!str2!=![NSString!stringWithUTF8String:"!I!love!programming!"];!
NSString!*!str3!=![NSString!stringWithFormat:@"%@",@"!I!love!programming!"];!
!
//6.! ????????????????:initWithContentsOfFile:! ! ! ! ! ! ! ! !
!

NSString!*[email protected]"!/Users/qianfeng/Desktop/StringAPI.pdf";! ! //????????! ! ! !
NSString!*astring!=![[NSString!alloc]!initWithContentsOfFile:path];! ! ! ! ! !
NSLog(@"astring:%@",astring);! ! ! ! ! !
[astring!release];! ! ! ! ! !

!

!

/*++++++++++++++++??????????????++++++++++++++++*/!

//??C????:strcmp????! ! ! ! ! !
!
char!string1[]!=!"string!";! ! ! ! ! !
char!string2[]!=!"string!";! ! ! ! ! !
if(strcmp(string1,!string2)!=!=!0)! ! ! ! ! !
{! ! ! ! ! !

! ! ! ! NSLog(@"1");! ! ! ! ! !
}! ! ! ! ! !
!
//1.! ! isEqualToString ????! ! ??????????????????????! ???????? 1! ???????? 0! ! ! ! ! ! !
!

NSString!*[email protected]"This!is!a!String!";! ! ! ! ! !
NSString!*[email protected]"This!is!a!String!";! ! ! ! ! !
BOOL!result!=![astring01!isEqualToString:astring02];! ! ! ! ! !
NSLog(@"result:%d",result);! ! ! ! ! !

!

//2.!compare ????(compare:????????????! ??????+1??0??1)! ! ! ! ! ! ! ! ! !
!
//NSOrderedSame ????????????????????!
NSString!*[email protected]"This!is!a!String!";! ! ! ! ! !
NSString!*[email protected]"This!is!a!String!";! ! ! ! ! ! ! ! ! !
BOOL!result!=![astring01!compare:astring02]!=!=!NSOrderedSame;! ! ! ! ! ! ! ! ! !
NSLog(@"result:%d",result);! !

!!!!!!!!!

//NSOrderedAscending ??????????????????(????????????????????astring01
???? astring02 ????)!
NSString!*[email protected]"This!is!a!String!";! ! ! ! ! !
NSString!*[email protected]"this!is!a!String!";! ! ! ! ! !
BOOL!result!=![astring01!compare:astring02]!=!=!NSOrderedAscending;! ! ! ! ! ! ! ! ! !
NSLog(@"result:%d",result);! ! ! //! ! !

//NSOrderedDescending ??????????????????(????????????????????astring01
???? astring02 ????)! ! ! !
!
NSString!*[email protected]"this!is!a!String!";! ! ! ! ! !
NSString!*[email protected]"This!is!a!String!";! ! ! ! ! !
BOOL!result!=![astring01!compare:astring02]!=!=!NSOrderedDescending;! ! ! ! ! ! ! ! ! !
NSLog(@"result:%d",result);! ! ! ! ! ! ! ! ! ! !

//3.! ??????????????????????! ! ! !
NSString!*[email protected]"this!is!a!String!";! ! ! ! ! !
NSString!*[email protected]"This!is!a!String!";! ! ! ! ! !
BOOL!result!=![astring01!caseInsensitiveCompare:astring02]!=!=!
NSOrderedSame;! ! ! ! ! ! ! ! ! !

NSLog(@"result:%d",result);! ! ! ! ! ! ! ! ! ! !

enum _NSComparisonResult {
NSOrderedAscending = -1, //!astring01 ???? astring02
NSOrderedSame, astring01 ???? astring02
NSOrderedDescending! ! ! ! ! ! ! ! ! ! astring01 ???? astring02
};!
!!!!

/*++++++++++++++++??????????????????++++++++++++++++*/!
NSString!*[email protected]"A!String";! ! ! ! ! ! !

NSString!*[email protected]"String";! ! ! !
!!!!
//1.!uppercaseString! ??????????! ! ! ! ! string1 ??????????????????????????
????????????????????????????string ??????????????????!
NSString!*string!=![string1!uppercaseString]??!
NSLog(@"string:%@",string);!
!
//2.!lowercaseString! ??????????!
string!=![string1!lowercaseString];!
NSLog(@"string:%@",string);! ! ! ! ! !
!
//3.!capitalizedString! ??????????????! ??????????????????????????! !
NSLog(@"string:%@",string);//??????????! ! ! ! ! !
!

/*++++++++++++++++????????++++++++++++++++*/!

!
NSString!*[email protected]"I!love!iOS!very!much!";! ! ! ! ! !
NSString!*[email protected]"iOS";! ! ! ! ! !
NSRange!range!=![string1!rangeOfString:string2];!//NSRange! ????????????????
?? string2 ?????? string1 ??????????????????????! ! ! ! !
NSUInteger! ! location!=!range.location;! ! ! ! ! !
NSUInteger!length=!range.length;! ! ! ! ! !
NSString!*astring!=![NSString!
stringWithFormat:@"Location:%lu,Leight:%lu",location,!length];! ! ! ! ! !
NSLog(@"astring:%@",astring);! ! ! ! ! !
[astring!release];! ! ! ! ! !
!

/*++++++++++++++++????????! ++++++++++++++++*/!
//1.!+substringToIndex:! ????????????????????????????????????????????????

!

????????! ! ! ! ! !
NSString!*[email protected]"This!is!a!string";! ! ! ! ! !
NSString!*string2!=![string1!substringToIndex:3];! ! ! ! ! !
NSLog(@"string2:%@",string2);! ! ! ! ! !
!
//2.!+substringFromIndex:! ??????????????????????????????????????????????
????????????????! ????????;! ! ! ! !
!
NSString!*[email protected]"This!is!a!string";! ! ! ! ! !
NSString!*string2!=![string1!substringFromIndex:3];! ! ! ! ! !
NSLog(@"string2:%@",string2);! ! ! ! ! !
!
//3.!+substringWithRange:!//??????????????????????????????! ! ! ! ! !
!
NSString!*[email protected]"This!is!a!string";! ! ! ! ! !
NSString!*string2!=![string1!substringWithRange:NSMakeRange(0,!4)];! ! ! ! ! !
NSLog(@"string2:%@",string2);! ! ! !

/*++++++++++++????????????????????????????????(??????????)++++++++++++*/!
!

//01??????????????????????????????????+!(BOOL)!hasPrefix:!(NSString!*)!
aString;! ! ????????????! ! ! !
!
NSString!*[email protected]"NSStringInformation.txt";! !
[String1!hasPrefix:@"NSString"]!=!=!1!?! ! NSLog(@"YES")!:!NSLog(@"NO");! ! ! ! ! !
//02??????????????????????????????????+!(BOOL)!hasSuffix:!(NSString!*)!
aString;! ! ????????????! ! ! !

[String1!hasSuffix:@".txt"]!=!=!1!?! ! NSLog(@"YES")!:!NSLog(@"NO");! ! ! ! ! ! !
!
/******************************************************************************! ! ! !

????NSMutableString!
*******************************************************************************/! ! ! ! ! ! ! ! ! !

!

/*+++++++++++++++????????????????????????++++++++++++++++*/!
//stringWithCapacity:! ! ! ! ! !

NSMutableString!*string;! ! ! ! ! !
string!=![NSMutableString!stringWithCapacity:20];//???????? 20 ????????????
????!
[email protected]”Welcome!to!qianfeng”;!

/*+++++++++++++++??????????++++++++++++++++*/!
//1.! ! appendString:! ! ! ! ! ! ! ! appendFormat:! ! ! ! ??????????????????????! !

!

NSMutableString!*String1!=![[NSMutableString!alloc]!initWithString:@"This!is!a!
NSMutableString"];! ! ! ! ! !

//[String1!appendString:@",!I!will!be!adding!some!characters"];! ! ! ! ! !
[String1!appendFormat:[NSString!stringWithFormat:@",!I!will!be!adding!some!
characters"]];! ! ! ! ! !
NSLog(@"String1:%@",String1);! ! ! !

!

//2.!+insertString:!atIndex:! ! ! ! ????????????????????! !
!
NSMutableString!*String1!=![[NSMutableString!alloc]!initWithString:@"This!is!a!
NSMutableString"];! ! ! ! ! !

[String1!insertString:@"Hi!!"!atIndex:0];! ! ! ! ! !
NSLog(@"String1:%@",String1);! ! ! ! ! !

/*++++++++??????????????????????????????????????????++++++*/!
!

//deleteCharactersInRange:! ! ??????????????????????! ! ! !
! NSMutableString!*String1!=![[NSMutableString!alloc]!initWithString:@"This!is!a!
NSMutableString"];! ! ! ! ! !
! [String1!deleteCharactersInRange:NSMakeRange(0,!5)];! ! ! ! ! !
! NSLog(@"String1:%@",String1);! ! ! ! ! !
!

/*++++++++??????????++++++*/!
!

//+setString:! ! ! ! ! ??????????!
!
NSMutableString!*String1!=![[NSMutableString!alloc]!initWithString:@"This!is!a!
NSMutableString"];! ! ! ! ! !
[String1!setString:@"Hello!Word!"];! ! ! ! ! !
NSLog(@"String1:%@",String1);! ! ! ! ! !
!

/*++++++++??????????????????????????????++++++*/!
//!replaceCharactersInRange:!withString:! ????!

!

NSMutableString!*String1!=![[NSMutableString!alloc]!initWithString:@"This!is!a!
NSMutableString"];! ! ! ! ! !
[String1!replaceCharactersInRange:NSMakeRange(0,!4)!withString:@"That"];! ! ! ! ! !
NSLog(@"String1:%@",String1);! ! ! ! !

时间: 2024-10-15 08:12:08

NSArra、NSMutableArray和NSDictionary、NSMutableDictionary和NSSting、NSMutableString的应用与总结的相关文章

关于NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary 在 OC 中我们天天都要用,而我们要怎么学习它们呢? 我认为学习这些常用的类,使用类比的方法就行了,只要学会一个类,就能类比另外两个类了.. 比如,NSString和NSMutableString的可变与不可变,主要区分的标准是,它们本身是不是可以变,可变的一般都能增,删,改,而不可变的方法主要是创建,查看,得到元素个数或者长度等等;

NSString,NSMutableString, NSArray ,NSMutableArray,NSDictionary,NSMutableDictionary 深拷贝,浅拷贝分析

NSString,NSMutableString, NSArray ,NSMutableArray,NSDictionary,NSMutableDictionary 深拷贝,浅拷贝. 首先我们得知道什么是深拷贝,什么事浅拷贝. 简单点说深拷贝就是拷贝内容,浅拷贝就是拷贝指针. 上面那些类我们可以这样理解他们的深.浅拷贝.只要上面那些类中的NSString,NSArray,NSDictionary这三个类的实例化对象是调用copy方法进行拷贝那么他们拷贝的就是一个指针,就是说他们只是拷贝了一个指向

NSDictionary , NSMutableDictionary, NSMutableDictionary 和 NSMutableSet)相当于java的map、set

1 NSDictionary 和 NSMutableDictionary NSDictionary  :就是java中的map; 放入对象是键值对 key-value  , 同样 秉持了一样的原则,只能放入对象,不可本体增删改; (1)初始化方法 + dictionaryWithObject:forKey: + dictionaryWithObjects:forKeys: + dictionaryWithObjectsAndKeys: 这三个是最常用的吧,注意是对象在前,key在后 2 访问 k

OC -- NSDictionary NSMutableDictionary

NSDictionary  NSMutableDictionary 只能存放OC对象,不能存放int, float, double, struct, enum类型数据 NSDictionary *dict1 = @{@"name" : @"As god name", @"address" : @"shanghai", @"program" : @"ios"}; NSDictionary

NSDictionary/NSMutableDictionary

NSDictionary/NSMutableDictionary 特点: (1)里存储的东西都是键值对 (2)可以像数组一样快速创建(适用 NSDictionary):@{key1 : value1, (3)快速访问元素:字典名 [key] 比如可以这样为指定 key 赋值: dic [@"key1"] = @"value1"; 快速获取 key 所对应的 value: key2 : value2} NSString *name = dic[@"name&

IOS开发之旅-IOS常用数据结构NSArray、NSMutableArray、NSDictionary、NSMutableDictionary介绍

NSArray NSArray基本用法 void arrayTest1() { //数组初始化最后必须以nil结尾,表示数组元素结束 NSArray *array1 = [[NSArray alloc]initWithObjects:@"item0",@"item1",@"item2",@"item3",@"item4",nil]; NSLog(@"%@",array1); /*( it

OC中的NSArray和NSMutableArray、NSDictionary和NSMutableDictionary用法

一:NSArray 和NSMutableArray 1: NSArray:不可变数组 NSArray是OC中使用的数组,只能用来存放OC对象,不能存放非OC对象如基本数据类型 它使不可变的,一旦初始化完毕,内容不能改变,也不能添加元素. 而C语言中的数组只能存放一种数据类型 (1) 普通数组的用法 // 普通数组的创建 // int arr[5] = {1,3,4}; // 对象数组的创建 // Person *p = [[Person alloc] init]; // Person *arrP

NSArray,NSMutable和NSSet,NSMutableSet和NSDictionary,NSMutableDictionary用法

开始编写应用程序的代码时,可以利用大量的 Objective-C 框架.其中,为所有应用程序提供基本服务的 Foundation 框架尤为重要.Foundation 框架包括表示基本数据类型的值类(如字符串和数字)以及用于储存其他对象的集 (collection) 类.ToDoList 应用程序中的大量代码都可以依靠值类和集类来编写. 值对象 Foundation 框架提供了为字符串.二进制数据.日期与时间.数字以及其他值产生值对象的类. 值对象是指封装了基本值(属于 C 数据类型)且提供与该值

OC_ NSDictionary ,NSMutableDictionary 字典

1 数组与字典的区别 数组: 1>存放一组有序对象的集合 2> 通过index下标找对应的对象 3> 数组中对象可以重复 字典: 1>存放一组无序key-value的集合 2> 通过key找到对应的value 3>字典key是唯一的,但是不同的value可以重复 2 字典的初始化 使用一个类方法创建一个空字典(常用) NSDictionary *dic1 = [NSDictionary dictionary]; (常用) 通过字面量去创建不可变数组和不可变字典 NSAr