我想从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