NSURLConnectionDataDelegate에 이상한 문제가 있습니다. json의 구문 분석을위한 클래스를 만들었습니다. 다음 코드 : 메서드 connectionDidFinishLoading에서Objective-C JSON 구문 분석이 다른 클래스에서 작동하지 않습니다.
- (void)createJSONDictionaryFromURL:(NSURL *)url
{
NSURLRequest *request = [NSURLRequest requestWithURL:url];
(void)[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
jsonData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[jsonData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Fail with error");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
self.jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
NSLog(@"%@", self.jsonDictionary);
}
은 콘솔이 올바른 데이터를 보여주고, 모든 것을 정확합니다. 그러나이 ViewController에서 viewDidLoad 메서드를 사용하고 싶습니다.
JSONParser *parser = [[JSONParser alloc] init];
NSDictionary *dict = [[NSDictionary alloc] init];
[parser createJSONDictionaryFromURL:url];
dict = parser.jsonDictionary;
NSLog(@"%@", dict);
콘솔에 (null)이 표시됩니다. 아무도 도와 줄 수 있습니까? 죄송합니다
내 나쁜 영어