从Objective-C上的void获取价值

时间:2012-10-08 07:11:48

标签: ios objective-c json string return

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

我想在viewDidLoad上不记录NSLog(@"%@", Market);,该怎么做?

3 个答案:

答案 0 :(得分: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;
 }

}

答案 1 :(得分:1)

如果market是成员变量,那么您可以登录viewDidLoad。或者从-(id)getIDfromMarket;

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

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

}

答案 2 :(得分:0)

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

然后从 - (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;
 }

}

然后在viewWillAppear

- (void)viewWillAppear
{
    [super viewWillAppear];

    NSArray *results = [self getIDfromMarket];
     NSLog(@"ID==%@",results);
}