这就是我获取条目的方式:
func fetchAllEntriesByLocation(latitude:Double, longitude: Double, completion: @escaping (([Item]) ->Void)) {
var items:[Item] = []
let query = Query(where: "content_type", .equals("item")).where("fields.location", .isNear(Location(latitude: latitude, longitude: longitude)))
client.fetchEntries(with: query) { [weak self] result in
guard let strongSelf = self else { return }
DispatchQueue.main.async {
strongSelf.delegate?.finishedTask()
}
switch result {
case .success(let entries):
entries.items.forEach { entry in
items.append(Item(entry: entry))
}
DispatchQueue.main.async {
completion(items)
}
case .error(let error):
print(error)
}
}
}
以及我如何绘制它们
final class Item: EntryModellable {
static let contentTypeId: String = "item"
let name: String
let location: Location
let address: String
let open: String
let desc: String
let phone: String
let url: String
let thumbUrl: Asset
init(entry: Entry) {
self.name = entry.fields["name"] as? String ?? ""
self.location = entry.fields["location"] as? Location ?? Location(latitude: 52.23, longitude: 20.9)
self.open = entry.fields["open"] as? String ?? ""
self.address = entry.fields["address"] as? String ?? ""
self.desc = entry.fields["opis"] as? String ?? ""
self.phone = entry.fields["phone"] as? String ?? ""
self.url = entry.fields["www"] as? String ?? ""
self.thumbUrl = entry.fields["thumb"] as? Asset
}
}
当我试图映射到
Asset
类型时,就会出现问题。虽然当我打印我的条目时,我看到“thumb”键,变量的类型是Contentful.Link.asset(Contentful.Asset)
。因此,我不能将其转换为资产类型。我只想实现的是存储图像Url,以便以后与翠鸟或类似的库一起使用以加载图像。
存储图像以访问其url的正确方法是什么?
编辑:
我尝试了下面提供的解决方案,现在得到以下运行时错误:
错误是:
致命错误:在展开可选值时意外找到nil
最佳答案
看起来您使用的是较旧版本的SDK。contentful.swift SDK现在位于version 1.0.1位置,强烈建议升级到带来API稳定性的最新版本,以及使用EntryDecodable
协议将数据映射到自己类型的不同接口。它利用了Swift 4的Decodable
协议,因此您可以使用标准lib+SDK提供的一些助手扩展方法来控制JSON如何映射到您的类型!
使用contentful.swift 1.0.1,EntryDecodable
协议将正确链接所有类型和资产,而不会创建重复项。您的类将如下所示(注意,如果您有Contentful中不需要的属性(因此可以在响应中省略),那么应该使用decodeIfPresent
来返回可选的。
final class Item: EntryDecodable, ResourceQueryable {
static let contentTypeId: String = "item"
let sys: Sys
let name: String
let location: Location
let address: String
let open: String
let desc: String
let phone: String
let url: String
// Links must be optional variables as they are resolved after the initializer exits
var thumbUrl: Asset?
public required init(from decoder: Decoder) throws {
sys = try decoder.sys()
let fields = try decoder.contentfulFieldsContainer(keyedBy: Item.Fields.self)
name = try fields.decode(String.self, forKey: .name)
desc = try fields.decode(String.self, forKey: .desc)
location = try? fields.decodeIfPresent(Location.self, forKey: .location) ?? Location(latitude: 52.23, longitude: 20.9)
address = try fields.decode(String.self, forKey: .address)
open = try fields.decode(String.self, forKey: .open)
phone try fields.decode(String.self, forKey: .phone)
url = try fields.decode(String.self, forKey: .url)
// Here is how you resolve your Asset.
try fields.resolveLink(forKey: .thumbUrl, decoder: decoder) { [weak self] linkedAsset in
self?.thumbUrl = linkedAsset as? Asset
}
}
// If your Contentful field names differ from your property names, define the mapping here.
enum Fields: String, CodingKey {
case name, location, open, address, phone
case url = "www"
case desc = "opis"
case thumbUrl = "thumb"
}
}
现在,您需要更新查询:
let query = QueryOn<Item>.where(field: .location, .isNear(Location(latitude: latitude, longitude: longitude)))
client.fetchMappedEntries(matching: query) { [weak self] (result: Result<MappedArrayResponse<Item>>) in
// Handle your data here, the thumbUrl field will contain a resolved asset for your entries (if that relationship exists in Contentful)
switch result {
case .success(let arrayResponse):
if let firstItem = arrayResponse.items.first {
print(firstItem.thumburl?.urlString)
}
case .error(let error):
print(error)
}
}
关于ios - 如何在Swift中从Contentful访问图像?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49112108/