我想从我的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);,该怎么做?

最佳答案

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

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

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

}

关于ios - 从Objective-C的虚无中获取值(value),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12776908/

10-12 03:09