2011-03-09 63 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