ios - 我们如何在另一个类中的数组或字典数据中获取 SBJSON connectionDidFinishLoading 方法

标签 ios swift

我想在另一个类中获取一个类数组数据(即我想在mainview类中获取connectionDidFinishLoading数组数据)

背景类.h

#import <UIKit/UIKit.h>
#import "JSON.h"
@interface BackGroundClass : UIViewController<NSURLConnectionDataDelegate>
@end


BackGroundClass.m

@interface BackGroundClass ()
    {
        NSMutableData * webData;
        NSURLConnection * connection;
        NSMutableArray * array;
    }

    @end

        @implementation BackGroundClass

        - (void)viewDidLoad {

            array = [[NSMutableArray alloc]init];

            NSURL * url  = [NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"];

            NSURLRequest * request = [NSURLRequest requestWithURL:url];

            connection = [NSURLConnection connectionWithRequest:request delegate:self];

            if(connection)
            {
                webData = [[NSMutableData alloc]init];
            }
            [super viewDidLoad];
        }

        - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
            [webData setLength:0];
        }

        - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
            [webData appendData:data];
        }

        - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
            NSLog(@"error is %@",[error localizedDescription]);
        }

       - (void)connectionDidFinishLoading:(NSURLConnection *)connection {


            NSString * allDataDictionbary = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];

            NSDictionary * responseString = [allDataDictionbary JSONValue];

            NSArray * allTweets = [responseString objectForKey:@"loans"];


            for (NSDictionary * obj in allTweets) {

                NSDictionary * act=[obj objectForKey:@"location"];
                NSDictionary * act1 = [ act objectForKey:@"geo"];
                NSString * level = [act1 objectForKey:@"pairs"];

                //NSDictionary * act = [obj objectForKey:@"description"];
                //NSArray * languages = [act objectForKey:@"languages"];
                //NSString * name = [[languages objectAtIndex:obj]objectForKey:@"name"];

               [array addObject:level];
            }

            NSLog(@"act array is %@",array);
        }

        - (void)didReceiveMemoryWarning {
            [super didReceiveMemoryWarning];
        }
        @end


Now connectionDidFinishLoading methods inside  array data i want to get in mainView class how can i get please help me some one else

主视图.h

#导入

@interface ViewController2 : UIViewController

@结束

主视图.m

导入“ViewController2.h”

    @interface ViewController2 ()

    @end

    @implementation ViewController2

    - (void)viewDidLoad {
        [super viewDidLoad];

    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }

    @end

最佳答案

通知

您可以使用 NSNotificationCenter 从您的连接类发送通知。并在主视图 Controller 中向通知添加一个观察者。通过 NSNotification userInfo 字典和通知对象中的对象 arg 传递数据有两种选择。

http://nshipster.com/nsnotification-and-nsnotificationcenter/

委派

创建一个委托(delegate)并将主 VC 指定为委托(delegate)并使用从响应中获得的数据调用委托(delegate)方法

http://www.tutorialspoint.com/ios/ios_delegates.htm

熟悉上述概念后,请查看以下选项

block

http://www.appcoda.com/objective-c-blocks-tutorial/

RAC 信号( ReactiveCocoa )

http://nshipster.com/reactivecocoa/

编辑 - 使用委托(delegate)

BackGroundClass.h

@protocol MyConnectioDelegate <NSObject>
//using id type here, so NSArray or NSDictionary objects can be passed
-(void)didRecieveResponse:(id)aCollection;

@end
@interface BackGroundClass : UIViewController<NSURLConnectionDataDelegate>
@property(weak, nonatomic)id<MyConnectioDelegate>delegate;
@end

BackGroundClass.m

   - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
          ....
         //Once you get the collection.
          [_delegate didRecieveResponse:<theCollection>];
      }

ViewController2.m

@interface ViewController2 ()< MyConnectioDelegate >

    @end
    @implementation ViewController2

    - (void)viewDidLoad {
        [super viewDidLoad];
//Hope you already have the object of BackGroundClass created
_backgroundClassObject.delegate = self;


    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }

    -(void)didRecieveResponse:(id)aCollection{
//You have the collection sent from background class
}


@end

关于ios - 我们如何在另一个类中的数组或字典数据中获取 SBJSON connectionDidFinishLoading 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30951378/

相关文章:

ios - 设备许可的应用不支持应用内购买

ios - iPhone 应用程序 - 是否可以从外部摄像头捕获和录制视频

ios - UITextView InputView UITableViewController

ios - Swift 3.0 map 返回到以前的 MapView

ios - 在 iPad 上加载大于 256MB 的文件时应用程序崩溃

ios - XCode - “' autorelease' 不可用 : not available in ARC mode”

ios - 更改图像属性 UITableView Swift

ios - 在容器 View 中加载 ViewController

swift - Swift 中的地址簿外部更改回调(使用 C 函数指针?)

swift - 在 Swift 中,冒号后面有类型声明有什么作用?