2011-03-09 12 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에서 다음

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

그리고 .H AppDelegate에에서하는 .m

에 synthized되어

#import "Country.h" 

@synthesize myCountry; 

myCountry = [[Country alloc] init]; 

그런 다음 #define을 사용하여 필요한 클래스에서 전역을 공유합니다.

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

두 개의 다른 json 피드를 처리하는 두 개의 구문 분석 클래스가 있습니다. 동일한 소스의 다른 데이터.

내 원래 구성 나는 parseclass.h에 rootcontroller에서 호출 된 메소드가 있습니다.

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

그런 다음 구현에 나는 JSON의 몇 가지 분석을하고 원래 선언하고 일하는 방법이이었다 루트 컨트롤러 에 alloced 된 NSmuteablearray에 데이터를 전달합니다.

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

이 :

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

는 이제 데이터 모델 내 새로운 디자인에 내가 구문 분석 클래스 그래서 난 마지막 줄에 변경 국가 클래스의 정보를 저장할

결과 배열은 jQuery과에 공급 배열이 null로 나열되는 결과가 나타납니다. 그들은 둘 다 다른 장소에서 방금 액세스 한 NSMuteablearray입니다. 조금 어이가 혼란스러워!

+0

실제로 countryList를 설정하고 있습니까? 당신은 그것을 선언하지만 어디에도 알 수없는 배열을 할당하고 초기화하지 마십시오. – occulus

+0

당신은 NSString으로 선언 한 countryName이라는 이름을 가지고 있지만 나중에 addObject :를 호출합니다. 그들은 실제로 같은 이름을 갖는 다른 것들입니까? – occulus

답변

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를하지 않았고 countryList NSMutableArray를 초기화했을 것입니다.

+0

그게 정확히 뭔지는 이드가 할당 한 나라지만 나라를 알지 못했습니다! 많은 감사 – Coasty

관련 문제