从其类中调用时,方法可以正常工作,但不是

zac*_*ual 4 cocoa-touch objective-c instantiation ios

我写了这个函数RootViewController.appRecord是一个包含XML属性的对象.

- (NSString*) getString:(int)row{
    AppRecord *appRecord = [self.entries objectAtIndex:row];
    NSString *appName = appRecord.appName;

    return appName;
}
Run Code Online (Sandbox Code Playgroud)

我想通过写这个来再次使用这个函数:

RootViewController *record = [RootViewController new]; 
NSString *appsName = [record getString:0];

NSLOG(appsName);
Run Code Online (Sandbox Code Playgroud)

编译之后,它没有返回任何内容,但是appsName如果我在RootViewControllerclass([self getString:0])中使用这个函数它会工作并返回,所以我知道函数没有问题.当我尝试更改return appNamereturn @"test",当我从另一个类访问它时,它工作并返回了一些东西.

这意味着这有问题appRecord; 它是在类中返回的,但是当我从另一个类访问它时没有返回任何内容.

如何解决这个问题呢?

Jes*_*her 5

你是从让你AppRecord self.entries RootViewController的实例record.这意味着除非你的new初始化程序entries在你调用之前初始化并填充它的变量(数组的外观)getString:,否则它将无法返回任何内容,因为该数组为空.

除此之外,我不知道您是否希望您的getString:(int)方法访问不同的数组,或者问题是否在您的初始化中RootViewController(更有可能)