开发者

How Does NSArray Operate? When compared to Java's ArrayList

Is there anyway to get the equivalents for an NSArray when compared to Java ArrayList?

However, what I'm trying to do (learning Objective-C at the minute) is have a NSArray of 6 integers. Allow myself to 'index' into specific开发者_Go百科 point of these integers and set an integer. Then check if the NSArray is null or zero, if not then loop through and print out integers to some label on screen (example).

Is this possible? The documentation isn't the best at all it seems. Though I am finding Apple's tutorials on general iPhone development extremely useful!


For what you want to do, the main differences between an implementation of java.util.List and NSArray are:

  • Objective-C doesn't have "autoboxing", so you can't just pass a number like 3 to the array and have it store the correct object. You have to explicitly wrap it yourself in an instance of NSNumber, by doing [NSNumber numberWithInt:3]
  • any implementation of List may optionally implement the .add() and .remove() API. In Foundation, there is a subclass of NSArray that allows you to change its content, called NSMutableArray.
  • Objective-C doesn't have generics, so you can't do the equivalent of List<String>

Other than that, List and NSArray are quite similar: each is abstract, telling you how you can use an implementing object but not enforcing the way that object works. For example, an ArrayList is implemented using an array, but NSArray chooses an appropriate implementation dynamically based on the content you try to use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜