- 浏览: 675613 次
- 性别:
- 来自: 深圳
最新评论
-
fingerplay:
请问一下,第一份,逻辑树,就是代码里可以操纵的,例如更改lay ...
UIView与CALayer -
ok_lyc:
分享到哪里去了
iPhoneUIFont各种字体 -
lliiqiang:
我的个人理解:wait方法是在java虚拟机层面上在获取对象锁 ...
JAVA多线程同步wait、notify、synchronized -
milixw:
谢谢分享,就在找这个
iphone 推送通知 java 实现 -
wsqwsq000:
你的log4j包不行,上网搜一下:log4j-1.2.16.j ...
iphone 推送通知 java 实现
三种集合类来收集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);
评论
http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html
发表评论
-
iphone开发如何后台播放音频
2012-12-25 14:38 1557<!--?xml version="1.0&q ... -
iPhone开发中使用AVAudioPlayer出现内存泄漏的解决办法
2012-12-25 11:53 3344最近在使用AVAudioPlayer播放音频时,发现 ... -
dispatch多线程
2012-10-31 20:45 1452#define kBgQueue dispat ... -
iPhone开发使用数组排序
2012-10-30 19:18 1786我们以数组的排序为例(也许NSSortDes ... -
用containsObject判断是否存在的问题
2012-10-16 15:37 4557在NSArray或NSMutableArray中想使用c ... -
用NSZombieEnabled解决恼人的EXC_BAD_ACCESS错误
2012-09-17 10:30 1374转载自:http://longtimenoc.com/a ... -
使用xib加载视图后,在dealloc中crash
2012-09-08 11:20 1764最近遇到个问题,就是使用xib加载视图后,经常会 ... -
iPhone中如何获取当前输入法
2012-09-06 16:28 1696如题,在iPhone开发中,我们如何获取当前的输入 ... -
objectForKey和valueForKey的区别
2012-08-24 17:23 2162valueforkey和objectforkey区别 ... -
粒子发射系统中的CAEmitterLayer
2012-08-22 16:00 2516简介:CAEmitterLayer提供了一个基于 ... -
iPhone开发使用UILineBreakMode不精确
2012-07-26 12:02 1548一开始用UILineBre ... -
Xcode方法提示不显示的解决办法
2012-07-24 09:45 1610Xcode方法提示显示的不正确,只显示方法 ... -
NSArray的排序功能sortedArrayUsingSelector
2012-07-18 17:54 2227- (NSArray *)sortedArrayUs ... -
iPhoneUIFont各种字体
2012-07-24 09:45 1556苹果开发者们想在应用中使用不同字体的话,往往会发现 ... -
iPhone使用ASIHTTPRequest请求时,发送了2次请求
2012-07-24 09:45 2554前段时间用到ASIHTTPRequest做请求时, ... -
iPhone开发调用新浪微博OAuth2.0
2012-06-05 02:30 3100就像题目说的,这次的小项目是做一个新浪微博的客 ... -
Iphone cover flow 开源实现
2012-06-04 00:04 884tapkulibrary-CloverFlow ... -
ASIHTTPRequest下载示例(支持断点续传)
2012-07-24 09:45 1591在工程中,我们会常常遇到需要下载的程序,比 ... -
ASIHTTPRequest类库简介和使用说明
2012-07-24 09:45 1692文章转载自:http: ... -
NSUserDefaults 用户偏好设定保存自定义类(序列化)
2012-04-17 23:30 0NSUserDefaults 用户偏好设定保存 ...
相关推荐
一些NSArray,NSDictionary,NSSet相关的算法知识1
详细的说明和用法 NSArray,NSSet,NSDictionary 以及他们子类的说明
NSArray,NSSet,NSEnumerator,NSDictionary及对应Mutable介绍,有对应的实例代码
这是NSArray,NSSet,NSEnumerator,NSDictionary及对应Mutable介绍的示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7884218
主要介绍了iOS集合遍历(NSArray、NSDictionary、NSSet)的方法,文中给出了详细的方法示例,并总结了各个方法的优缺点来供大家学习参考,需要的朋友们下面来一起看看吧。
【无限互联】iOS开发视频教程课件第四章Foundation框架课件是由无限互联提供的iOS开发视频教程对应的课件,课件非常详细的讲解了Foudation框架里面的...包括: NSNumber、NSString、NSArray、NSDictionary和NSSet等。
它为您提供了一组关于NSArray,NSDictionary和NSSet的扩展方法,以帮助您处理常见的操作(通常以功能方式)。 文献资料 完整文档可在获得。 使用CocoaPods安装 是Swift和Objective-C Cocoa项目的依赖项管理器。 您...
Objective C数组的内存布局要了解NSArray,NSSet,NSDictionary这些集合类的使法,我们需要先弄明其对应的内存布局(Memory L
支持的基础集合包括: NSArray , NSDictionary , NSOrderedSet和NSSet 。功能运算符过滤循环访问集合,并返回包含满足条件的元素的数组。 NSArray < NSNumber> *filteredArray = [@[ @ 13 , @ 42 , @ 0 ] fbl_...
NSArray NSBundle NSData NSDate NSDictionary NSException NSFileManager NSObject NSSet NSString NSTimer NSURL UIKit UIBezierPath UIButton UIColor UIDevice UIImage UIImageView UILable ...
NSDictionary,NSArray,NSSet和NSString尚未隐式转换为Dictionary,Array,Set和String。 为了在这些类型之间进行转换,我们已向这些类型临时添加了一个协议,以允许对其进行转换。 有一种方法称为bridge() 。 ...
文档中包括: NSNumber,NSArray,NSString,NSDictionary,NSSet 属性以及相关方法
## MCSCollectionUtility NSArray , NSSet和NSDictionary类别提供了许多使收集操作更容易和更干净的方法。 ##安装要在您的应用程序中使用MCSCollectionUtility,只需将“ Classes”文件夹拖到yout项目中,然后...
这是iPhone常用集合类介绍的示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7884218
但是,苹果对于我们的NSDictionary、NSSet、NSArray等值有中文时,打印出来的是Unicode编码,人类无法直接读懂,因此,笔者研究研究如何将打印出来的日志保持原有的格式化且能够将Unicode编码打印出来是正常人类可读...
该库使Objective-C集合对象(例如NSDictionary , NSArray , NSSet和NSOrderSet )返回类似于JSON和Unicode的字符串。 在Xcode控制台中使用NSLog()或与LLDB或GDB进行交互以打印集合对象时,这将很有帮助。 如果您...
MACollectionUtilities MACollectionUtilities是可可收集类中一组... DICT(a, b, c, d) -创建一个NSDictionary ,其中包含NSDictionary a -> b和c -> d 。 请注意, DICT使用IMO更为明智的键/值顺序,而不是可可的标准
Software engineering,programming language,operating system,iOS,OS,iPhone,iPad objective c,cocoa touch,...NSDate,NSArray,NSMutableArray,NSMutableDictionary,NSSet,NSOrderedSet,NSMutableSet,