所有以相同值结尾的Objective C数组元素 [英] Objective C Array elements all ending up with same values

查看:68
本文介绍了所有以相同值结尾的Objective C数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,道歉,如果这是一个很基本的问题,但是我是Objective C的新手,我无法在所有搜索中找到问题的答案(尽管这很可能是因为我

Firstly, an apology if this is a rather basic question, however I am a newcomer to Objective C and I have not been able to find an answer to my question in all of my searches (although this may well be because I am a novice not quite using the correct terminology while looking for an answer!)

我有一个简单的循环,在其中循环填充一些自定义对象的NSMutableArray。我发现,在循环之后,我有一个数组,其元素似乎都具有相同的值:即在插入(0,1,2,3)之后,我发现我的数组包含(3,3,3,3)

I have a simple loop, inside of which I populate an NSMutableArray with some custom objects. What I find is that after the loop, I have an array whose elements all seem to have the same values: i.e. after inserting (0,1,2,3) I find that my array contains (3,3,3,3).

该问题仅在使用对象时出现;第二个数组(我仅在其中添加字符串)最终填充了期望值。

The problem is only present when using my objects; a second array, into which I add only strings, ends up populated with the expected values.

看一下数组中对象的地址,我发现它们都是不同的:我是否正确地认为这意味着我不只是拥有一个数组?

Looking at the addresses of the objects in my array, I see that they are all different: am I correct in thinking that this means that I do not simply have an array of pointers that reference the same object?

我想了解为什么这不如我预期的那样。我感觉我缺少一些非常基本的东西,任何见解都会受到赞赏。

I am wanting to understand why this is not behaving as I expected. I have a feeling that there is something quite basic that I am missing, and any insights would be greatly appreciated.

这是我执行的循环:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"STARTING");
    NSMutableArray* myArray1 = [NSMutableArray array];
    NSMutableArray* myArray2 = [NSMutableArray array];

    for (int i=0; i < 4; i++) {
        MyObject* obj = [[MyObject alloc] initWithSomeString: [NSString stringWithFormat: @"ID%u", i] 
                                                  AndSomeNum: i];
        [myArray1 addObject: obj];
        [myArray2 addObject: [NSString stringWithFormat: @"ID%u", i]];

        NSLog(@"  Added to array1: %@", [myArray1 objectAtIndex: i]);
        NSLog(@"       and array2: %@", [myArray2 objectAtIndex: i]);
    }

    NSLog(@"ALL DONE:\nARRAY1=%@\nARRAY2=%@", myArray1, myArray2);
}

运行此命令后得到的输出是:

The output that I get after running this is:

STARTING
   Added to array1: num=0 self=<0x928e9b0> str=<0x9290090> str=ID0
        and array2: ID0
   Added to array1: num=1 self=<0x9290080> str=<0x928ff80> str=ID1
        and array2: ID1
   Added to array1: num=2 self=<0x928eb00> str=<0x9290020> str=ID2
        and array2: ID2
   Added to array1: num=3 self=<0x7499a30> str=<0x7499ae0> str=ID3
        and array2: ID3
ALL DONE:
ARRAY1=(
    "num=3 self=<0x928e9b0> str=<0x7499ae0> str=ID3",
    "num=3 self=<0x9290080> str=<0x7499ae0> str=ID3",
    "num=3 self=<0x928eb00> str=<0x7499ae0> str=ID3",
    "num=3 self=<0x7499a30> str=<0x7499ae0> str=ID3"
)
ARRAY2=(
    ID0,
    ID1,
    ID2,
    ID3
)

这是MyObject的实现类:

This is the implementation of the MyObject class:

@implementation MyObject

NSString* _blah;
int _num;

-(MyObject*)initWithSomeString: (NSString*)blah AndSomeNum: (int)num {
    self = [super init];
    _blah = [NSString stringWithString: blah];
    _num = num;
    return self;
}

-(NSString*)description {
    return [NSString stringWithFormat: @"num=%u self=<%p> str=<%p> str=%@", _num, self, _blah, _blah];
}

@end


推荐答案

这里的问题是您对这两个变量的定义:

The issue here is your definition of these two variables:

NSString* _blah;
int _num;

将它们放置在此文件中的位置,将它们定义为全局变量。因此,您的类的每个实例都共享这两个相同的变量。这就是为什么每次调用初始化程序时都会覆盖相同的'_blah'。

Where you have them placed in this file, they are defined as global variables. So every instance of your class is sharing these same two variables. This is why each time you call your initializer, the same '_blah' is being overwritten.

如果您要让它们成为 instance 变量,每个实例都有自己的'_blah'和'_num',那么您需要在类的实现或接口的{}中声明它们。因此,这将作为示例:

If you meant for them to be instance variables, each instance having its own '_blah' and '_num', then you need to declare them within {} in the implementation or interface for your class. So this would work as an example:

@implementation MyObject {

 NSString* _blah;
 int _num;

} // continue with method definitions...

这篇关于所有以相同值结尾的Objective C数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆