设为首页 加入收藏

TOP

Objective-c史上最全字符串处理(八)
2014-11-23 19:52:14 来源: 作者: 【 】 浏览:45
Tags:Objective-c 史上 最全 字符串 处理
"aa"];
[mutableArr addObject:@"bb"];
[mutableArr addObject:@"cc"];
[mutableArr addObject:@"dd"];

for(int i=0;i<[mutableArr count];i++){
NSLog(@"mutableArr==%@",[mutableArr objectAtIndex:i]);
}

//----- -(void) removeObjectAtIndex:(unsinged) index; 删除指定索引的对象,
//删除一个对象之后,数组中并没有留下 漏洞,被删除对象后面的数组元素的哦被前移来填补空缺
[mutableArr removeObjectAtIndex:2];
for(int i=0;i<[mutableArr count];i++){
NSLog(@"removeObjectAtIndex == %@",[mutableArr objectAtIndex:i]);
}

//枚举
//NSEnumerator ,它是cocoa用来描述这种集合迭代运输的方法
//-(NSEnumerator *) objectEnumerator;
NSEnumerator *enumerator=[mutableArr objectEnumerator];
id thingie;
while(thingie=[enumerator nextObject]){
NSLog(@"i found %@",thingie);
}

//快速枚举
for(NSString *string in mutableArr){
NSLog(@"for in == %@",string);
}

//NSDictionary 字典
/*
NSDictionary 是在给定的关键字(通常是一个NSString字符串)下存储一个数值(可以是任何类型的对象)。然后你可以用这个关键字来查找相应的数值。
NSDictionary 是键查询的优化存储方式。它可以立即找出要查询的数据,而不需要遍历整个数组进行查找。

+(id) dictionaryWithObjectAndKeys:(id) firstObject,....;
该方法接收对象和关键字交替出现的系列,以nil值作为终止符号。
**/
NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"aaa",@"a",@"bbb",@"b",nil];
NSString *dicStr=[dic objectForKey:@"a"];
if([dicStr isEqualToString:@"aaa"]){
NSLog(@"------------00000000000000000");
}

//可变字典
NSMutableDictionary *mutableDic=[NSMutableDictionary dictionaryWithCapacity:50];
[mutableDic setObject:@"1111" forKey:@"1"];
[mutableDic setObject:@"222" forKey:@"2"];

//删除 -(void) removeObjectForKe:(id) key;
[mutableDic removeObjectForKey:@"2"];

NSArray *keyArr=[mutableDic allKeys];
for(NSString *str in keyArr){
NSLog(@"key== %@",str);
NSLog(@"value== %@",[mutableDic objectForKey:str]);
}


//各种数值,NSNumber NSValue
/*
cocoa 提供了NSNumber类来包装基本数据类型
+(NSNumber *) numberWithChar:(char) value;
+(NSNumber *) numberWithInt:(int) value;
+(NSNumber *) numberWithFloat:(float) value;
+(NSNumber *) numberWthiBool:(BOOL) value;

-(char) charValue;
-(int) intVlaue;
-(float) floatValue;
-(BOOL) boolValue;
-(NSString *) stringValue;


**/
NSNumber *number;
number=[NSNumber numberWithInt:3];
[mutableDic setObject:number forKey:@"int"];

int num=[[mutableDic objectForKey:@"int"] intValue];
NSLog(@"int object value== %d",num);


//NSValue .NSNumber实际上是NSValue的子类,NSValue可以包装任意值

/**
+(NSValue *) valueWithBytes:(const void *) value objCType:(const char *) type;
传递的参数是你想要包装的数值的地址,通常,得到的是你想要存储的变量的地址(在 c语言里适用操作符 & ),你也可以提供一个描述这个数据类型的字符串,通常用来说明struct中实体的类型和大小。你不用自己写代码
来生成这个字符串,@encode编译器指令可以接受数据类型的名称并为你生成合适的字符串
*/
NSRect rect= NSMakeRect(1, 2, 30, 40);

NSValue *value;
value=[NSValue valueWithBytes:&rect objCType:@encode(NSRect)];
NSMutableArray *mr=[NSMutableArray arrayWithCapacity:50];
[mr addObject:value];

//getValue 提取数据
/**
-(void) getValue:(void *) value; 要传递的是存储这个数值的变量的地址
*/

/***
value=[mr objectAtIndex:0];

NSRect r;
NSLog(@"00000 ===%@",r);
[value getValue:&r];
NSLog(@"111== %@",r);
*/

/**
+(NSValue *) valueWithPoint:(NSPoint) point;
+(NSValue *) valueWithSize:(NSSize) size;
+(NSValue *) valueWithRect:(NSRect) rect;



-(NSPoint) pointValue;
-(NSSize) sizeva lue;
-(NSRect) rectValue;

*/

//NSNull
/*
*+(NSNull *) null;
*/
[mutableDic setObject:[NSNull null] forKey:@"fax"];
id fax;
fax=[mutableDic objectForKey:@"fax"];
if(fax==[NSNull null]){
NSLog(@
首页 上一页 5 6 7 8 下一页 尾页 8/8/8
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇最小堆 / 优先队列(C语言实现) 下一篇C语言经典面试题――一年有多少秒..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: