- 浏览: 421456 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
binghc:
能使用浏览器作为客户端么,用socket构建一个最简单的htt ...
HTTPS那些事 用java实现HTTPS工作原理 -
littleShyBoy:
如果是使用http client请求https的接口,http ...
HTTPS那些事 用java实现HTTPS工作原理 -
skw1975:
...
HTTPS那些事 用java实现HTTPS工作原理 -
sealinesu:
看了半天,真的是半天,总算是把这些概念都理清了,谢谢博主
spring事务传播机制实例讲解 -
wanghaozdw:
请问下,在内外层事务均是REQUIRED的情况下,内层事务抛出 ...
spring事务传播机制实例讲解
NSSet:无序的集合,散列存储。
读developer.apple关于NSSet的解释:You can use sets as an alternative to arrays when the order of elements isn’t important and performance in testing whether an object is contained in the set is a consideration—while arrays are ordered, testing for membership is slower than with sets.
就是说,如果搜索一个元素,NSSet的效率会比NSArray高。为什么呢?道理比较简单:hash!NSSet中元素的存储和访问都是一个hash的过程。比如你要存储元素A,一个hash算法直接就能直接找到A应该存储的位置;同样,当你要访问A时,一个hash过程就能找到A存储的位置。而对于NSArray,若想知道A到底在不在数组中,则需要一个一个元素比较,显然效率没了。
1,Objective-C 2.0法,最应该使用的
NSArray *aArray; //我们的Array,假设他已经初始化有内容了 for(id innerObj in aArray) //id可以由其他对象类型替代 { //也就是说, 在循环中的参数innerObj就是aArray中的对象 //由于NSArray中只能储存对象,所以我们使用id作为内涵对象的类型,其实就是个指针了 //而且如果NSArray中的内容不一致,id更不会出现冲突 }
2,C的老方法,不推荐,低性能
NSArray *aArray; //我们的Array,假设他已经初始化有内容了 for(int i=0;i<[aArray count]; i++) { [[aArray objectAtIndex:i] 然后作你想作的事情]; //做爱 作的事情... }
3, 用NSEnumerator(不知道怎么翻译阿….)
NSEnumerator* myIterator = [myArray reverseObjectEnumerator]; id anObject; while( anObject = [myIterator nextObject]) //每次读取“逐读器”的下一个对象 { //anObject和刚才的innerObj是一个意思,但是看看多了这么多行 }
三种集合类来收集cocoa对象(NSObject对象):
NSArray 用于对象有序集合(相当于是数组)
NSSet 用于对象无序集合
NSDictionary用于键值映射
以上三种集合类是不可变的(一旦初始化后,就不能改变)
以下是对应的三种可变集合类(这三种可变集合类是对应上面三种集合类的子类):
NSMutableArray
NSMutableSet
NSMutableDictionary
注:这些集合类只能收集cocoa对象(NSOjbect对象),如果想保存一些原始的C数据(例如,int, float, double, BOOL等),则需要将这些原始的C数据封装成NSNumber类型的,NSNumber对象是cocoa对象,可以被保存在集合类中。
NSArray
Ordered collection of objects. Immutable. You cannot add or remove objects to it once it’s created.
Important methods:
+ (id)arrayWithObjects:(id)firstObject, ...; // nil terminated
- (int)count;
- (id)objectAtIndex:(int)index; // NSString *s1=[[myarray objectAtIndex: 0];
- (void)makeObjectsPerformSelector:(SEL)aSelector;
- (NSArray *)sortedArrayUsingSelector:(SEL)aSelector;
- (id)lastObject; // returns nil if there are no objects in the array (convenient)
注:
类方法arrayWithObjects 可以创建an autoreleased NSArray of the items.例如
@implementation MyObject
- (NSArray *)coolCats {
return [NSArray arrayWithObjects:@“Steve”, @“Ankush”, @“Sean”, nil];
}
@end
Other convenient create with methods (all return autoreleased objects):
[NSString stringWithFormat:@“Meaning of %@ is %d”, @“life”, 42];
[NSDictionary dictionaryWithObjectsAndKeys:ankush, @“TA”, janestudent, @“Student”, nil];
[NSArray arrayWithContentsOfFile:(NSString *)path];
-----创建数组 -----
//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 = 0; i < [oldArray count]; i++)
{
obj = [[oldArray objectAtIndex:i] copy];
[newArray addObject: obj];
}
//
NSLog(@"newArray:%@", newArray);
[newArray release];
//快速枚举
//NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSArray *oldArray = [NSArray arrayWithObjects:
@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
NSLog(@"oldArray:%@",oldArray);
for(id obj in oldArray)
{
[newArray addObject: obj];
}
//
NSLog(@"newArray:%@", newArray);
[newArray release];
//Deep copy
//NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSArray *oldArray = [NSArray arrayWithObjects:
@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
NSLog(@"oldArray:%@",oldArray);
newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);
NSLog(@"newArray:%@", newArray);
[newArray release];
//Copy and sort
//NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSArray *oldArray = [NSArray arrayWithObjects:
NSMutableArray
Mutable version of NSArray.
- (void)addObject:(id)anObject;
- (void)insertObject:(id)anObject atIndex:(int)index;
- (void)removeObjectAtIndex:(int)index;
- (void)removeAllObjects;
-----给数组分配容量-----
//NSArray *array;
array = [NSMutableArray arrayWithCapacity:20];
-----在数组末尾添加对象-----
//- (void) addObject: (id) anObject;
//NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];
[array addObject:@"Four"];
NSLog(@"array:%@",array);
-----删除数组中指定索引处对象-----
//-(void) removeObjectAtIndex: (unsigned) index;
//NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];
[array removeObjectAtIndex:1];
NSLog(@"array:%@",array);
----- 数组枚举-----
//1、- (NSEnumerator *)objectEnumerator; //从前向后
//NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];
NSEnumerator *enumerator;
enumerator = [array objectEnumerator];
id thingie;
while (thingie = [enumerator nextObject]) {
NSLog(@"thingie:%@",thingie);
}
//2、- (NSEnumerator *)reverseObjectEnumerator; //从后向前
//NSMutableArray *array = [NSMutableArray arrayWithObjects:
@"One",@"Two",@"Three",nil];
NSEnumerator *enumerator;
enumerator = [array reverseObjectEnumerator];
id object;
while (object = [enumerator nextObject]) {
NSLog(@"object:%@",object);
}
//3、快速枚举
//NSMutableArray *array = [NSMutableArray arrayWithObjects:
@"One",@"Two",@"Three",nil];
for(NSString *string in array)
{
NSLog(@"string:%@",string);
}
----- NSValue(对任何对象进行包装)-----
//将NSRect放入NSArray中
NSMutableArray *array = [[NSMutableArray alloc] init];
NSValue *value;
CGRect rect = CGRectMake(0, 0, 320, 480);
value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];
[array addObject:value];
NSLog(@"array:%@",array);
//从Array中 提取
value = [array objectAtIndex:0];
[value getValue:&rect];
NSLog(@"value:%@",value);
----★使用NSMutableArray要防止内存泄露★------
NSObject* p1 = [[NSObject alloc] init];
NSObject* p2 = [[NSObject alloc] init];
NSMutableArray* objectsArray = [[NSMutableArray alloc] init];
[objectsArray addObject:p1];
NSLog(@"p1 count:%d", [p1 retainCount]);//输出 2,也就是执行追加对象后,对象的计数器也被加1
//[p1 release];
//NSLog(@"p1 count:%d", [p1 retainCount]);
//同样做数组替换时
[objectsArray replaceObjectAtIndex:0 withObject:p2];
NSLog(@"p2 count:%d", [p2 retainCount]);//输出 2,同样也是2
NSLog(@"p1 count:%d", [p1 retainCount]);//输出 1,对象p1仍然存在
//[p2 release];
//NSLog(@"p2 count:%d", [p2 retainCount]);
//执行清空数组
[objectsArray removeAllObjects];
NSLog(@"p2 count:%d", [p2 retainCount]);//输出 1,对象p2仍然存在
//[p2 release];
由此可知,每次执行上面的数组操作后,要执行对象release,如上面注释中的语句,才能保证内存不泄露。
NSSet
Unordered collection of objects.
Immutable. You cannot add or remove objects to it once it’s created.
Important methods:
+ setWithObjects:(id)firstObj, ...; // nil terminated
- (BOOL)containsObject:(id)anObject;
- (id)anyObject;
- (void)makeObjectsPerformSelector:(SEL)aSelector;
- (id)member:(id)anObject; // uses isEqual: and returns a matching object (if any)
NSMutableSet
Mutable version of NSSet.
+ (NSMutableSet *)set;
- (void)removeObject:(id)anObject;
- (void)unionSet:(NSSet *)otherSet;
- (void)minusSet:(NSSet *)otherSet;
- (void)intersectSet:(NSSet *)otherSet;
NSDictionary
key-value, key-value, ..... 一系列键值对。
Immutable. You cannot add or remove objects to it once it’s created.
Keys are objects which must implement. Keys are usually NSString objects.
- (NSUInteger)hash & - (BOOL)isEqual:(NSObject *)obj
Important methods:
+ dictionaryWithObjectsAndKeys: (id)firstObject, ...;
- (id)objectForKey:(id)key;
- (NSArray *)allKeys;
- (NSArray *)allValues;
//- (id) initWithObjectsAndKeys;
//NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];
NSString *string = [dictionary objectForKey:@"One"];
NSLog(@"string:%@",string);
NSLog(@"dictionary:%@",dictionary);
[dictionary release];
Mutable version of NSDictionary.
+ (NSMutableDictionary *)dictionary;
- (void)removeObjectForKey:(id)key;
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
//创建
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
//添加字典
[dictionary setObject:@"One" forKey:@"1"];
[dictionary setObject:@"Two" forKey:@"2"];
[dictionary setObject:@"Three" forKey:@"3"];
[dictionary setObject:@"Four" forKey:@"4"];
NSLog(@"dictionary:%@",dictionary);
//删除指定的字典
[dictionary removeObjectForKey:@"3"];
NSLog(@"dictionary:%@",dictionary);
发表评论
-
iOSSDK5+XCode4.2下百度地图错误处理
2014-02-09 12:18 990最近需要使用百度地图进行ios开发,从官方下载最新的2.1 ... -
xcode破解
2012-12-05 10:10 0http://mmz06.blog.163.co ... -
ios发送邮件
2012-12-04 12:12 2786IOS 程序内发邮件 ... -
XCode免证书开发环境
2012-12-04 10:13 1192本文转自 http://www.189works.com/ar ... -
Cocoa框架一览
2012-06-18 17:33 2581Cocoa框架 本文转自CSDN Blog ... -
Windows下的Objective-C集成开发环境(IDE)的搭建 (二)
2012-06-18 15:43 7839Windows下的Objective-C集成开发环境(ID ... -
ASIHttprequest的使用
2012-06-14 10:22 10604本文转自csdn ASIHTTPRequest对 ... -
NSDate的使用
2012-06-13 15:49 857NSDate //得到当前的日期 NSDate *da ... -
Windows下的Objective-C集成开发环境(IDE)的搭建 (一)
2012-06-12 10:42 14571Windows下的Objective- ... -
VMware7下安装的Mac OS X如何修改显示分辨率
2012-04-15 09:34 2576VMware7下安装的Mac OS X如何修改显示分辨率 作 ... -
win7-64位通过VMware安装Macos系统
2012-04-14 22:45 5286经过几天的折腾和努力,今天终于在win7(64bit)下将Ma ... -
object-c demo例子
2012-04-09 18:25 2890先写一个hello.m #import <Fou ... -
windows下搭建object-c遇到的问题
2012-04-09 16:56 1968具体搭建步骤可参见donhao的: 一起来学Object ...
相关推荐
详细的说明和用法 NSArray,NSSet,NSDictionary 以及他们子类的说明
这是NSArray,NSSet,NSEnumerator,NSDictionary及对应Mutable介绍的示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7884218
NSArray,NSSet,NSEnumerator,NSDictionary及对应Mutable介绍,有对应的实例代码
一些NSArray,NSDictionary,NSSet相关的算法知识1
主要介绍了iOS集合遍历(NSArray、NSDictionary、NSSet)的方法,文中给出了详细的方法示例,并总结了各个方法的优缺点来供大家学习参考,需要的朋友们下面来一起看看吧。
NSArray排序demo
Swift-NSArray数组的使用
这是iPhone常用集合类介绍的示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7884218
OC-NSArray数组的使用Demol
Objective C数组的内存布局要了解NSArray,NSSet,NSDictionary这些集合类的使法,我们需要先弄明其对应的内存布局(Memory L
官方文档,数组类的最权威文档。各种数组方法的详细说明。英文的。
在使用了 Linq、Underscore.js 和 ActiveRecord 等框架之后,我们习惯了许多为集合提供的方法。 我们没有继续在没有它们的情况下生活,而是自己写了一些。 每个返回NSArray或NSSet都会创建一个新对象,而不会改变...
NSArray+Reduce Reduce 是 NSArray 上的 Objective-C 类别,提供有用的数组函数,如reduce 、 filter和map 。
NSArray *arr = [[NSArray alloc] initWithObjects:如果要初始化1000项,可以这样:当然换成swift就超级简单了
Object C 字符串和数组NSString NSArray操作
比较完美的解决了数组越界的问题, 在debug模式下会崩溃 , 在release不会崩溃 , 开发过程中 , 方便调试 , 正式环境中 减少崩溃.
NSString NSArray NSDictionary 方法名释义
1. 数组的常用处理方式 //--------------------不可变数组 //1....NSString *s1 = @zhangsan; NSString *s2 = @lisi; NSString *s3 = @wangwu;...//(2)使用类方法创建 NSArray *array2 = [NSArray arrayWithOb
Iphone数组一些基础操作 NSArray/NSMutableArrayIphone