NSArray是静态的数组,就是它所指向的内容是不可改变的,它指向一段内存区域,一旦初始化,不能通过它对该内存区域的数据进行修改操作,但是它可以读数据。
NSMutableArray是动态的是NSArray的子类,可以对所指向的内存区域内容进行更改,并可以增加数组内容
NSArray和NSmutableArray的第一个数据的下标为0。
********************************************************************************
NSArray
********************************************************************************
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:
@"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil];
NSLog(@"oldArray:%@",oldArray);
NSEnumerator *enumerator;
enumerator = [oldArray objectEnumerator];
id obj;
while(obj = [enumerator nextObject])
{
[newArray addObject: obj];
}
[newArray sortUsingSelector:@selector(compare:)];
NSLog(@"newArray:%@", newArray);
[newArray release];
//从字符串分割到数组- componentsSeparatedByString:
NSString *string = [NSString alloc] initWithString:@"One,Two,Three,Four"];
NSLog(@"string:%@",string);
NSArray *array = [string componentsSeparatedByString:@","];
NSLog(@"array:%@",array);
[string release];
//从数组合并元素到字符串- componentsJoinedByString:
NSArray *array = [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
NSString *string = [array componentsJoinedByString:@","];
NSLog(@"string:%@",string);
//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);
//- (NSEnumerator *)objectEnumerator;从前向后
NSMutableArray *array = [NSMutableArray arrayWithObjects:
@"One",@"Two",@"Three",nil];
NSEnumerator *enumerator;
enumerator = [array objectEnumerator];
id thingie;
while (thingie = [enumerator nextObject]) {
NSLog(@"thingie:%@",thingie);
}
//- (NSEnumerator *)reverseObjectEnumerator;从后向前
NSMutableArray *array = [NSMutableArray arrayWithObjects:
@"One",@"Two",@"Three",nil];
NSEnumerator *enumerator;
enumerator = [array reverseObjectEnumerator];
id object;
while (object = [enumerator nextObject]) {
NSLog(@"object:%@",object);
}
//快速枚举
NSMutableArray *array = [NSMutableArray arrayWithObjects:
@"One",@"Two",@"Three",nil];
for(NSString *string in array)
{
NSLog(@"string:%@",string);
}
//- (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];
//创建
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);
//将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);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *home;
home = @"../Users/";
NSDirectoryEnumerator *direnum;
direnum = [fileManager enumeratorAtPath: home];
NSMutableArray *files = [NSMutableArray alloc] init];
//枚举
NSString *filename;
while (filename = [direnum nextObject]) {
if([filename pathExtension] hasSuffix:@"jpg"]){
[files addObject:filename];
}
}
//快速枚举
for(NSString *filename in direnum)
{
if([filename pathExtension] isEqualToString:@"jpg"]){
[files addObject:filename];
}
}
NSLog(@"files:%@",files);
//枚举
NSEnumerator *filenum;
filenum = [files objectEnumerator];
while (filename = [filenum nextObject]) {
NSLog(@"filename:%@",filename);
}
//快速枚举
for(id object in files)
{
NSLog(@"object:%@",object);
}
for(NSString *arrayString1 in readArray1)
{
//NSLog(@"arrayString1=%@",arrayString1);
NSArray *array1=[arrayString1 componentsSeparatedByString:@"#:)"];
NSString *name1=[array1 objectAtIndex:0];
NSString *page1=[array1 objectAtIndex:1];
[self.array addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:name1,@"name",page1,@"page",nil]];
NSString *fileString3=[NSString stringWithFormat:@"%@#:)%@",
name1,page1];
[self.markName addObject:fileString3];
}
分享到:
相关推荐
这些类包括NSArray、NSSet、NSEnumerator和NSDictionary,以及它们的可变版本NSMutableArray、NSMutableSet和NSMutableDictionary。这些类提供了各种操作来处理对象的集合,如添加、删除、查找等。以下是关于这些类...
- NSMutableArray是NSArray的可变版本,支持插入、删除和替换元素。由于需要维护数组的顺序,这些操作的时间复杂度通常为O(n)。在遍历时删除元素时,推荐倒序遍历以避免索引冲突,如示例代码所示。 3. **...
##### (4) 集合类(NSArray、NSMutableArray、NSDictionary、NSMutableDictionary、NSSet、NSMutableSet) - **NSArray 和 NSMutableArray** - **定义**: - NSArray:不可变数组,一旦创建后不能更改。 - ...
对于可变对象(如NSMutableArray或NSMutableDictionary),`copy`会创建一个新的不可变副本,即使原始对象是可变的。这意味着你不能修改新副本的内容。 2. `mutableCopy`方法: 对于不可变对象,`mutableCopy`会...
根据给定的文件信息,以下是对Objective-C中最经典、最全面的基础库的深入解析,主要聚焦于几个核心类:NSString、NSMutableString、NSArray、NSMutableArray、NSDictionary、NSMutableDictionary以及NSValue。...
常见的集合类包括NSArray、NSMutableArray、NSDictionary、NSMutableDictionary、NSSet和NSMutableSet等。这些类都是Foundation框架的一部分,为Objective-C和Swift开发者提供了强大的数据结构支持。在这个名为...
在iOS中,你可以将NSArray或NSMutableArray转换为JSON字符串。以下是一个Objective-C的例子: ```objc NSArray *array = @[@1, @"two", @{@"key": @"value"}]; NSError *error; NSData *jsonData = ...
- 对于集合类(NSArray, NSMutableArray, NSDictionary, NSMutableDictionary),MJExtension会自动处理其内部的对象或字典的序列化和反序列化。 - 如果模型中包含自定义的子模型,子模型也需要遵循`NSCoding`协议...
NSMutableDictionary *groupedNames = [NSMutableDictionary dictionary]; for (NSString *name in sortedNames) { NSString *firstChar = [name substringToIndex:1].uppercaseString; if (!groupedNames[first...
在iOS开发中,数据结构的使用是至关重要的,特别是数组(NSArray和NSMutableArray)和字典(NSDictionary和NSMutableDictionary)这两种常用的数据容器。然而,在处理这些数据结构时,常常会遇到一些异常情况,例如...
OC中的数组主要分为两种:NSArray和NSMutableArray。NSArray是不可变的,一旦创建,就不能修改元素。而NSMutableArray则是可变的,支持添加、删除和替换元素。它们都实现了NSCopying和NSMutableCopying协议,可以...
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; for (int i = 0; i [resultSet columnCount]; i++) { NSString *key = [resultSet columnNameForIndex:i]; id value = [resultSet ...
4. **读写操作**:Objective-C和Swift中,我们通常使用`NSDictionary`或`NSMutableDictionary`,以及`NSArray`或`NSMutableArray`来操作属性列表。使用`write(to:atomically:)`和`dictionary(withContentsOfFile:)`...
- `NSArray`和`NSMutableArray`分别用于存储固定和可变大小的对象数组。 - `NSSet`和`NSMutableSet`用于存储不重复的对象集合。 - `NSDictionary`和`NSMutableDictionary`则用于存储键值对形式的数据。 **2. User ...
- 对于数组,分类可能会扩展`NSMutableArray`(或`NSArray`),添加一个安全的版本的`objectAtIndex:`方法。这个新方法会先检查索引是否越界,然后安全地返回对应元素或`nil`。 - 对于字典,分类可能会扩展`...
- **数组**:可以使用`NSMutableArray`和`NSArray`来存储和操作对象集合。 - **字典**:可以使用`NSMutableDictionary`和`NSDictionary`来存储键值对。 例如创建一个可变数组和一个可变字典: ```objective-c ...
NSMutableDictionary *appleMapDict = [NSMutableDictionary dictionary]; appleMapDict[@"title"] = @"苹果地图"; [availableMaps addObject:appleMapDict]; // 检查并添加其他地图应用 if ...
#### NSArray与NSMutableArray - **NSArray**:不可变数组,一旦创建其内容就不能被修改。 - **NSMutableArray**:可变数组,允许在运行时添加、删除或替换元素。 #### NSSet与NSMutableSet - **NSSet**:不可变...
NSMutableDictionary *studentDic = [NSMutableDictionary dictionary]; // 解析name节点 NSArray *nameArray = [item elementsForName:@"name"]; GDataXMLElement *nameElement = [nameArray objectAtIndex:0];...