标签:
*****************************************************************************
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!*astring!=!@"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!*path!=!@"!/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!*astring01!=!@"This!is!a!String!";! ! ! ! ! ! NSString!*astring02!=!@"This!is!a!String!";! ! ! ! ! ! BOOL!result!=![astring01!isEqualToString:astring02];! ! ! ! ! ! NSLog(@"result:%d",result);! ! ! ! ! !
!
//2.!compare ????(compare:????????????! ??????+1??0??1)! ! ! ! ! ! ! ! ! !
!
//NSOrderedSame ????????????????????!
NSString!*astring01!=!@"This!is!a!String!";! ! ! ! ! !
NSString!*astring02!=!@"This!is!a!String!";! ! ! ! ! ! ! ! ! !
BOOL!result!=![astring01!compare:astring02]!=!=!NSOrderedSame;! ! ! ! ! ! ! ! ! !
NSLog(@"result:%d",result);! !
!!!!!!!!!
//NSOrderedAscending ??????????????????(????????????????????astring01
???? astring02 ????)!
NSString!*astring01!=!@"This!is!a!String!";! ! ! ! ! !
NSString!*astring02!=!@"this!is!a!String!";! ! ! ! ! !
BOOL!result!=![astring01!compare:astring02]!=!=!NSOrderedAscending;! ! ! ! ! ! ! ! ! !
NSLog(@"result:%d",result);! ! ! //! ! !
//NSOrderedDescending ??????????????????(????????????????????astring01
???? astring02 ????)! ! ! !
!
NSString!*astring01!=!@"this!is!a!String!";! ! ! ! ! !
NSString!*astring02!=!@"This!is!a!String!";! ! ! ! ! !
BOOL!result!=![astring01!compare:astring02]!=!=!NSOrderedDescending;! ! ! ! ! ! ! ! ! !
NSLog(@"result:%d",result);! ! ! ! ! ! ! ! ! ! !
//3.! ??????????????????????! ! ! ! NSString!*astring01!=!@"this!is!a!String!";! ! ! ! ! ! NSString!*astring02!=!@"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!*string1!=!@"A!String";! ! ! ! ! ! !
NSString!*string2!=!@"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!*string1!=!@"I!love!iOS!very!much!";! ! ! ! ! !
NSString!*string2!=!@"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!*string1!=!@"This!is!a!string";! ! ! ! ! !
NSString!*string2!=![string1!substringToIndex:3];! ! ! ! ! !
NSLog(@"string2:%@",string2);! ! ! ! ! !
!
//2.!+substringFromIndex:! ??????????????????????????????????????????????
????????????????! ????????;! ! ! ! !
!
NSString!*string1!=!@"This!is!a!string";! ! ! ! ! !
NSString!*string2!=![string1!substringFromIndex:3];! ! ! ! ! !
NSLog(@"string2:%@",string2);! ! ! ! ! !
!
//3.!+substringWithRange:!//??????????????????????????????! ! ! ! ! !
!
NSString!*string1!=!@"This!is!a!string";! ! ! ! ! !
NSString!*string2!=![string1!substringWithRange:NSMakeRange(0,!4)];! ! ! ! ! !
NSLog(@"string2:%@",string2);! ! ! !
/*++++++++++++????????????????????????????????(??????????)++++++++++++*/! !
//01??????????????????????????????????+!(BOOL)!hasPrefix:!(NSString!*)!
aString;! ! ????????????! ! ! !
!
NSString!*String1!=!@"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 ????????????
????!
string.string!=!@”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);! ! ! ! !
NSArra、NSMutableArray和NSDictionary、NSMutableDictionary和NSSting、NSMutableString的应用与总结
标签:
原文地址:http://www.cnblogs.com/PengFei-N/p/4703166.html