如何将指向NSObject自定义子类的指针添加到NSMutableArray? [英] How to add pointer to an NSObject custom subclass to an NSMutableArray?

查看:77
本文介绍了如何将指向NSObject自定义子类的指针添加到NSMutableArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于ViewController.m中的以下代码段:

For the following code snippet from ViewController.m:

- (IBAction)buttonAction:(id)sender
{
    CustomButton *button = (CustomButton *)sender;
    button.associatedObject = [self.sourceOfObjects.objectArray lastObject];
    [self.foo.arrayOfObjects addObject:button.associatedObject]; // this does not work?
}

•CustomButton是UIButton的子类,并具有@property(非原子的,牢固的)associatedObject,它是指向NSObject类型的对象的指针.

• CustomButton is subclass of UIButton and has @property (nonatomic, strong) associatedObject that is a pointer to an object of type NSObject.

•sourceOfObjects是MyCustomObject类型(NSObject的子类)自身的@属性(非原子的,强的).

• sourceOfObjects is a @property (nonatomic, strong) of self of type MyCustomObject, a subclass of NSObject.

•objectArray是NSMutableArray类型的sourceOfObjects的@property(非原子的,强壮的).

• objectArray is a @property (nonatomic, strong) of sourceOfObjects of type NSMutableArray.

•foo是MyCustomObject类型的ViewController的@property(非原子的,强壮的),它是NSObject的子类.

• foo is a @property (nonatomic, strong) of the ViewController of type MyCustomObject, a subclass of NSObject.

•arrayOfObjects是NSMutableArray类型的foo的@属性(非原子的,强的).

• arrayOfObjects is a @property (nonatomic, strong) of foo of type NSMutableArray.

问题:为什么我不能将button.associatedObject中的指针添加到self.foo.arrayOfObjects中,所以我在self.foo.arrayOfObjects中有一个指向与button.associatedObject相同的对象的指针?

QUESTION: Any idea why I cannot add the pointer from button.associatedObject to self.foo.arrayOfObjects so that I have a pointer in self.foo.arrayOfObjects that points to the same object as button.associatedObject?

我期望发生的事情:如果我随后要求提供[self.foo.arrayOfObjects lastObject],它应该返回button.associatedObject也指向的对象.

WHAT I EXPECT TO HAPPEN: If I subsequently ask for [self.foo.arrayOfObjects lastObject], it should return the object that button.associatedObject also points to.

实际发生的情况:随后要求self.foo.arrayOfObjects.count返回0.我不认为这是arrayOfObjects初始化的问题,因为我有惰性的实例化.

WHAT ACTUALLY HAPPENS: Subsequently asking for self.foo.arrayOfObjects.count returns 0. I do not believe it is an issue with initialization of arrayOfObjects as I have lazy instantiation in place.

我希望我能准确地表述这个问题,并尽量做到准确. :)

I hope I phrased this question accurately, tried to be precise. :)

推荐答案

我忘记为MyCustomObject类的定义中的self.foo.arrayOfObjects添加惰性实例化. :S这意味着文件MyCustomObject.m中缺少以下行:

I forgot to add lazy instantiation for self.foo.arrayOfObjects within the definition for class MyCustomObject. :S What that means is that within file MyCustomObject.m the following lines were missing:

- (void)setArrayOfObjects:(NSMutableArray *)arrayOfObjects
{
    _arrayOfObjects = arrayOfObjects;
}

- (NSMutableArray *)arrayOfObjects
{
    if (!_arrayOfObjects) _arrayOfObjects = [[NSMutableArray alloc] init];
    return _arrayOfObjects;
}

因为未在MyCustomObject类中延迟实例化arrayOfObjects,所以从ViewController.m中将对象添加到self.foo.arrayOfObjects并没有做任何事情,因为self.foo.arrayOfObjects仍然为null,而将对象添加为null则会得到一个null对象

Because arrayOfObjects was not lazily instantiated within the MyCustomObject class, adding an object to self.foo.arrayOfObjects from within ViewController.m did nothing because self.foo.arrayOfObjects was still null, and adding an object to null procures a null object.

这篇关于如何将指向NSObject自定义子类的指针添加到NSMutableArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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