将多个 NSObject 添加到 NSMutableArray

发布于 2024-12-22 04:12:31 字数 808 浏览 3 评论 0 原文

使用以下代码,数组中的两项相同(最后一项)。我做错了什么导致该数组覆盖这些值?我正在尝试使用 1 个对象,这样我就不必实例化 X 个对象。

self.myArray = [[NSMutableArray alloc] init];

MyObjClass *obj = [[MyObjClass alloc] init];
obj.firstName = @"First Name";
obj.lastName = @"Last Name";
obj.created = @"Dec 17 16:24";
[self.myArray addObject:obj];

obj.firstName = @"First Name2";
obj.lastName = @"Last Name2";
obj.created = @"Dec 18 7:41";
[self.myArray addObject:obj];

MyObjClass.h 中,我有 @interface MyObjClass : NSObjectNSObject 是错误的数据类型吗?

MyObjClass.h 中的属性:

@property (strong) NSString *firstName;
@property (strong) NSString *lastName;

以及来自 MyObjClass.m 的属性:

@synthesize firstName, lastName;

With the following code, both items in the array are the same (the last item). What am I doing wrong that is causing this array to overwrite the values? I'm trying to use 1 object so I don't have to instantiate X number of objects.

self.myArray = [[NSMutableArray alloc] init];

MyObjClass *obj = [[MyObjClass alloc] init];
obj.firstName = @"First Name";
obj.lastName = @"Last Name";
obj.created = @"Dec 17 16:24";
[self.myArray addObject:obj];

obj.firstName = @"First Name2";
obj.lastName = @"Last Name2";
obj.created = @"Dec 18 7:41";
[self.myArray addObject:obj];

In MyObjClass.h I have @interface MyObjClass : NSObject. Is NSObject the wrong datatype?

Properties in MyObjClass.h:

@property (strong) NSString *firstName;
@property (strong) NSString *lastName;

And from MyObjClass.m:

@synthesize firstName, lastName;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

〗斷ホ乔殘χμё〖 2024-12-29 04:12:32

您需要为添加的每个对象创建一个唯一的实例。此外,obj 需要在此方法中释放,因为将其添加到数组中会保留它。但是,如果您使用 ARC,则不需要/允许释放。

self.myArray = [[NSMutableArray alloc] init];

MyObjClass *obj;

obj = [[[MyObjClass alloc] init] autorelease];
obj.firstName = @"First Name";
obj.lastName = @"Last Name";
obj.created = @"Dec 17 16:24";
[self.myArray addObject:obj];

obj = [[[MyObjClass alloc] init] autorelease];
obj.firstName = @"First Name2";
obj.lastName = @"Last Name2";
obj.created = @"Dec 18 7:41";
[self.myArray addObject:obj];

You need to create a unique instance for each object that is added. Also the obj needs to be released in this method because adding it to the array retains it. However if you are using ARC no releasing is needed/allowed.

self.myArray = [[NSMutableArray alloc] init];

MyObjClass *obj;

obj = [[[MyObjClass alloc] init] autorelease];
obj.firstName = @"First Name";
obj.lastName = @"Last Name";
obj.created = @"Dec 17 16:24";
[self.myArray addObject:obj];

obj = [[[MyObjClass alloc] init] autorelease];
obj.firstName = @"First Name2";
obj.lastName = @"Last Name2";
obj.created = @"Dec 18 7:41";
[self.myArray addObject:obj];
姜生凉生 2024-12-29 04:12:32

您通常误解了 -addObject: 方法和对象。您正在向数组添加对 MyObjClass 实例的引用(指针)。如果您添加同一个实例两次,则没有什么区别。此外,由于您只存储指向数组中对象的指针,因此数组中始终拥有对象的最新“版本”。

您需要分配两个实例并将它们都添加:

MyObjClass *object1 = ...;
[array addObject:object1];
MyObjClass *object2 = ...;
[array addObject:object2];

You are misunderstanding the -addObject: method and objects in general. You are adding a reference (pointer) to your instance of MyObjClass to the array. If you add the same instance twice, it doesn't make a difference. Also, as you only store a pointer to the object in the array, you always have the latest "version" of your object in the array.

You need to allocate two instances and add them both:

MyObjClass *object1 = ...;
[array addObject:object1];
MyObjClass *object2 = ...;
[array addObject:object2];
聆听风音 2024-12-29 04:12:32

将对象添加到 NSMutableArray 时,它们会被保留,而不是复制。如果您不想修改包含的对象,请使用以下代码序列:

self.myArray = [[NSMutableArray alloc] init];

MyObjClass *obj = [[MyObjClass alloc] init];
obj.firstName = @"First Name";
obj.lastName = @"Last Name";
obj.created = @"Dec 17 16:24";
[self.myArray addObject:[[obj copy] autorelease]];

obj.firstName = @"First Name2";
obj.lastName = @"Last Name2";
obj.created = @"Dec 18 7:41";
[self.myArray addObject:obj]; // if you're using it further then you need to make a copy of it again

When adding objects to NSMutableArray, they are retained, not copied. If you don't want to modify your included object, use this sequence of code:

self.myArray = [[NSMutableArray alloc] init];

MyObjClass *obj = [[MyObjClass alloc] init];
obj.firstName = @"First Name";
obj.lastName = @"Last Name";
obj.created = @"Dec 17 16:24";
[self.myArray addObject:[[obj copy] autorelease]];

obj.firstName = @"First Name2";
obj.lastName = @"Last Name2";
obj.created = @"Dec 18 7:41";
[self.myArray addObject:obj]; // if you're using it further then you need to make a copy of it again
梦境 2024-12-29 04:12:32

尝试在 MyObjClass 类中设置属性,如下所示:

@property (copy) NSString *firstName;
@property (copy) NSString *lastName;
@property (copy) NSString *created;

Try set the properties in your MyObjClass class like this:

@property (copy) NSString *firstName;
@property (copy) NSString *lastName;
@property (copy) NSString *created;
桃扇骨 2024-12-29 04:12:31

数组不会覆盖值,您在代码中。您有一个 MyObjClass 实例。 *obj 是指向该对象的指针,当您将其添加到数组两次时,该数组有两个索引指向您添加两次的对象。

通过设置 obj 的属性,您可以更改 *obj 指针和数组所指向的那个对象的值。

为什么你关心实例化 X 对象?听起来您想要数组中具有不同值的 X 个对象。

除了创建 X 对象之外,您还可以复制第一个对象,设置值,然后将其添加到数组中,但由于您无论如何都设置了所有值,我不确定为什么您不初始化一个新对象。

编辑:

根据您下面的评论,看来您对多个对象的关注是围绕内存管理的。当您将对象添加到数组时,它会保留该对象,因此在您添加它之后(如果您在该范围内完成了该对象),然后释放或自动释放它。当数组被释放时,它会对数组中的所有对象调用release。无论您初始化还是复制,您都需要 n 个对象 - 您仍然有多个。释放它们,然后在释放时让数组释放。

The array isn't overwriting the values, you are in your code. You have one instance of the MyObjClass. *obj is a pointer to that objects and when you add it to the array twice, the array has two indexes that point to the object you added twice.

By setting the properties on obj, you're changing the values of that one object that both your *obj pointer and the array points to.

Why are you concerned about instantiating X objects? It sound like you want X objects which are in the array with distinct values.

Besides creating X objects, you can copy the first object, set the values then add that to the array but since you're setting all the values anyways, I'm not sure why you just wouldn't init a new object.

EDIT:

Based on your comment below, it looks like your concern of multiple objects is around memory management. When you add an object to an array, it retains the object so after you add it (if you're done with it in that scope), then release or autorelease it. When the array is released, it will call release on all the objects in the array. You need n objects whether you init or copy - you still have multiple. Release them and then let the array release when it's released.

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