I have a problem that I can't figure out even after a couple of hours of testing different ways to achieve what I need: Here is the code I have a problem with:
AppDelegate.h
#import <Three20/Three20.h>
#import "NewsRssParser.h"
#import "NewsRss.h"
@class NewsRssParser;
@class NewsRss;
@interface AppDelegate : NSObject <UIApplicationDelegate,NewsRssParserDelegate> {
NewsRssParser * _rssParser;
NSMutableArray * _newsRssArray;
}
@property(readwrite, retain) NewsRssParser * rssParser;
@property(readwrite, retain) NSMutableArray * newsRssArray;
@end
AppDelegate.m
@implementation AppDelegate
@synthesize rssParser = _rssParser;
@synthesize newsRssArray = _newsRssArray;
[...]
- (void)myFunction{
NSMutableArray *dsItems = [[[NSMutableArray alloc] init] autorelease];
for(NSMutableArray *rssItem in self.rssParser.rssItems)
{
NSString * rssItemTitle = [(NewsRss *)rssItem title];
NSString * rssItemAuthor = [@"par " stringByAppendingString:[(NewsRss *)rssItem author]];
NSString * rssItemDescription = [(NewsRss *)rssItem description];
NSString * rssItemLinkUrl = [(NewsRss *)rssItem linkUrl];
NSString * rssItemImageUrl = [(NewsRss *)rssItem mediaUrl];
TTTableMessageItem *tMsgItem = [TTTableMessageItem itemWithTitle:rssItemTitle caption:rssItemAuthor text:rssItemDescription timestamp:nil imageURL:rssItemImageUrl URL:rssItemLinkUrl];
[self.newsRssArray addObject:tMsgItem];
[dsItems addObject:tMsgItem];
}
NSLog(@"AppDelegate/count/rssItems=%d", [self.rssParser.rssItems count]);
NSLog(@"AppDelegate/count/newsRssArray=%d", [self.newsRssArray count]);
NSLog(@"AppDelegate/count/dsItems=%d", [dsItems count]);
}
When I run the code, my RSS parser works properly and I can loop on 10 items in self.rssParser.rssItems. I need to access these items in another view to show them in a table, hence I prepare the data in NSMutableArray * newsRssArray that I have declared in my .h file
However if I try to add those 10 objects in the NSMutableArray * newsRssArray, I have nothing in the end (see the code output in the debugger). I test my code successfully if I use NSMutableArray *dsItems declared inside my function.
Here is gdb output:
Attaching to process 47209.
2011-01-29 14:55:24.813 TestApp[47209:207] AppDelegate/count/rssItems=10
2011-01-29 14:55:24.814 TestApp[47209:207] AppDelegate/count/newsRssArray=0
2011-01-29 14:55:24.815 TestApp[47209:207] AppDelegate/count/dsItems=10
(gdb)
What am I doing wrong?
Thanks for your help!
NSMutableArray *rssItemin your enumeration and repeatedly cast it toNewsRss *inside the loop? You should declareNewsRss *rssItemin your enumeration instead.newsRssArray. If itscountreturns zero then the most probable cause is that you haven’t actually allocated/inited the array.