2016-07-30 80 views
0

任何人都可以帮助这个最基本的快速内容的例子?试图从Contentful(working)获得响应,然后将结果实体映射到我的自定义结构。内容丰富,以迅速结构

Contentful swift文档非常缺乏强制您在Git repose和复杂的示例之间跳跃,代码片段仅涉及打印结果。

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

    } 
} 

回答

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是在主线程中执行。