Objective C 中的数组 (iPhone)
我想问一下 Objective C 中的 NSArray。
我想创建一个动态数组,因为我不知道数组的大小。我还想将一些数据插入到数组中。但是,我不知道如何声明、初始化并将元素插入到数组中。
以下是C++版本。 我想创建一个空数组A,并将数组B中的所有数组元素放入A中。
// empty array
string arrA[] = new string();
// put the arrB into arrA
for(int i=0; i < arrB.length(); i++)
arrA[i] = arrB[i];
非常感谢。
I want to ask about the NSArray in objective C.
I want to create a dynamic array as I don't know about the size of the array. And I also want to insert some of the data to the array. However, I don't know how to decl, init and insert the element in to the array.
The following is the C++ version.
I want to create a empty array A and put all the array element in array B to A.
// empty array
string arrA[] = new string();
// put the arrB into arrA
for(int i=0; i < arrB.length(); i++)
arrA[i] = arrB[i];
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用名为
NSArray
或NSMutableArray
的类。它们与std::vector
类似,但由于 Objective-C 的工作方式,它们的使用有些不同。与 Objective-C NSArray 对象的区别在于,它们可以采用任何类型的对象,只要它是 NSObject 的子类即可。
要访问值,您必须使用
objectAtIndex:
消息。You should use the class named
NSArray
orNSMutableArray
. These are similar tostd::vector
however they are somewhat different in their use due to the way Objective-C works.The difference with Objective-C
NSArray
objects is that they can take any type of object as long as it is a subclass ofNSObject
.To access values you must use the
objectAtIndex:
message.使用 NSMutableArray。如果 arrB 是 NSArray:
以下是文档:http://developer.apple.com/mac/library/documentation/Cocoa/Reference /Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableArray/addObjectsFromArray:
Use a
NSMutableArray
. IfarrB
is aNSArray
:Here is the documentation: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableArray/addObjectsFromArray: