2011-03-09 65 views
0

我已经设置了一个基本的应用程序,它拉下JSON流并在两个视图控制器中显示信息。我主要使用基于根控制器的存储阵列,虽然它并不是最佳实践。我决定将数据模型移动到一个单独的类,然后在应用程序委托中分配(我可以使用单例,但该应用程序是基本的,即时学习)。NSMuteablearray null当使用全球

所以国家类是

@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; 

和每个属性中的.m

在的appdelegate是synthized .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; 

然后在.M

#import "Country.h" 

@synthesize myCountry; 

myCountry = [[Country alloc] init]; 

全球然后在需要的类共享使用#

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

我有处理两个不同的URL JSON的任务是为两个解析类。相同来源的不同数据。

我的原始配置我有一个从根控制器调用parseclass.h中的方法。

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

然后在执行我做JSON的一些分析和数据传递到其最初申报并在根控制器 这是它是如何工作的alloced的NSmuteablearray。结果数组送入UITableView的

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

现在,在我的新设计数据模型我想解析类来存储全国一流的信息让我改变最后一行:

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

这导致数组被列为空!他们都是从不同的地方访问NSMuteablearray。我有点困惑!

+0

你确实在设置countryList吗?你声明它,但不要在任何我能看到的地方分配并初始化一个可变数组。 – occulus 2011-03-09 13:31:29

+0

你有一个叫做countryName的东西声明为NSString,然后你调用addObject:就可以了。他们是不是真的有不同的名字? – occulus 2011-03-09 13:35:05

回答

0

进行日志记录以确定故障点出了什么问题。

所以只是这一行之前:

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

插入此:

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

然后运行它,检查控制台,并认准的事是零,不应该是。

你的问题可能只是你没有alloc'd和init'd你的countryList NSMutableArray。

+0

这就是它是什么样的国家,而不是country.countrylist在appdelegate!非常感谢 – Coasty 2011-03-09 13:56:30