簡體   English   中英

如何在iOS中的另一個類中獲取NSDictionary JSON數據

[英]How to get NSDictionary JSON data in another class in ios

我是iPhone編程的新手。我在ClassA中創建了一個調用normallogin的方法。但是如果在iB中調用此方法,則iam在ClassB中調用此方法,它將與服務器連接並在classA的didFinishLoading方法中從服務器獲取JSON數據,並且我已將其存儲在新的JSON對象。現在我想在classB中獲得新的JSON對象新的NSDictionary對象。

-(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];

我可以想到兩種方法來實現您的要求,但是可能還有更多方法。

1)使用委托

將ObjectB設置為ObjectA的代表。 基本上,它是A的弱實例變量,並且符合協議。 因此,在您的ClassA.h文件中定義(在ClassA的interface-block之前):

@class ClassA;    // The interface of ClassA is defined later, so we need to assure that ClassA exists ("forward declaration").
@protocol ClassADelegate <NSObject>
-(void)objectA:(ClassA*)objectA didFinishLoading:(NSDictionary*)dict;
@end

然后,您可以按以下方式修改代碼(我省略了日志):

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString * returnString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSDictionary *newJSON = [NSJSONSerialization JSONObjectWithData:responseData
                                                        options:0
                                                          error:nil];
    if ([self.delegate respondsToSelector:@selector(objectA:didFinishLoading:)])
    {
        [self.delegate objectA:self didFinishLoading:newJSON];
    }
}

2)使用塊

您可以傳入在connectionDidFinishLoading:執行的代碼塊。 因此,在ClassA中為該塊定義一個屬性:

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

self.didFinishLoadingHandler(newJSON);
self.didFinishLoadingHandler = nil;

didFinishLoading的底部。 這將執行該塊並重置屬性。

normallogin將傳入的塊存儲在屬性中。 在ObjectB中執行以下操作:

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

UPDATE

更改-(void)normallogin; -(void)normallogin:(void(^)(NSDictionary*))didFinishLoadingHandler; 然后在實現中做類似的事情

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

您可以創建一個表示您在解析JSON響應后所獲得的數據的類。 添加一個singleton class ,該singleton class將允許您從任何所需的視圖控制器訪問此類的對象。

您需要創建一個可以為您提供退貨的退貨類型函數。 像這樣

-(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用於數據

可以使用委托來實現。 您的B類必須確認協議為RespondeDelegate。

@Protocol ResponseDelegate<NSObject> 
-(Void)workWithJsonResponse:(NSDictionary *)response;
@end

在ClassA.h中創建如下屬性

@property (nonatomic,strong) id<ResponseDelegate> delegate;

在類Am文件中合成委托屬性

-(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];

在ClassB.h文件中執行此操作

@Interface ClassB:UIViewController<ResponseDelegate>

在ClassB.m文件中處理/實現委托方法

-(Void)workWithJsonResponse:(NSDictionary *)response {
//DO the stuff here
}

希望這對您有用。如有任何問題,請通知我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM