SheppardDigital
asked on
Objective-C, having a method return an array
Hi,
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
-(NSArray*)giveMeThePosts
{
// 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?
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
-(NSArray*)giveMeThePosts
{
// 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?
Oops syntax error on that NSLog. Remove the other square bracket at the end.
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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.
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.
ASKER
Memory management was the issue, it seemed I was releasing the object before returning it.
Thanks
Thanks
Open in new window