我想從JSON響應中獲取解析對象並在方法中使用,當我通常這樣做時,我可以在此方法後使用parsedObject [self.delegate requestJSONFinishedWithParsedObject:parsedObject];我使用,但現在我想要這個parsedObject在我想要的地方。我的意思是調用一個方法,並在返回中有對象。異步後發送請求objective-c後返回parsedobject IOS
像這樣:
的NSDictionary * parsedObject = [self.delegate responseJsonDictionary]; 在我需要的課程中使用它。
我的問題是,我有一個@property (strong, nonatomic) NSMutableArray *projectsArray;
,我想在使用它之前完整填充此屬性。我正在創建一個包含JSON項目的動態菜單,但是當來自JSON的數據位於self.projectsArray時,我的菜單已經創建並且json的對象沒有出現。
我使用這張幻燈片菜單:https://github.com/andrewroycarter/SlideViewController
我使用這個類來管理所有的請求和它完美的作品。
RequestJSON.m
-(void) requestWithURL:(NSDictionary *)jsonDictionary :(NSString*)myURL :(NSString *)method
{
NSURL *thisURL = [NSURL URLWithString:myURL];
NSString *__jsonString;
NSData *__jsonData;
if([NSJSONSerialization isValidJSONObject:jsonDictionary])
{
__jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil];
__jsonString = [[NSString alloc]initWithData:__jsonData encoding:NSUTF8StringEncoding];
}
// Be sure to properly escape your url string.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:thisURL];
[request setHTTPMethod:method];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"Carlos" forHTTPHeaderField:@"User"];
if([method isEqual: @"POST"]){
[request setValue:[NSString stringWithFormat:@"%d", [__jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: __jsonData];
}
NSLog(@"llega= asdasdasd %@, %@, %@",method,jsonDictionary,myURL);
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//INicializo variable NSMutableData
self.myConnectionData= [NSMutableData data];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSDictionary *myResponse = [[NSDictionary alloc] init];
myResponse=[httpResponse allHeaderFields];
statusCode = [httpResponse statusCode];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.myConnectionData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[self.delegate requestJSONFailed];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *thisError;
NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:myConnectionData options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&thisError];
[self.delegate requestJSONFinishedWithParsedObject:parsedObject];
}
@end
RequestJSON.h
@protocol RequestJSONDelegate;
@interface RequestJSON : NSObject
{
NSInteger statusCode;
}
@property (nonatomic,assign) id <RequestJSONDelegate> delegate;
@property (nonatomic, strong) NSMutableData *myConnectionData;
-(void) requestWithURL:(NSDictionary *) params:(NSString*)myURL :(NSString *)method;
-(void) requestForLogin:(NSString*)myURL :(NSString*)method :(NSString*)pass;
@end
@protocol RequestJSONDelegate
-(void) requestJSONFinishedWithParsedObject:(NSDictionary*)parsedObject;
-(void) requestJSONFailed;
@end
在我想這些數據的其他類我用這個方法
-(void) requestJSONFinishedWithParsedObject:(NSDictionary *)parsedObject{
NSArray *projectsObjectArray = [parsedObject objectForKey:@"projects"];
}
-(void) requestJSONFailed{}
並開始請求我在我的主類中使用了這個。
example.m
RequestJSON *requestJSON = [ [RequestJSON alloc] init];
[requestJSON requestWithURL:(NSDictionary *) params:url :method];
[requestJSON setDelegate:self];
謝謝
嗨馬努,我已經做了你所說的,甚至我已經把更多的[_tableView reloadData]在我的代碼。問題是當我嘗試爲我的部分使用數組時,它仍然是空的。我正在使用這個菜單[鏈接](https://github.com/andrewroycarter/SlideViewController)當我使用projectsArray時,我在MySlideViewController.m中創建了一個新節,但是當我登錄時 - (void)requestJSONFinishedWithParsedObject :(NSDictionary *)parsedObject'self.projectsArray它的完整..謝謝 – roxeman3 2013-05-07 18:09:16
非常奇怪...首先嚐試找到問題,我建議你做一個測試,沒有任何遠程請求,用你的數據初始化你的數組viewController希望能夠正常工作,並在UITableView委託方法中逐步檢查一切正常。之後,你做了檢查你的方法 - (無效)requestJSONFinishedWithParsedObject:(NSDictionary *)parsedObject接收到的數據具有預期的格式 – Manu 2013-05-08 08:34:02
我也這樣做。如果我創建一個數組,它的作品。問題是我收到requestJSONFinishedWithPasedObject方法中的信息,如果我用那裏的信息完成數組,則第一個方法中的數組是空的。問題是我創建之前self.projectArray已滿的部分..甚至重新加載tableview我不能看這個數據。 – roxeman3 2013-05-08 11:04:13