doupeng8494 2012-04-02 09:22
浏览 66
已采纳

对于NSMutableArray的Objective-C中的每个循环

NSMutableArray *output = [[NSMutableArray alloc]init]; 

or

NSArray *output = [[NSMutableArray alloc]init];

I can set keys and values. Now, I just want to access each key and value, but I don't know the number of keys set.

In PHP it is very easy, something as follows:

foreach ($output as $key => $value)

How is it possible in Objective-C?

  • 写回答

4条回答 默认 最新

  • douguazhi5966 2012-04-02 09:31
    关注

    I think you are confused with NSDictionary with NSArray.

    In NSDictionary you can set the object with the keys like

    NSMutableDictionary *output = [[NSMutableDictionary alloc]init]; 
    [yourDictionary setObject:@"yourvalue" forKey:@"yourKey"];
    

    and you can get the object like

    [yourDictionary objectForKey:@"yourKey"];
    

    In your Array it just adds the object in the index.

    NSArray * output = [[NSArray alloc] initWithObjects:@"option1",@"option2", nil];
    

    get the object as

    [output objectAtIndex:1];  // 1 is the index number
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站