2016-07-30 84 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是在主線程中執行。