如何访问B类中A类创建的NSMutableArray

时间:2015-03-10 00:35:38

标签: ios objective-c nsmutablearray nsarray linker-errors

我在一个名为ArraySetup的类中声明了一个NSMutableArray,我需要在AppDelegate application:didFinishLaunchingWithOptions:的文件中填充它。在我的ArraySetup.h中:

#import <Foundation/Foundation.h>

@interface ArraySetup : NSObject
{
    NSMutableArray *places;
}


extern NSMutableArray *places;

@end

(。m只有默认代码) 在我的AppDelegate.m中:

// The ArraySetup.h has been imported
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);


    NSString *placesPath = [[pathList objectAtIndex:0] stringByAppendingPathComponent:@"places.plist"];


    NSArray *placesPLIST = [NSArray arrayWithContentsOfFile:placesPath];

    if (placesPLIST) {
        places = [placesPLIST mutableCopy];
    } else {
    places = [[NSMutableArray alloc] init];
    }
}

代码没有给我任何错误,但是当我在模拟器上运行我的程序时,我收到错误:

Undefined symbols for architecture i386:
  "_places", referenced from:
      -[AppDelegate application:didFinishLaunchingWithOptions:] in AppDelegate.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

好吧,我想我们需要在这里退一步。 您要做的是为名为places的类NSMutableArray中名为ArraySetup的变量赋值。

首先,您需要创建ArraySetup的实例:

ArraySetup *arraySetup = [[ArraySetup alloc] init];

此处的第二个问题是NSMutableArray *places;中的ArraySetup是一个实例变量,无法在ArraySetup之外访问。要将它暴露给其他类,您可以创建如下属性:

@property (nonatomic, copy) NSMutableArray *places

然后你可以像AppDelegate一样设置它的值:

arraySetup.places = [placesPLIST mutableCopy];

我建议您在面向对象编程中阅读有关constructorsencapsulation的更多信息。

祝你好运。