Objective C 中的数组 (iPhone)

发布于 2024-09-07 19:52:05 字数 337 浏览 2 评论 0原文

我想问一下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶气 2024-09-14 19:52:05

您应该使用名为 NSArrayNSMutableArray 的类。它们与 std::vector 类似,但由于 Objective-C 的工作方式,它们的使用有些不同。

// Immutable array (must be created with contents specified)
NSArray *strings = [NSArray arrayWithObjects:@"Hello", @", world!", nil];

// Mutable array (can be modified)
NSMutableArray *strings = [NSMutableArray array];
[strings addObject:@"Hello"];
[strings addObject:@", world!"];

与 Objective-C NSArray 对象的区别在于,它们可以采用任何类型的对象,只要它是 NSObject 的子类即可。

NSArray *array = [NSArray arrayWithObjects:@"A string", [NSNumber numberWithInt:1337], [NSDictionary dictionary]];

要访问值,您必须使用objectAtIndex: 消息。

NSString *aString = [array objectAtIndex:10];
NSLog(aString);

You should use the class named NSArray or NSMutableArray. These are similar to std::vector however they are somewhat different in their use due to the way Objective-C works.

// Immutable array (must be created with contents specified)
NSArray *strings = [NSArray arrayWithObjects:@"Hello", @", world!", nil];

// Mutable array (can be modified)
NSMutableArray *strings = [NSMutableArray array];
[strings addObject:@"Hello"];
[strings addObject:@", world!"];

The difference with Objective-C NSArray objects is that they can take any type of object as long as it is a subclass of NSObject.

NSArray *array = [NSArray arrayWithObjects:@"A string", [NSNumber numberWithInt:1337], [NSDictionary dictionary]];

To access values you must use the objectAtIndex: message.

NSString *aString = [array objectAtIndex:10];
NSLog(aString);
东走西顾 2024-09-14 19:52:05

使用 NSMutableArray。如果 arrB 是 NSArray:

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

[arrA addObjectsFromArray: arrB];

以下是文档: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. If arrB is a NSArray:

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

[arrA addObjectsFromArray: arrB];

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文