// // UserDefaultsUtils.swift // MTP2_iOS // // Created by zhongyuan on 2018/6/10. // Copyright © 2018年 zhongyuan.All rights reserved. // import Foundation enum UserDefaultsType: String { case isOpened case loginPhone case noticeRead case reSearchGoods case addressInfo } /// UserDefault相关工具类 class UserDefaultsUtils { // MARK: - 生命周期相关 static func isFirstOpen() -> Bool { if let isOpened = UserDefaults.standard.value(forKey: UserDefaultsType.isOpened.rawValue) as? Bool { return !isOpened } else { return true } } static func setOpened() { UserDefaults.standard.set(true, forKey: UserDefaultsType.isOpened.rawValue) } /// 获取地址信息 /// - Returns: Bool static func addressInfo() -> MoAddress? { if let jsonString = UserDefaults.standard.value(forKey: UserDefaultsType.addressInfo.rawValue) as? String, let address = MoAddress.deserialize(from: jsonString) { return address } else { return nil } } /// 存储地址信息 /// - Parameter address: address static func setAddressInfo(_ address: MoAddress) { UserDefaults.standard.set(address.toJSONString() ?? "", forKey: UserDefaultsType.addressInfo.rawValue) } /// 设置通知未读标志 /// /// - Parameter ids: ids static func setUnReasNoticeIds(_ ids: [String]) { UserDefaults.standard.setValue(ids, forKey: UserDefaultsType.noticeRead.rawValue) UserDefaults.standard.synchronize() } /// 获取未读标志 /// /// - Returns: [String] static func getUnReadNoticeIds() -> [String] { let ids = UserDefaults.standard.value(forKey: UserDefaultsType.noticeRead.rawValue) ?? [] return ids as! [String] } /// 获取上次登录的手机号码 /// /// - Returns: String static func getLoginPhone() -> String? { return UserDefaults.standard.value(forKey: UserDefaultsType.loginPhone.rawValue) as? String } /// 设置登录手机号信息 /// /// - Parameter phone: 手机号码 static func setLoginPhone(_ phone: String) { UserDefaults.standard.setValue(phone, forKey: UserDefaultsType.loginPhone.rawValue) UserDefaults.standard.synchronize() } /// 设置已搜索的商品数据信息 /// - Parameter result: goodsId static func setResearchGoodsInfo(_ result: String) { var results = UserDefaultsUtils.getResearchGoodsInfo() if !results.contains(result) { results.append(result) } UserDefaults.standard.setValue(results, forKey: UserDefaultsType.reSearchGoods.rawValue) UserDefaults.standard.synchronize() } /// 获取之前搜索的商品数据信息 static func getResearchGoodsInfo() -> [String] { return (UserDefaults.standard.value(forKey: UserDefaultsType.reSearchGoods.rawValue) as? [String]) ?? [] } /// 删除所有的搜索记录 static func deleteAllResearchGoodsInfo() { UserDefaults.standard.setValue([], forKey: UserDefaultsType.reSearchGoods.rawValue) UserDefaults.standard.synchronize() } } protocol UserDefaultsSettable { static var name: String { get } associatedtype defaultKeys: RawRepresentable } extension UserDefaultsSettable where defaultKeys.RawValue == String { static func set(value: String?, forKey key: defaultKeys) { let aKey = name + key.rawValue UserDefaults.standard.set(value, forKey: aKey) } static func string(forKey key: defaultKeys) -> String? { let aKey = name + key.rawValue return UserDefaults.standard.string(forKey: aKey) } } extension UserDefaults { /// 商品更新时间戳 struct goodsUpdateTime: UserDefaultsSettable { static var name: String = "goodsUpdateTime" enum defaultKeys: String { case actual } } /// 错误码更新时间戳 struct errorcodeUTime: UserDefaultsSettable { static var name: String = "errorcodeUTime" enum defaultKeys: String { case actual } } }