donglang1894 2011-12-02 17:06
浏览 46
已采纳

objective-c NSArrays的array_key_exists()和array_search()等价物是什么?

  1. I have this bug in Obj-C that is caused because the program is trying to access a non-existing NSArray key.
  2. Typically in my PHP programs I verify that a key exists before using it using array_key_exists(NEEDLE, HAYSTACK).
  3. Is there an equivalent in Cocoa / Obj-C for these two functions, and if not, how would you go about creating these.
  4. I am more interested in a solution for array_key_exists() than in array_search().
  • 写回答

5条回答 默认 最新

  • dou91736 2011-12-02 17:13
    关注

    In an NSArray, keys can only be integers. So you can simply check whether the index you are trying to access is less than the number of items in the array. You can check the number of items via the count method:

    NSArray *array = ... some array ... 
    NSUInteger index = ... some index ...
    
    if (index < [array count]) {
         // It's safe to do [array objectAtIndex:index]
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab