如何在ios中的另一个类中获取NSDictionary JSON数据

如何在ios中的另一个类中获取NSDictionary JSON数据,ios,iphone,objective-c,Ios,Iphone,Objective C,我是iPhone编程新手。我在ClassA中创建了一个方法调用normallogin。但是如果调用此方法,我会在ClassB中调用此方法,它将与服务器连接,并在classA中的didFinishLoading方法中从服务器获取JSON数据,我已将其存储在新的JSON对象中。现在我想在classB newNSDictionary对象中获取新的JSON对象 -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSStr

我是iPhone编程新手。我在ClassA中创建了一个方法调用normallogin。但是如果调用此方法,我会在ClassB中调用此方法,它将与服务器连接,并在classA中的
didFinishLoading
方法中从服务器获取JSON数据,我已将其存储在新的JSON对象中。现在我想在classB new
NSDictionary
对象中获取新的JSON对象

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
 NSString * returnString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"%@returnStringreturnString", returnString);

  NSDictionary *newJSON = [NSJSONSerialization JSONObjectWithData:responseData
                                                            options:0
                                                              error:nil];
  NSLog(@"%@newJSON:---", newJSON);
}
如何在classB中获取新的JSON对象数据,我将使用下面的代码 我正在用下面的代码调用classB中的normallogin方法。一切都很好。但我想在classB中获得新的JSON数据。请告诉我如何获取数据

loginconnectiomm *lconnection =[[loginconnectiomm alloc]init];
 [lconnection normallogin];

您可以创建一个类来表示解析
JSON
responce后获得的数据。
添加一个
singleton类
,该类将允许您从所需的任何视图控制器访问此类对象

您需要创建一个返回类型函数来提供返回。像这样

-(NSDictionary *) normallogin{

NSDictionary *newJSON = [NSJSONSerialization JSONObjectWithData:responseData options:0   error:nil];
 //Note : Here I write down this dictionary only for example .. Please implement your code here and that dictionary you are getting return this . 
return newJSON;

}
loginconnectiomm *lconnection =[[loginconnectiomm alloc]init];
 NSDictionary *returnDic = [lconnection normallogin];
然后调用这个函数,像这样获取返回字典

-(NSDictionary *) normallogin{

NSDictionary *newJSON = [NSJSONSerialization JSONObjectWithData:responseData options:0   error:nil];
 //Note : Here I write down this dictionary only for example .. Please implement your code here and that dictionary you are getting return this . 
return newJSON;

}
loginconnectiomm *lconnection =[[loginconnectiomm alloc]init];
 NSDictionary *returnDic = [lconnection normallogin];

现在您可以使用returnDic获取数据了

我可以想出两种方法来实现您的要求,但可能还有更多方法

1)使用代表

将ObjectB设置为ObjectA的委托。基本上,它是a的弱实例变量,并且符合协议。因此,在ClassA.h文件中定义(在ClassA的接口块之前):

2)使用块

您可以传入在
connectiondFinishLoading:
中执行的代码块。因此,在ClassA中为块定义一个特性:

@property (nonatomic, copy) void(^didFinishLoadingHandler)(NSDictionary*);

didfishload
的底部。这将执行块的命令并重置属性

normallogin
中,将传入的块存储在属性中。在ObjectB中,执行以下操作:

__block NSDictionary* jsonDict = nil;
loginconnectiomm *lconnection =[[loginconnectiomm alloc]init];
[lconnection normallogin:^(NSDictionary* json){
    jsonDict = json;
}];
更新

-(void)normallogin的变更声明
-(void)normallogin:(void(^)(NSDictionary*)didfishloadinghandler。然后在实现中执行以下操作

-(void)normallogin:(void(^)(NSDictionary*))didFinishLoadingHandler
{
    self.didFinishLoadingHandler = didFinishLoadingHandler;
    // Rest of the stuff from your normallogin
    // ...
}

这可以使用委托来实现。您的B类必须向协议确认,如RespondeDelegate

@Protocol ResponseDelegate<NSObject> 
-(Void)workWithJsonResponse:(NSDictionary *)response;
@end
在ClassB.h文件中执行此操作

@Interface ClassB:UIViewController<ResponseDelegate>

希望这对您有用。如果有任何问题,请告诉我。

使用NSUserDefault进行添加和删除,以了解有关委派的信息。这是iOS中常见的设计模式。不要做任何调整,只要花1-2个小时学习它,并将它应用到你的问题上。并且是教程谢谢你提供重播..在这里我可以调用-(NSDictionary*)normalLogin哪个调用你正在服务器中获取数据。。还有一件事,您需要在.m类中声明此函数。感谢您使用我尝试过的块提供replay..Ya,但它在这一行中显示错误[lconnection normallogin:^(NSDictionary*jsson)…错误是这样的没有可见的@interface for声明选择器“normallogin”更新了我的答案以包含该部分;)一方面注意:由于您是iPhone开发的新手(可能是Objective-C),您应该花时间看看编码风格指南(例如,这个:或这个:).每一篇文章都有值得商榷的地方,但大多数文章在一般情况下或在stackoverflow这样的网站上寻求帮助时确实有助于提高可读性。
@property (nonatomic,strong) id<ResponseDelegate> delegate;
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{NSString * returnString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *newJSON = [NSJSONSerialization JSONObjectWithData:responseData
                                                        options:0
                                                          error:nil];
ClassB *obj = [[ClassB alloc] init]; // initialize your obj as per your convenience
obj.delegate = self;
[obj.delegate workWithJsonResponse:newJSON];
@Interface ClassB:UIViewController<ResponseDelegate>
-(Void)workWithJsonResponse:(NSDictionary *)response {
//DO the stuff here
}