哪种遍历nsarray 快速遍历/NSDictionary方式快

扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
IOS开发NSArray,NSSet,NSDictionary,NSString操作总结
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口当前访客身份:游客 [
得到别人的帮助,我想分给需要帮助的人
:&--- Call MEMDEV_IOCPRINT ---& Call cmd MEMDE...
:确定程序可以正常运行吗?我在测试程序中发现,发...
:收藏了,谢谢
今日访问:14
昨日访问:31
本周访问:285
本月访问:891
所有访问:12121
ios model字典 NSArray NSDictionary小结
发表于1个月前( 10:33)&&
阅读(16)&|&评论()
0人收藏此文章,
ios model字典 NSArray NSDictionary小结
ios model字典
#import&&Foundation/Foundation.h&
@interface&AdObject&:&NSObject
///&广告ID
@property&(strong,nonatomic)&NSString&*adID;
///&广告图片的地址
@property&(strong,nonatomic)&NSString&*imageP
///&广告标题
@property&(strong,nonatomic)&NSString&*adT
&*&&@brief&&自定义初始化方法
&*&&@param&dict&存放&HPAdObject&内容的字典
&*&&@return&当前类的一个实体
-&(id)initWithDictionary:(NSDictionary*)
@implementation&AdObject
-&(id)initWithDictionary:(NSDictionary*)dict
&&&&if&(self&=&[super&init])
&&&&&&&&_adID&=&dict[@"id"];
&&&&&&&&_imagePath&=&dict[@"img"];
&&&&&&&&_adTitle&=&dict[@"title"];
&&&&return&
AdObject&*obj&=&[[AdObject&alloc]initWithDictionary:dictItem];
@implementation&AdObject
+&(instancetype)carGroupWithDict:(NSDictionary&*)dict
&&&&AdObject&*cg&=&[[self&alloc]&init];
&&&&[cg&setValuesForKeysWithDictionary:dict];
&&&&return&
AdObject&*cg&=&[AdObject&initWithDictionary:dict];&//alloc也给封装了,
获取本地文件
NSArray&*tmp&=&[NSArray&arrayWithContentsOfFile:[[NSBundle&mainBundle]&pathForResource:@"cars_simple"&ofType:@"plist"]];
NSArray使用小结
//类方法数组创建&&
NSArray&*array1&=&[NSArray&arrayWithObject:@"obj"];&&
NSArray&*array2&=&[NSArray&arrayWithObjects:@"obj1",&@"obj2",&@"obj3",&nil];&&
NSArray&*array3&=&[NSArray&arrayWithArray:array2];&&
//实例方法创建数组&&
NSArray&*array4&=&[[NSArray&alloc]&initWithObjects:@"AAA",&@"bbb",&nil];&&
//数组个数&&
NSLog(@"array3&count&:%d",&[array3&count]);&&
&&&&&&&&&&
//访问元素&&
NSLog(@"obj&at&index&:%@",&[array2&objectAtIndex:2]);&&
//是否包含指定对象&&
NSLog(@"isContains&:%d",&[array5&containsObject:@"obj2"]);&&
&&&&&&&&&&&&&&&&&
//查找某个对象所在索引&&
NSLog(@"indexOfObject&:%d",[array5&indexOfObject:@"obj3"]);&&
&&&&&&&&&&
//最后一个元素&&
NSLog(@"lastObejct&:%@",&[array5&lastObject]);&&
&&&&&&&&&&
//遍历数组&&
for&(id&element&in&array5)&{&&
&&&&NSLog(@"element&:%@",&element);&&
NSMutableA
NSMutableArray&*MutableArray&=&[NSMutableArray&alloc]&init];
[mutableArray&addObject:@"000"];&&
//插入元素&&
[mutableArray&insertObject:@"ccc"&atIndex:0];&
//移除指定元素&&
[mutableArray&removeObject:@"ccc"];&&
&&&&&&&&&&
//移除指定下标元素&&
[mutableArray&removeObjectAtIndex:0];&&
&&&&&&&&&&
//移除最后一个元素&&
[mutableArray&removeLastObject];&&
&&&&&&&&&&
//添加数组&&
[mutableArray&addObjectsFromArray:array5];&&
&&&&&&&&&&
//移除指定数组中的内容&&
[mutableArray&removeObjectsInArray:array2];&&
&&&&&&&&&&
//指定索引替换对象&&
[mutableArray&replaceObjectAtIndex:0&withObject:@"==="];&&
//删除全部对象&&
[mutableArray&removeAllObjects];
NSDictionary
//创建多个字典&&
NSDictionary&*dic2&=&[NSDictionary&dictionaryWithObjectsAndKeys:&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&@"value1",&@"key1",&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&@"value2",&@"key2",&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&@"value3",&@"key3",&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&@"value4",&@"key4",&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&nil];&
//根据key获取value&&
NSLog(@"key3&value&:%@",&[dic3&objectForKey:@"key3"]);&&
&&&&&&&&&&
//获取字典数量&&
NSLog(@"dic&count&:%d",&dic3.count);&&
&&&&&&&&&&
//所有的键集合&&
NSArray&*keys&=&[dic3&allKeys];&&
&&&&&&&&&&
//所有值集合&&
NSArray&*values&=&[dic3&allValues];
NSMutableD
NSMutableDictionary&*mutableDic&=&[[NSMutableDictionary&alloc]&init];
//添加现有的字典数据&&
[mutableDic&addEntriesFromDictionary:dic3];&
//添加新的键值对象&&
[mutableDic&setValue:@"set1"&forKey:@"setKey1"];&&
&&&&&&&&&&
//以新的字典数据覆盖旧的字典数据&&
[mutableDic&setDictionary:dic2];&&
&&&&&&&&&&
//根据key删除value&&
[mutableDic&removeObjectForKey:@"key1"];&&
&&&&&&&&&&
//快速遍历&&
for(id&key&in&mutableDic)&{&&
&&&&NSLog(@"key&:%@&&value&:%@",&key,&[mutableDic&objectForKey:key]);&&
&&&&&&&&&&
//枚举遍历&&
NSEnumerator&*enumerator&=&[mutableDic&keyEnumerator];&&
id&key&=&[enumerator&nextObject];&&
while&(key)&{&&
&&&&&NSLog(@"enumerator&:%@",&[mutableDic&objectForKey:key]);&&
&&&&&key&=&[enumerator&nextObject];&&
&&&&&&&&&&
//根据key数组删除元素&&
[mutableDic&removeObjectsForKeys:keys];&&
//删除所有元素&&&&&&&&
[mutableDic&removeAllObjects];
更多开发者职位上
1)">1)">1" ng-class="{current:{{currentPage==page}}}" ng-repeat="page in pages"><li class='page' ng-if="(endIndex<li class='page next' ng-if="(currentPage
相关文章阅读the_loong 的BLOG
用户名:the_loong
文章数:111
评论数:15
访问量:273072
注册日期:
51CTO推荐博文
////& NSString+HXAddtions.h//& HXWeb////& Created by hufeng on 12-2-13.//& Copyright (c) 2012年&__MyCompanyName__. All rights reserved.//#import&&Foundation/Foundation.h&@interface&NSString (HXAddtions)+(NSString *) jsonStringWithDictionary:(NSDictionary *)+(NSString *) jsonStringWithArray:(NSArray *)+(NSString *) jsonStringWithString:(NSString *)+(NSString *) jsonStringWithObject:(id)+(void) jsonT@end////& NSString+HXAddtions.m//& HXWeb////& Created by hufeng on 12-2-13.//& Copyright (c) 2012年&__MyCompanyName__. All rights reserved.//#import&"NSString+HXAddtions.h"@implementation&NSString (HXAddtions)+(void)jsonTest{& &&//test&& && & NSDictionary *dictionary1 = [NSDictionary dictionaryWithObjectsAndKeys:&& & & & & & & & & & & & & & & &&@"阿三\"\n11",@"name",&& & & & & & & & & & & & & & & &&@"18",@"age",&& & & & & & & & & & & & & & & &&nil];& & NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjectsAndKeys:&& & & & & & & & & & & & & & & &&@"阿四",@"name",&& & & & & & & & & & & & & & & &&@"20",@"age",&& & & & & & & & & & & & & & & &&nil];&& && & NSArray *array = [NSArray arrayWithObjects:dictionary1,dictionary2,&nil];&& &&& && & NSDictionary *dictionary3 = [NSDictionary dictionaryWithObjectsAndKeys:&& & & & & & & & & & & & & & & &&@"李\na",@"name",&& & & & & & & & & & & & & & & &&@"29",@"age",&& & & & & & & & & & & & & & & &&nil];&& && & NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:& & & & & & & & & & & & & & & &&@"string",@"string",& & & & & & & & & & & & & & & & array,@"array",& & & & & & & & & & & & & & & & dictionary3,@"dictionary",& & & & & & & & & & & & & & & &&nil];& & NSLog(@"dictionary:%@",dictionary);& & NSString *jsonString = [NSString jsonStringWithObject:dictionary];& & NSLog(@"dictionary jsonString:%@",jsonString);&& && & NSLog(@"%@",[NSDictionary dictionaryWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]]);&& &}+(NSString *) jsonStringWithString:(NSString *) string{& &&return&[NSString stringWithFormat:@"\"%@\"",& & & & & & & & & & & & & & [[string stringByReplacingOccurrencesOfString:@"\n"&withString:@"\\n"] stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""]& & & & & & & & & & & & & ];}+(NSString *) jsonStringWithArray:(NSArray *)array{& & NSMutableString *reString = [NSMutableString string];& & [reString appendString:@"["];&& & NSMutableArray *values = [NSMutableArray array];& &&for&(id&valueObj&in&array) {& & & & NSString *value = [NSString jsonStringWithObject:valueObj];& & & &&if&(value) {& & & & & & [values addObject:[NSString stringWithFormat:@"%@",value]];& & & & }& & }& & [reString appendFormat:@"%@",[values componentsJoinedByString:@","]];& & [reString appendString:@"]"];&& &&return&reS}+(NSString *) jsonStringWithDictionary:(NSDictionary *)dictionary{& & NSArray *keys = [dictionary allKeys];& & NSMutableString *reString = [NSMutableString string];& & [reString appendString:@"{"];& & NSMutableArray *keyValues = [NSMutableArray array];& &&for&(int&i=0; i&[keys count]; i++) {& & & & NSString *name = [keys objectAtIndex:i];& & & &&id&valueObj = [dictionary objectForKey:name];& & & & NSString *value = [NSString jsonStringWithObject:valueObj];& & & &&if&(value) {& & & & & & [keyValues addObject:[NSString stringWithFormat:@"\"%@\":%@",name,value]];& & & & }& & }& & [reString appendFormat:@"%@",[keyValues componentsJoinedByString:@","]];& & [reString appendString:@"}"];& &&return&reS}+(NSString *) jsonStringWithObject:(id) object{& & NSString *value =&nil;& &&if&(!object) {& & & &&return&& & }& &&if&([object isKindOfClass:[NSString class]]) {& & & & value = [NSString jsonStringWithString:object];& & }else&if([object isKindOfClass:[NSDictionary class]]){& & & & value = [NSString jsonStringWithDictionary:object];& & }else&if([object isKindOfClass:[NSArray class]]){& & & & value = [NSString jsonStringWithArray:object];& & }& &&return&}@end
了这篇文章
类别:┆阅读(0)┆评论(0)

我要回帖

更多关于 nsdictionary 遍历key 的文章

 

随机推荐