2017-05-12 17 views
0

私はこのプロジェクトでコアデータを処理しています。 jsonファイルからデータを取得し、特定のデータをコアデータに取得する必要があります。しかし、私はタイプキャストに立ち往生します。JSON "Any"は 'NSDictionary'に可逆ではありません。キャストタイプの決定方法

[![do { 
    let][1]][1] jsonDict = try JSONSerialization.jsonObject(with: jsonData!, options: .allowFragments) as! NSDictionary 

    let jsonArray = jsonDict.value(forKeyPath: "response.venues") as! NSArray 

    for jsonDictionary: NSDictionary in jsonArray { 

    let venueName = jsonDictionary["name"] as? String 
    let venuePhone = (jsonDictionary as AnyObject).value(forKeyPath: "contact.phone") as? String 
    let specialCount = (jsonDictionary as AnyObject).value(forKeyPath: "specials.count") as? NSNumber 

    let locationDict = jsonDictionary["location"] as! NSDictionary 
    let priceDict = jsonDictionary["price"] as! NSDictionary 
    let statsDict = jsonDictionary["stats"] as! NSDictionary 

    let location = Location(entity: locationEntity!, insertInto: coreDataStack.context) 
    location.address = locationDict["address"] as? String 
    location.city = locationDict["city"] as? String 
    location.state = locationDict["state"] as? String 
    location.zipcode = locationDict["postalCode"] as? String 
    location.distance = locationDict["distance"] as? NSNumber 

    let category = Category(entity: categoryEntity!, insertInto: coreDataStack.context) 

    let priceInfo = PriceInfo(entity: priceEntity!, insertInto: coreDataStack.context) 
    priceInfo.priceCategory = priceDict["currency"] as? String 

    let stats = Stats(entity: statsEntity!, insertInto: coreDataStack.context) 
    stats.checkinsCount = statsDict["checkinsCount"] as? NSNumber 
    stats.usersCount = statsDict["userCount"] as? NSNumber 
    stats.tipCount = statsDict["tipCount"] as? NSNumber 

    let venue = Venue(entity: venueEntity!, insertInto: coreDataStack.context) 
    venue.name = venueName 
    venue.phone = venuePhone 
    venue.specialCount = specialCount 
    venue.location = location 
    venue.category = category 
    venue.priceInfo = priceInfo 
    venue.stats = stats 
    } 

enter image description here

そして、私のJSONツリーはここにもある:enter image description here

私はまったく動作しないいくつかの添字を、してみてくださいされています。この問題を解決するには?前もって感謝します。

答えて

1

1)あなたは、おそらくにこのようresponse

からスウィフト3venuesを抽出する必要があります)NSDictionary

2の代わりに[Any]代わりのNSArray[String:Any]を使用してみてください:

do { 
     let jsonDict = try JSONSerialization.jsonObject(with: jsonData!, options: .allowFragments) as! [String:Any] 
     let jsonResponseArray = jsonDict["response"] as! [Any] 
     let jsonVenueArray = jsonResponseArray["venues"] as! [String:Any] 
     for venue: [String:Any] in jsonVenueArray { 
      ... 
     } 
} catch { 
    print("Error:", error) 
} 
+0

スウィフト3ディクショナリネイティブの値の型は 'AnyObject'ではなく' Any'でなければなりません。 BTW Dictionaryは 'value(forKeyPath:)'メソッドを持っていません –

+0

@LeoDabus AnyとAnyObjectはちょっと混乱します。この場合AnyObjectを使用すると機能しませんか? – Sebastian

+0

String、Float、Doubleは構造体で、AnyObjectはすべてのクラスが暗黙的に準拠する(strucではなく)プロトコルです。 AnyObjectを使用すると、Double、Float、IntのすべてがNSNumberに変換されます –

関連する問題