【问题标题】:Contentful to struct in swift满足于快速构建
【发布时间】:2016-10-11 17:25:46
【问题描述】:

谁能帮助这个最基本的快速内容示例?只是试图从 Contentful (working) 获得响应,然后将生成的实体映射到我的自定义结构。

内容丰富的 swift 文档非常缺乏,迫使您在 Git 休息和复杂示例之间跳转,并且代码 sn-ps 仅指打印结果。

// 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

    }
}  

【问题讨论】:

    标签: swift contentful


    【解决方案1】:
    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 的代码都在主线程上执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      相关资源
      最近更新 更多