开发者

NSMuteablearray null when using Global

I have setup a basic app which pulls down a JSON stream and displays info in two view controllers. I have it mostly working with the storage arrays based in the root controller which although works isn't best practice. I decide开发者_如何学God to move the data model to a separate class which is then alloced in the app delegate (I could use singletons but the app is basic and im just learning).

So country class is

@interface Country : NSObject {

NSString *countryName;
NSString *countryDetail;
NSMutableArray *countryList;
UIImage *countryFlag;

}

@property (nonatomic, retain) NSString *countryName;
@property (nonatomic, retain) NSString *countryDetail;
@property (nonatomic, retain) UIImage *countryFlag;
@property (nonatomic, retain) NSMutableArray *countryList;

and each of the properties is synthized in the .m

In the appdelegate .h

@class RootController;
@class Country;

@interface FO_InfoAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navController;
RootController *viewController;
Country *myCountry;
} 

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet RootController *viewController;
@property (nonatomic, retain) IBOutlet UINavigationController *navController;
@property (nonatomic,retain) Country *myCountry;

And then in the .m

#import "Country.h"

@synthesize myCountry;

myCountry = [[Country alloc] init];

The Global is then shared in the classes needed using #define

  #define FOAppDelegate ((FO_InfoAppDelegate *)[[UIApplication sharedApplication] delegate])

I have two parsing classes which handle two different url json feeds. Same source different data.

Im my original configuration I have a method in the parseclass.h called from the rootcontroller.

- (void)querycountrieslistwithViewCont:(UIViewController *)controller;

Then in the implementation I do some parsing of the JSON and pass the data to an NSmuteablearray which originally was declared and alloced in the Root Controller This was how it was working. The resulting array fed into UITableview

NSArray *countries = [jparser objectWithString:fco_content error:&error];
for (NSDictionary *country in countries) {
 [viewController.countryName addObject:[country valueForKeyPath:@"country.slug"]]

Now in my new design with the datamodel I want the parse class to store the info in the country class so I change the last line to :

[FOAppDelegate.myCountry.countryList addObject:[country valueForKeyPath:@"country.slug"]];

This results in the array being listed as null!? They are both NSMuteablearray just accessed from a different place. Im a little puzzled!


Put in logging to work out what's going wrong at the point of failure.

So just before this line:

[FOAppDelegate.myCountry.countryList addObject:[country valueForKeyPath:@"country.slug"]];

Insert this:

NSLog(@" FOAppDelegate.myCountry = %@", FOAppDelegate.myCountry);
NSLog(@" FOAppDelegate.myCountry.countryList = %@", FOAppDelegate.myCountry.countryList);
NSLog(@" country = %@", country);
NSLog(@" country's valueForKeyPath = %@", [country valueForKeyPath:@"country.slug"]);

Then run it, check the console, and look for things being nil that shouldn't be.

Your problem may just be that you haven't alloc'd and init'd your countryList NSMutableArray.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜