0

我想从我的 void 方法中获取 json 字符串,让我们看看

-(void) getIDfromMarket{}

我想从 getIDfromMarket 显示结果 json 并且它已经成功了,现在我想将它传递给

-(void)viewDidLoad

怎么做?这是我的代码

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self getIDfromMarket];
     NSLog(@"ID==",ID);
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//////get global market From JSOn
-(void) getIDfromMarket{
    NSError *error = nil;
    ///set params url to get market localization
    NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://someurl"]];
    if (jsonData) {
    id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    NSArray *Market = [jsonObjects objectForKey:@"market"];
        NSLog(@"%@", Market);
        if(Market==NULL){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oh Snap!" message:@"Can't Get Market ID " delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];

        }
    }
    else{

        NSLog(@"Error Fetch Market ID");
 }

}

@end

我不想登录NSLog(@"%@", Market);viewDidLoad,怎么办?

4

3 回答 3

2
-(NSArray *) getIDfromMarket{
    NSError *error = nil;
    ///set params url to get market localization
    NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://someurl"]];
    if (jsonData) {
        id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        NSArray *Market = [jsonObjects objectForKey:@"market"];
        NSLog(@"%@", Market);
        if(Market==nil){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oh Snap!" message:@"Can't Get Market ID " delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
            return nil;
        } else {
            return Market;
        }
    }
    else{

        NSLog(@"Error Fetch Market ID");
        return nil;
 }

}
于 2012-10-08T07:36:46.260 回答
1

如果市场是成员变量,那么您可以登录 viewDidLoad。或返回值-(id)getIDfromMarket;

-(id) getIDfromMarket{
    // your code
    return market; // autorelase
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *lArray  = [self getIDfromMarket];// autorelase
    NSLog(@"ID==",lArray);

}
于 2012-10-08T07:29:18.963 回答
0

如果您希望能够使用从 JSON 请求返回的结果,您应该将 getIDFromMarket 返回类型从 (void) 更改为 (NSArray),然后在方法结束时,如果请求成功,则键入“return Market”,如果它失败返回零。

然后从 -(void) viewWillApper 调用类似 NSArray *thisArray = [getIDFromMarket]; 的方法

从这里您将能够使用数组中的内容并在视图加载之前在屏幕上设置数据

另一种选择是拥有 NSArray 的属性,当返回结果时,您会将其存储到此属性中,以后您可以从该类中的任何方法访问该属性。

希望这可以帮助

//////get global market From JSOn
-(NSArray) getIDfromMarket{
    NSError *error = nil;
    ///set params url to get market localization
    NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://someurl"]];
    if (jsonData) {
    id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    NSArray *Market = [jsonObjects objectForKey:@"market"];
        NSLog(@"%@", Market);
        if(Market==NULL){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oh Snap!" message:@"Can't Get Market ID " delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
            return nil;
        }
        else {
            return Market;
        }
    }
    else{
        NSLog(@"Error Fetch Market ID");
        return nil;
 }

}

然后在视图中会出现

- (void)viewWillAppear
{
    [super viewWillAppear];

    NSArray *results = [self getIDfromMarket];
     NSLog(@"ID==%@",results);
}
于 2012-10-08T07:25:24.957 回答