1

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!

6
  • @robin That’s the correct syntax for synthesizing a property whose name is different from the corresponding backing instance variable. Commented Jan 29, 2011 at 9:18
  • @ceyquem Why do you declare NSMutableArray *rssItem in your enumeration and repeatedly cast it to NewsRss * inside the loop? You should declare NewsRss *rssItem in your enumeration instead. Commented Jan 29, 2011 at 9:24
  • @ceyquem Paste the code where you initialise newsRssArray. If its count returns zero then the most probable cause is that you haven’t actually allocated/inited the array. Commented Jan 29, 2011 at 9:27
  • @bavarious: good point for the cast in the loop, that was an unchanged old version of the loop to be optimized, thanks Commented Jan 29, 2011 at 11:06
  • @bavarious: i am confused, newRssArray is a property of the AppDelegate, I thought it was not necessary to allocate it. Where (in which function) and how should I initialize it? Commented Jan 29, 2011 at 11:07

2 Answers 2

2

Just glancing over the code you have supplied, I don't see where you initialize newsRssArray. Is this in another place or did you forget to init?

Sign up to request clarification or add additional context in comments.

4 Comments

I agree. use an NSLog to see if _newsRssArray is nil in your log statements.
There were 2 problems in my code: I did not declare _newsRssArray properly in the .h (missing _) and I replaced _newsRssArray by self.newsRssArray in the .m code that does not work. Both self.newsRssArray and _newsRssArray dont work.
Ah, yes. Have to be careful with small details like that :)
you likely do not need to declare _newRssArray in the .h - the newer compiler will handle that for you, the property/synthesize is good enough; you absolutely should alloc/init the value at some point of initialization … [nil count] == 0
0

Just may be you are adding the items to _newsRssArray instead of newsRssArray

1 Comment

There were 2 problems in my code: I did not declare _newsRssArray properly in the .h (missing _) and I replaced _newsRssArray by self.newsRssArray in the .m code that does not work. Both self.newsRssArray and _newsRssArray dont work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.