无法从其他类访问NSArray内容

时间:2012-09-19 16:36:54

标签: objective-c xcode class nsarray

我知道这听起来像是一个愚蠢的问题,很多人现在开始告诉我,我需要阅读关于属性如何工作的苹果文档,搜索类似问题并将其作为重复关闭等等...我做了一些搜索,尝试阅读苹果文档,但仍然无法弄清楚我的代码有什么问题,并会欣赏一些帮助...

我在PrefMySpotsViewCtrl类的.h和.m中定义了NSMutable数组,如下所示:

NSMutableArray *mySpotsArray;

@property (nonatomic, retain) NSMutableArray *mySpotsArray;

@synthesize mySpotsArray;

当我从同一个类中对这个数组进行NSLog时这样:

NSLog(@"in class: %@", mySpotsArray);

一切运作良好。我试图从另一个类NSLog这个数组:

PrefMySpotsViewCtrl *PrefMS = [[PrefMySpotsViewCtrl alloc] init];
NSLog(@"%@", [PrefMS mySpotsArray]);

显示为空。

我做错了什么?

谢谢。

编辑...

我像这样初始化数组:

- (id)init
{
self = [super init];
if (self)
{
    mySpotsArray = [[NSMutableArray alloc]init];
}

return self;
}

添加,删除这样的对象:

/-----------------------------------------
- (IBAction)addSpot:(id)sender
{
[mySpotsArray addObject:[[MySpots alloc]init]];
[mySpotsTable reloadData];

[self saveMySpots];
}




//-----------------------------------------
- (IBAction)deleteSpot:(id)sender
{
NSInteger row = [mySpotsTable selectedRow];
[mySpotsTable abortEditing];
if (row !=-1)
{
    [mySpotsArray removeObjectAtIndex: row];
}

[mySpotsTable reloadData];

[self saveMySpots];
 }

1 个答案:

答案 0 :(得分:2)

此...

PrefMySpotsViewCtrl *PrefMS = [[PrefMySpotsViewCtrl alloc] init];

...创建一个新对象。在调用MySpots之前,它的数组中不会有任何addSpot:个对象。您在数组中可能有PrefMySpotsViewCtrlMySpots对象的事实不会影响您刚刚创建的对象。

相关问题