开发者

Method works correctly when called from within its class but not otherwise

I wrote this function in my RootViewController. appRecord is an object holding a开发者_运维技巧n XML attribute.

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

    return appName;
}

I want to use this function again by writing this:

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

NSLOG(appsName);

After I compiled, it didn't return anything, but it works and returns appsName if I use this function inside the RootViewController class ([self getString:0]), so I know there is no problem with the function. When I tried to change return appName to return @"test", it worked and returned something when I accessed it from the other class.

This means there is a problem with this appRecord; it was returned inside the class but returns nothing when I access it from another class.

How to solve this problem?


You are getting your AppRecord from self.entries in the instance of RootViewController record. This means unless your new initializer initializes and populates its entries variable (array by the looks of things) before you call getString:, it won't be able to return anything since that array is empty.

Beyond that, I don't know if you want your getString:(int) method to access a different array, or if the problem is in your initialization of RootViewController (more likely)


Maybe I don't understand your question, but if you are relying on the NSLog statement to see if the function is returning anything, you should change it to:

NSLog(@"%@", appsname);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜