在swift中内容丰富

时间:2016-07-30 09:12:57

标签: swift contentful

任何人都可以帮助这个最基本的快速内容示例吗?只是尝试从Contentful(工作)获取响应,然后将生成的实体映射到我的自定义结构。

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 个答案:

答案 0 :(得分:0)

client.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的所有代码。