2013-04-25 2 views
0

iPad 용으로 개발하여 Coordinates.geojson이라고하는 파일을 만들었습니다. 난에서이 값을 retreive 수있는 방법iOS : geojson iOS 액세스

{ 
    "shelf1": { 
     "name": "six.png", 
     "coordinates": [ 
         [ 
         14, 
         25, 
         329, 
         138 
         ], 
         [ 
         14, 
         185, 
         329, 
         138 
         ], 
         [ 
         14, 
         344, 
         158, 
         138 
         ], 
         [ 
         185, 
         344, 
         158, 
         138 
         ], 
         [ 
         14, 
         94, 
         158, 
         138 
         ], 
         [ 
         185, 
         500, 
         158, 
         138 
         ] 
         ] 
    } 
} 

: 나는 여기

JsonDecoder.m

라는 클래스 파일 내에서 액세스하려는 JsonDecoder.m

@implementation JsonDecoder 



- (id)initWithJson 
{ 
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Coordinates" ofType:@"geojson"]; 
    NSData *data = [NSData dataWithContentsOfFile:filePath]; 
    NSError *error; 
    _json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
    return self; 
} 

- (NSArray*) getCoordinatesFromShelf:(NSString *) bookShelfName 
{ 
    NSArray *coordinates = [[[_json objectForKey:@"shelf1"] objectForKey:@"coordinates"]objectAtIndex:1]; 
    for(id i in coordinates) 
     NSLog(@"%@",i); 
    return coordinates; 
} 
@end 

그리고 내 Coordinates.geojson입니다 클래스 파일에서?

감사합니다. 아이폰 OS에서

답변

0

를 해결.

은 위의 내가 geojson 파일에 대한 내 프로젝트에 대상 회원을 설정하는 것을 잊었다했다 완벽

했다.

는 이렇게하는 jsonfile 표시의 fileinspector을 클릭하고 "대상 회원"

감사에서 프로젝트의 체크 박스를 켜고 끌합니다.

1

는> = 5는 외부 라이브러리

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Coordinates" ofType:@"geojson"]; 
NSData *data = [NSData dataWithContentsOfFile:filePath]; 
NSError *error; 
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

없이 분석 할 수 그러나 파일의 내용이 유효한 JSON 문자열이 아닙니다.

같은 가능한 변화를 경우

{ 
    "shelf1": { 
        "name": "six.png", 
        "coordinates": [ 
            [ 
                14, 
                25, 
                329, 
                138 
            ], 
            [ 
                14, 
                185, 
                329, 
                138 
            ], 
            [ 
                14, 
                344, 
                158, 
                138 
            ], 
            [ 
                185, 
                344, 
                158, 
                138 
            ], 
            [ 
                14, 
                94, 
                158, 
                138 
            ], 
            [ 
                185, 
                500, 
                158, 
                138 
            ] 
        ] 
    } 
} 

그런 다음 당신이 그것을 액세스 할 수 있습니다

NSDictionary *shelf1 = [json objectForKey:@"shelf1"]; 

//OR 

NSArray *coordinates = [[json objectForKey:@"shelf1"] objectForKey:@"coordinates"]; 
+0

그래서 내 편집을 참조하십시오. – Eyeball

+0

2013-04-26 06 : 51 : 24.515 skattkistan [1580 : c07] *** 캐치되지 않은 예외 'NSInvalidArgumentException'으로 인해 앱이 종료 됨, 이유 : '데이터 매개 변수가 있습니다. 없음 ' *** – Eyeball