objective-c数组使用小结

使用数组对象创建的数组功能非常强大,在Java语言或者C语言中定义的数组必须满足数组中的每一个元素必须是同样的类型。
而Objective-C语言可以在Array数组中放任意类型的objective-c的对象。有两点值得注意,一是只能放指向这个对象的指针,而不能直接放int ,char,double 等等;二是不能放nil。

使用NSArray关键字创建一个不可变的数组,一旦初始化完毕后这个数组的元素是不可以再动态地添加和删除。

[array count] : 得到这个对象数组的长度。
[array objectAtIndex:0]: 传入数组脚标的id 得到数据对象。
[array arrayWithObjects:xx,xx,xx,nil] :向数组对象初始化赋值。这里可以写任意对象的指针,结尾必须使用nil,标识为数组定义结束。

 

+ (id)array WithObjects :(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)

 

 

 

    NSArray *arr=[NSArray arrayWithObjects:@"one xiaowang",@"two xiaoli",@"three xiaotian",nil];

    NSLog(@"arr count = %d ,%@",[arr count],[arr objectAtIndex:0]);

 

   
遍历数组中对象的方法有两种,一种称为普通遍历;另一种称为快速枚举遍历。
但apple认为前者效率低下且安全性不高,推荐使用后者即快速枚举法。

普通遍历方法

    for (int i=0;i<[arr count];i++)
    {
        NSLog(@"普通遍历:i = %d 时的数组对象为: %@",i,[arr objectAtIndex: i]);
    }

 

快速枚举遍历方法

 

   for (NSObject *object in arr) {
        NSLog(@"快速枚举遍历数组对象为: %@",object);
    }

 

 

 

动态可变数组的管理方式和不可变数组类似。

 

NSMutableArray * arr=[NSMutableArray arrayWithCapacity:10];

    [arr addObject:@"xiaoli"];
    [arr addObject:@"xiaowang"];

    [arr addObject:@"xiaotian"];

    NSLog(@"arr count = %d ,%@",[arr count],[arr objectAtIndex:0]);

    for (int i=0;i<[arr count];i++)
    {
        NSLog(@"普通遍历:i = %d 时的数组对象为: %@",i,[arr objectAtIndex: i]);
    }

    [arr removeObjectAtIndex:1];

    for (NSObject *object in arr) {
        NSLog(@"快速枚举遍历数组对象为: %@",object);
    }

 

 

 

 

字典类的对象是使用方式同数组也类似

 

主要的方法为

+ dictionaryWithObjectsAndKeys: (id)firstObject, ...;
- (int)count;
- (id)objectForKey:(id)key;
- (NSArray *)allKeys;
- (NSArray *)allValues;

+ (NSMutableDictionary *)dictionary;
- (void)setObject:(id)anObject forKey:(id)key;
- (void)removeObjectForKey:(id)key;
- (void)removeAllObjects;
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;

 

    NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:@"name1",@"key1",@"name2",@"key2",nil];

    for (NSString * str_key in [dict allKeys])
    {
        NSLog(@"key is %@",str_key);
    }

    for (NSString * str_val in [dict allValues])
    {
        NSLog(@"value is %@",str_val);
    }

      NSLog(@"value is %@ at %@",[dict objectForKey:@"key1"],@"key1");


    NSMutableDictionary *dict2=[NSMutableDictionary dictionaryWithCapacity:10];

    [dict2 setObject:@"name1" forKey:@"key1"];

    NSLog(@"dict2 value is %@ at %@",[dict2 objectForKey:@"key2"],@"key2");

    [dict2 setObject:@"name2" forKey:@"key2"];
    NSLog(@"dict2 value is %@ at %@",[dict2 objectForKey:@"key2"],@"key2");
 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值