gpt4 book ai didi

ios - 如何快速保存 Json 数据并离线显示

转载 作者:搜寻专家 更新时间:2023-11-01 06:28:31 26 4
gpt4 key购买 nike

我正在尝试保存我的 json 文件并将其显示为离线。我正在尝试这段代码,但它对我不起作用..

let myData = NSKeyedArchiver.archivedData(withRootObject: self.data)
UserDefaults.standard.set(myData, forKey: "userJson")
UserDefaults.standard.synchronize()

谁能建议我更好的保存数据和离线显示的方法?

最佳答案

你不应该将 JSON 保存在 UserDefault 中,而是将其保存在文档目录中的文件中

我已经创建了通用类,可以很容易地做到这一点

//
// OfflineManager.swift
//
//
// Created by Prashant on 01/05/18.
// Copyright © 2018 Prashant. All rights reserved.
//

import UIKit

class OfflineManager: NSObject {

static let sharedManager = OfflineManager()
let LocalServiceCacheDownloadDir = "LocalData"

enum WSCacheKeys {
case CampignList
case CampignDetail(id:String)
case ScreenShotList

var value:String {
switch self {
case .CampignList:
return "CampignList"
case .CampignDetail(let id):
return id
case .ScreenShotList :
return "ScreenShotList"
}

}
}

func getBaseForCacheLocal(with fileName:String) -> String? {

let filePath = FileManager.default.getDocumentPath(forItemName: self.LocalServiceCacheDownloadDir)
if FileManager.default.directoryExists(atPath: filePath) {
return filePath.stringByAppendingPathComponent(fileName)
} else {
if FileManager.default.createDirectory(withFolderName: self.LocalServiceCacheDownloadDir) {
return filePath.stringByAppendingPathComponent(fileName)
}
}
return nil
}



//------------------------------------------------------------

@discardableResult
func cacheDataToLocal<T>(with Object:T,to key:WSCacheKeys) -> Bool {
let success = NSKeyedArchiver.archiveRootObject(Object, toFile: getBaseForCacheLocal(with: key.value)!)
if success {
print( "Local Data Cached\(String(describing: getBaseForCacheLocal(with: key.value)))")
} else {
print("Error")
}

return success

}

//------------------------------------------------------------

func loadCachedDataFromLocal<T>(with key:WSCacheKeys ) -> T? {
return NSKeyedUnarchiver.unarchiveObject(withFile: getBaseForCacheLocal(with: key.value)!) as? T
}


//------------------------------------------------------------


func removeAllCacheDirs () {
do {
try FileManager.default.removeItem(atPath: self.getBaseForCacheLocal(with: "")!)

} catch {
print("error in remove dir \(error.localizedDescription)")
}

}

//--------------------------------------------------------------------------------


}

这里是extension FileManager的一些辅助方法

public var getDocumentDirectoryPath: String {
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
return documentDirectory
}

public func getDocumentPath(forItemName name: String)-> String {
return getDocumentDirectoryPath.stringByAppendingPathComponent(name)
}

public func directoryExists(atPath filePath: String)-> Bool {
var isDir = ObjCBool(true)
return FileManager.default.fileExists(atPath: filePath, isDirectory: &isDir )
}

public func createDirectory(withFolderName name: String)-> Bool {
let finalPath = getDocumentDirectoryPath.stringByAppendingPathComponent(name)
return createDirectory(atPath: finalPath)
}

这是字符串扩展的方法

public func stringByAppendingPathComponent(_ path: String) -> String {
let fileUrl = URL.init(fileURLWithPath: self)
let filePath = fileUrl.appendingPathComponent(path).path
return filePath
}

如何使用?

保存

   OfflineManager.sharedManager.cacheDataToLocal(with: object as! [String:Any], to: .CampignList)

读取数据

    DispatchQueue.global().async {
// GET OFFLINE DATA
if let object:[String:Any] = OfflineManager.sharedManager.loadCachedDataFromLocal(with: .CampignList) {
do {
let data = try JSONSerialization.data(withJSONObject: object, options: [])
let object = try CampaignListResponse.init(data: data)
self.arrCampignList = object.data ?? []
DispatchQueue.main.async {
self.tableVIew.reloadData()
}
} catch {
}
}
}

注意:您可以为您的 json 类型定义您自己的 WSCacheKeys,就像我正在获取一些事件列表一样

关于ios - 如何快速保存 Json 数据并离线显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50597331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com