キリンめも(技術)

記事を書いています #rails #swift #JS #UE4 #unity

obj-c json受け渡し

この方法はあまりおすすめしない。汚染

//jsonで内容取得

// データの取得 
NSBundle *bnd01 = [NSBundle mainBundle]; 
NSString *pth01 = [bnd01 pathForResource:@"q_fim"ofType:@"json"]; 
NSURL *url01 = [NSURL fileURLWithPath:pth01]; 

NSURLRequest *req01 = [NSURLRequest requestWithURL:url01]; 

NSData *dat01 = [NSURLConnection sendSynchronousRequest:req01 
returningResponse:nil error:nil]; 
// JSONオブジェクトの取得 
NSDictionary *cor01 =(NSDictionary *) 
[NSJSONSerialization JSONObjectWithData:dat01 
options:NSJSONReadingMutableContainers 
// 基本的にはこれを選ぶ 
error:nil]; 
_q1.text = cor01[@"fim_q"][0][@"q1_food"]; 
_q3.text = cor01[@"fim_q"][15][@"q4_outerwear"]; 
}



-----------------------------------------
@interface AppDelegate : UIResponder 
{
// グローバル変数
NSString *hoge
}

@property (nonatomic, retain) NSString *hoge;

すると、ViewController.m で参照や設定が可能に。

// グローバル変数に保存
AppDelegate* delegate = [[UIApplication sharedApplication] delegate];