The method works correctly when called from its class, but not otherwise

I wrote this function in my RootViewController . appRecord is an object that contains the 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 compilation, it did not return anything, but it works and returns appsName if I use this function inside the RootViewController class [self getString:0] ), so I know that 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 another class.

This means that 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?

+4
source share
2 answers

You get your AppRecord from self.entries in an instance of the RootViewController record . This means that if your new initializer initializes and populates its entries variable (an array with a look of things) before calling getString: it will not be able to return anything, since this array is empty.

Also, I don't know if you want your getString:(int) method to access another array, or if the problem is in your initialization of the RootViewController (rather)

+5
source

I may not understand your question, but if you rely on the NSLog statement to see if the function returns anything, you should change it to:

 NSLog(@"%@", appsname); 
+1
source

All Articles