誰でもこの最も基本的な迅速なContentfulの例を支援できますか? Contentful(working)からの応答を取得し、結果のエンティティをカスタム構造にマップしようとしています。
Contentful swiftのドキュメントには、Gitの休憩と複雑な例の間をジャンプすることを強制するものがほとんどなく、コードスニペットは結果の出力のみを参照しています。
// Create structs that can be decoded from json
import Foundation
import Decodable
struct MyEntity {
let name: String
}
extension MyEntity {
static func decode(j: AnyObject) throws -> MyEntity {
return try Entity(
name: j => "nested" => "name"
)
}
}
// From other class, get the data (working) and then map it to struct objects
let client = Client(spaceIdentifier: "my_identifier", accessToken: "my_access_token")
client.fetchEntries(["content_type": "my_entity"]).1.next { result in
for item in result.items{
// Now what? item is not json so
// how to i convert it to an instance of MyEntity
}
}
回答:
回答№1は0client.fetchEntries(["content_type": "my_entity"]).1.next { result in
result.items.map ({ (item) in
let hello = item.field["hello"] as? String
let world = item.field["world"] as? String
})
}
成功完了ブロックのUIに触れるすべてのコードがメインスレッドで実行されることを確認してください。