swift UserDefaults

本文详细介绍了Swift中如何使用UserDefaults进行数据存储,包括创建、获取、设置和删除数据的方法,以及注册域、持久化域和易失性域的管理。还提到了监听UserDefaults变化的通知和数据大小限制。
摘要由CSDN通过智能技术生成

使用 UserDefaults类存储数据是最简单的方法,但最好只存储少量数据。UserDefault类可以存储、读取、删除数据。

创建,返回一个默认对象
var userDefault:UserDefaults=UserDefaults.standard

获取 Getting Default Values
func object(forKey: String) -> Any?
Returns the object associated with the specified key.
func url(forKey: String) -> URL?
Returns the URL associated with the specified key.
func array(forKey: String) -> [Any]?
Returns the array associated with the specified key.
func dictionary(forKey: String) -> [String : Any]?
Returns the dictionary object associated with the specified key.
func string(forKey: String) -> String?
Returns the string associated with the specified key.
func stringArray(forKey: String) -> [String]?
Returns the array of strings associated with the specified key.
func data(forKey: String) -> Data?
Returns the data object associated with the specified key.
func bool(forKey: String) -> Bool
Returns the Boolean value associated with the specified key.
func integer(forKey: String) -> Int
Returns the integer value associated with the specified key.
func float(forKey: String) -> Float
Returns the float value associated with the specified key.
func double(forKey: String) -> Double
Returns the double value associated with the specified key.
func dictionaryRepresentation() -> [String : Any]
Returns a dictionary that contains a union of all key-value pairs in the domains in the search list.

例:
var str:String=ud.string(forKey: “aa”)!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值