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
}
私はまったく動作しないいくつかの添字を、してみてくださいされています。この問題を解決するには?前もって感謝します。
スウィフト3ディクショナリネイティブの値の型は 'AnyObject'ではなく' Any'でなければなりません。 BTW Dictionaryは 'value(forKeyPath:)'メソッドを持っていません –
@LeoDabus AnyとAnyObjectはちょっと混乱します。この場合AnyObjectを使用すると機能しませんか? – Sebastian
String、Float、Doubleは構造体で、AnyObjectはすべてのクラスが暗黙的に準拠する(strucではなく)プロトコルです。 AnyObjectを使用すると、Double、Float、IntのすべてがNSNumberに変換されます –