【问题标题】:Add multiple Structs to one UserDefaults entity将多个结构添加到一个 UserDefaults 实体
【发布时间】:2023-02-03 14:06:26
【问题描述】:

我有这样的UserDefaults

fileprivate enum userdefaultKeys: String {
    userSearchHistory = "userSearchHistory",
}

extension UserDefaults {
    static func getUserSearchHistory() -> SearchHistory? {
        let data = self.standard.data(forKey: userdefaultKeys.userSearchHistory.rawValue)
        return SearchHistory.decode(json: data)
    }
    
    static func setUserSearchHistory(userSearchHistory: SearchHistory?) {
        guard let json: Any = userSearchHistory?.json else { return }
        self.standard.set(json, forKey: userdefaultKeys.userSearchHistory.rawValue)
    }
}

我将这些数据保存到 UserDefaults


struct SearchHistory: Codable {
  let type: SearchHistoryEnum
  let name: String
  let corpNo: String
  let storeNo: String
  let long: Double
  let lat: Double
}

enum SearchHistoryEnum: Codable {
  case storeSearch
  case jsonSearch
}


let historySearch = SearchHistory(type: SearchHistoryEnum.storeSearch, name: store?.storename ?? "", corpNo: store?.corpno ?? "", storeNo: store?.storeno ?? "", long: longtitude, lat: latitude)
UserDefaults.setUserSearchHistory(userSearchHistory: historySearch)

这没关系,但它在时间里只保存了一个SearchHistory的实例。我想要最多 5 个。当第 6 个实例出现时,我想删除最旧的一个

【问题讨论】:

    标签: swift


    【解决方案1】:

    首先你的枚举没有编译,你可能是说

    fileprivate enum UserdefaultKeys: String {
       case userSearchHistory
    }
    

    没有必要指定原始值。


    其次,我强烈建议不要与框架作对并遵守 UserDefaults 模式来重载 getter 和 setter。我不知道你解码和编码的特殊方法,这是一个简单的标准实现 JSONDecoderJSONEncoder

    extension UserDefaults {
        func searchHistory(forKey key: String = UserdefaultKeys.userSearchHistory.rawValue) -> SearchHistory? {
            guard let data = data(forKey: key) else { return nil }
            return try? JSONDecoder().decode(SearchHistory.self, from: data)
        }
        
        func searchHistory(forKey key: String = UserdefaultKeys.userSearchHistory.rawValue) -> [SearchHistory]? {
            guard let data = data(forKey: key) else { return nil }
            return try? JSONDecoder().decode([SearchHistory].self, from: data)
        }
        
        func set(_ value: SearchHistory, forKey key: String = UserdefaultKeys.userSearchHistory.rawValue) {
            guard let data = try? JSONEncoder().encode(value) else { return }
            set(data, forKey: key)
        }
        
        func set(_ value: [SearchHistory], forKey key: String = UserdefaultKeys.userSearchHistory.rawValue) {
            guard let data = try? JSONEncoder().encode(value) else { return }
            set(data, forKey: key)
        }
    }  
    

    好处是单个项目或数组的语法始终相同

    let historySearch = SearchHistory(type: SearchHistoryEnum.storeSearch, name: store?.storename ?? "", corpNo: store?.corpno ?? "", storeNo: store?.storeno ?? "", long: longtitude, lat: latitude)
    UserDefaults.standard.set(historySearch)
    

    或者

    UserDefaults.standard.set([historySearch])
    

    删除最早的代码放在写入数据的方法中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多