Objective-C数组
来源:互联网 发布:端口聚合是什么意思 编辑:程序博客网 时间:2024/05/22 01:50
Objective-C数组相关操作。
// insert code here...
NSLog(@"数组");
//指定多个字符串创建数组
NSArray *array;
array=[NSArrayarrayWithObjects:@"0-asd",@"1-fds",@"2-哈咯",@"3-个人",nil];
//数组的长度
NSLog(@"数组长度%d",array.count);
//通过索引取得对象
for(int i=0;i<array.count;i++)
{
NSString *secondStr=[arrayobjectAtIndex:i];
NSLog(secondStr,nil);
}
//高速枚举法取得对象,Objective-C2.0开始支持,
for(NSString *strin array)
{
NSLog(str,nil);
}
//对象的追加于删除
//创建空数组
NSMutableArray *MutArray=[NSMutableArrayarray];
//追加对象
[MutArrayaddObject:@"A"];
[MutArrayaddObjectsFromArray:array];
//插入对象
NSString *thstr=@"插入值";
[MutArrayinsertObject:thstr atIndex:4];
//替换对象
[MutArrayreplaceObjectAtIndex:2withObject:@"替换"];
//删除所有对象
//[Mutarray removeAllObjects];
//删除最后的对象
[MutArrayremoveLastObject];
//删除索引为Index的对象
[MutArrayremoveObjectAtIndex:0];
//删除所有于object同值的对象
[MutArrayremoveObject:@"0-asd"];
//删除数组中所有与object等价的对象
[MutArrayremoveObjectIdenticalTo:thstr];
//删除数组中所有与数组array包含相同的元素
[MutArrayremoveObjectsInArray:array];
NSLog(@"%@",MutArray);
在iOS 开发过程中, NSArray,NSMutableArray、NSDictionary、 NSMutableDictionary 经常被用到。这里先来介绍NSArray 与 NSMutableArray。
NSArray 和 NSMutableArray 二者有时可任意选用,有时又必区分开来。
首先,NSArray只能存储Objective-C 对象,而不能存储C语言中的基本数据类型,比如:int,flot,指针等。 在使用NSArray时,需特别注意,必须以nil 结尾,以此来代表结束。
举例如下:
NSArray 通过类方法 arrayWithObjects 创建一个新的NSArray。
NSArray * array;
array = [ NSArray arraryWithObjects: @"one", @"two", @"three",nil ] ;
为了查看array 的内容, 可通过NSLog方法,如下:
for(int i=0; i<[array count]; i++ )
{
NSLog (@"index %d has %@", i, [array objectAtIndex:i]);
}
从中可看出,NSArray 创建的是静态数组,一旦创建之后,就再也不能添加和删除数组中的对象了。
NSMutableArray 是 NSArray 补充类。NSMutalbeArray 创建的是动态数组,可随意添加或删除数组中的元素。
也行你会看到 arrayWithCapacity 方法,这个数组容量仅仅是一个参考,不会真的限制数组的大小。 是为代码优化而设计的。
NSMutableArray 使用addObject: 在末尾添加对象, 用removeObjectAtIndex: 来删除指定索引处的对象。对象删除后,被删除对象后面的数组元素被迁移,填补空缺。
这里给出一个实例:
#import <Foundation/Foundation.h>
int main( )
{
NSArray *array = [[NSArray alloc]
initWithObjects: @”red”, @"white”,
@"blue”, nil];
printf(“Array has been created.”);
return 0;
}
这里需特别注意的是:NSArray 的初始化必须以 nil 结束。
另外还需注意,在字符串前加 @ ,这是 数组在 Objective-C 与 C 的区别。
如何访问获取 NSArray 中的对象呢? 通过 NSArray的objectAtIndex: 方法。 改写如下:
#import <Foundation/Foundation.h>
int main( )
{
NSArray *array = [[NSArray alloc]
initWithObjects: @”red”, @”white”,
@”blue”, nil];
printf(“array[0] = %s”, [[array objectAtIndex: 0] cString]);
return 0;
}
输出结果: array[0] = red.
同理,通过 NSArray的 insertObjectAtIndex: 可以很方便地将已有的对象插入数组中的指定位置。
NSDictionary和NSMutableDictionary
//NSDictionary 初始化
NSDictionary *d1 = [NSDictionary dictionaryWithObjectsAndKeys:@"Shanghai", @"Location", @"Programmer", @"Occupation", nil];
NSLog(@"d1: %@", d1);
//NSDictionary 个数
NSLog(@"d1 count: %ld", [d1 count]);
//NSDictionary 枚举keys/values
//NSEnumerator *keys1 = [d1 keyEnumerator];
NSEnumerator *objs1 = [d1 objectEnumerator];
for (NSObject *obj1 in objs1) {
NSLog(@"obj: %@", obj1);
}
//NSDictionary 根据key获取object
NSLog(@"obj: %@", [d1 objectForKey:@"Location"]);
//NSMutableDictionary 初始化 dictionaryWithCapacity, dictionaryWithObjectsAndKeys:<#(id), ...#>, nil
NSMutableDictionary *d2 = [NSMutableDictionary dictionaryWithCapacity:2];
[d2 setObject:@"Shanghai" forKey:@"Location"];
[d2 setObject:@"Programmer" forKey:@"Occupation"];
NSLog(@"d2: %@", d2);
[d2 setObject:@"Male" forKey:@"Gender"];
NSLog(@"d2: %@", d2);
1、初始化字典
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];
2、添加字典
[dictionary setObject:@"One" forKey:@"1"];
[dictionary setObject:@"Two" forKey:@"2"];
[dictionary setObject:@"Three" forKey:@"3"];
[dictionary setObject:@"Four" forKey:@"4"];
3、删除字典
[dictionary removeObjectForKey:@"3"];
//================================================
int,NSInteger,NSUInteger,NSNumber
1.当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。
2.NSUInteger是无符号的,即没有负数,NSInteger是有符号的。
3.有人说既然都有了NSInteger等这些基础类型了为什么还要有NSNumber?它们的功能当然是不同的。
[array addObject:3];//会编译错误
[array addObject:[NSNumber numberWithInt:3]];
Cocoa提供了NSNumber类来包装(即以对象形式实现)基本数据类型。
例如以下创建方法:
+(NSNumber *)numberWithInt: (int)value;
+(NSNumber *)numberWithFloat: (float)value;
+(NSNumber *)numberWithBool: (BOOL) value;
将基本类型数据封装到NSNumber中后,就可以通过下面的实例方法重新获取它:
-(int)intValue;
-(float)floatValue;
-(BOOL)boolValue;
-(NSString *)stringValue;
- Objective-C数组小结
- Objective-C数组
- Objective-C数组
- Objective-C 可变数组
- objective-c 学习 数组
- objective-c 之数组
- Objective-C 数组NSArray
- Objective-C数组详解
- Objective-C 数组
- objective-c之数组
- objective-c C类型数组
- objective-c二维数组 objective-c多维数组 iPhone 开发
- Objective-c 字符串 数组 转换
- NSMutableArray 可变数组-Objective-C
- Objective-C之数组对象
- objective c 字符串处理 ,数组
- Objective - c 创建二维数组
- Objective-C 不可变数组
- linux的free命令详解-内存是拿来用的不是拿来看的
- IP 和 子网掩码 计算
- jquery 动态绑定select
- PHP的C扩展 makefile的修改
- jsp 成员声明和局部生明
- Objective-C数组
- Top 30 Django Tutorials and Articles
- zencart数据库操作函数
- bzoj2668
- cisco command 详解
- .net mvc3 站点配置404错误自定义页面,返回404状态码。
- assumption versus belief
- Java学习第十二天
- memcache的启动参数