Avatar of SheppardDigital

asked on 

Objective-C, having a method return an array


This should be easy for someone.

In my viewcontroller I'm accessing an object called reader which parses some xml and stores them in an array of post objects. So far this all works because i can loop through the objects and display data about each object in the console.

Im my viewcontroller I'm trying to get the post object to return the array of objects, but no matter what I do nothing seems to be working.

in my view controller I have...

- (void)viewDidLoad
    // Initialise reader object
    Reader *myReader = [[Reader alloc] initWithPath:@"feed.xml"];
    // Get posts from the reader
    self.posts = [[NSArray alloc] initWithArray:[myReader giveMeThePosts]];
    NSLog(@"Post count = %i",[myReader.posts count]);
    for (Post* post in self.posts) {
        NSLog(@"Title=%@", post.title);
    // Release myReader, we're finished with it
    [myReader release];
    [super viewDidLoad];

and here is the giveMeThePosts method that's in the reader object

    // Put posts into a local array
    NSArray *thePosts = [[NSArray alloc] initWithArray:self.posts];
    NSLog(@"The Count: %i",[thePosts count]);
    // Return the array
    return [thePosts autorelease];

and this point it's worth noting that the NSLog bit in the above function doesn't appear at all, as if the function isn't being ran.

Can anyone point me in the direction of where I'm going wrong?

Avatar of undefined
Last Comment
Avatar of SpeedyApocalypse
Flag of Canada image

This may be a silly question, but do you have -(NSArray*)giveMeThePosts declared in the .h of Reader?  Other than that, it looks fine from a glance.  Try add this some where and post the result:

NSLog(@"%@",[myReader giveMeThePosts]);]

Open in new window

Oops  syntax error on that NSLog.  Remove the other square bracket at the end.
Avatar of SheppardDigital


yeh, the method was declared in the header of the Reader object.

I gave up on getting that to work and instead got the init function to return the array, which seemed to work ok.

I think the problem might have been down to memory management and losing the data
Avatar of SpeedyApocalypse
Flag of Canada image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of SheppardDigital


Thanks for the link, I'll have a read.

As you can tell I'm new to this, and the learning curve from things like PHP, javascript and .net is a bit steep.

Back on the subject of returning data from a method, I've just setup an new method that returns a string and everything seems to be working ok. Very strange.
Yah, there is definitely a curve from PHP as you now have to worry about disposing objects and working with their memory.  A little trick I like to do when solving memory issues is run it through Instruments to catch leaks, as well as commenting out all of the releases, to track down what is causing a loss of data.  What is odd in your case though, is the fact that the app did not crash as that will ususally happen if you use an object without the required memory.  Very strange.  I mean, from your code, everything looked great method and returning wise.
Avatar of SheppardDigital


Memory management was the issue, it seemed I was releasing the object before returning it.


iOS is a mobile operating system developed by Apple; it is a derivative of the Mac OS X desktop operating system, with which it shares many (but not all) common frameworks and other components. These include Cocoa Touch, the Mach/Darwin/XNU kernel and code from the Berkeley Software Distribution (BSD). Each iOS application runs in its own secure sandbox to prevent altering other applications, the operating system, or any other data.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo