1

我目前正在努力尝试缓存数据并检索正在使用 SwiftyJSON 解析的数据。

对于第一种方法,我尝试使用以下函数缓存数据

缓存功能:

func cacheStory(data: SwiftyJSON.JSON){

    let cache = Shared.JSONCache

    if let dataToCache: Haneke.JSON  = data as? Haneke.JSON {

       // print(dataToCache)
        cache.set(value: dataToCache, key: "data")
    }
}

然后将像这样使用此功能

使用缓存功能:

   var storyDataJson = SwiftyJSON.JSON(myresponse)

   self.stories = storyDataJson["stories"]

   self.cacheStory(storyDataJson["stories"])

这实际上并没有做任何事情,我目前收到警告

从“JSON”转换为不相关类型“JSON”总是失败

因此,当尝试使用下面的函数获取这些数据时,应用程序甚至无法构建。

检索功能:

func getCachedStory(key: String) {
    let cache = Shared.JSONCache
    cache.fetch(key: key).onSuccess { data in

        if let d = data {
             print(d)
        } else {
            print("Nowt")
        }
    }
}

不太确定是否可以一起使用这两个库。

4

0 回答 0