現在swiftでぐるなびAPIからjsonデータを取得し表示する
アプリを製作しているのですが取得後データが入っていない時の
処理がうまくいきません、よかったらお力添えください。
下の方のif文でpic内のshop_image1が無い場合は
NotFoundを表示したいのですが何故かデータがなくても
elseの方へ進んでしまいます。
おそらくif文の条件がいけないと思うのですがどうすればいいでしょうか。
データが入っていない場合は実行中の辞書の項目は
(_NSDictionaryM *) 0 Key/Value pairs
となっています。
swift
1URLns = NSURL(string:URLstr) 2AccessURL = NSURLRequest(URL:URLns) 3let jsonData :NSData = NSData(contentsOfURL: URLns)! 4 5 do { 6 json = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers) as! NSDictionary 7 print("\(URLstr)") 8 } catch { 9 // エラー処理 10 } 11 12 let rest:NSArray = json.objectForKey("rest") as! NSArray //restdata整理 13 var access:NSDictionary! 14 var pic:NSDictionary! = nil 15 16 17 18 for var i=0 ; i < rest.count ; i++ { 19 print(rest[i].objectForKey("name") as! NSString) 20 access = rest[i].objectForKey("access") as! NSDictionary 21 print("\(access.objectForKey("station") as! NSString)から約\(access.objectForKey("walk") as! NSString)分") 22 pic = rest[i].objectForKey("image_url") as! NSDictionary 23 if pic["shop_image1"]?.isKindOfClass(NSNull) == true { 24 print("NotFound") 25 }else{ 26 //print("\(pic.objectForKey("shop_image1") as! NSString)") 27 } 28
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。