1
2
3
4
5
6
7
8
9
10
11
12
|
class
func
get_uuid() ->
String
{
var
userid =
NSUserDefaults
.standardUserDefaults().stringForKey(
"hangge"
)
if
(userid !=
nil
){
return
userid!
}
else
{
var
uuid_ref =
CFUUIDCreate
(
nil
)
var
uuid_string_ref =
CFUUIDCreateString
(
nil
, uuid_ref)
var
uuid:
String
=
NSString
(format: uuid_string_ref)
NSUserDefaults
.standardUserDefaults().setObject(uuid, forKey:
"hangge"
)
return
uuid
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
var
userDefault =
NSUserDefaults
.standardUserDefaults()
//AnyObject
userDefault.setObject(
"hangge.com"
, forKey:
"Object"
)
var
objectValue:
AnyObject
? = userDefault.objectForKey(
"Object"
)
//Int类型
userDefault.setInteger(12345, forKey:
"Int"
)
var
intValue = userDefault.integerForKey(
"Int"
)
//Float类型
userDefault.setFloat(3.2, forKey:
"Float"
)
var
floatValue = userDefault.floatForKey(
"Float"
)
//Double类型
userDefault.setDouble(5.2240, forKey:
"Double"
)
var
doubleValue = userDefault.doubleForKey(
"Double"
)
//Bool类型
userDefault.setBool(
true
, forKey:
"Bool"
)
var
boolValue = userDefault.boolForKey(
"Bool"
)
//NSURL类型
userDefault.setURL(
NSURL
(string:
"http://hangge.com"
)!, forKey:
"NSURL"
)
var
urlValue = userDefault.
URLForKey
(
"NSURL"
)
//NSString类型
userDefault.setObject(
"hangge.com"
, forKey:
"NSString"
)
var
nsStringValue = userDefault.objectForKey(
"NSString"
)
as
!
NSString
//NSNumber类型
var
number:
NSNumber
=
NSNumber
(int:22)
userDefault.setObject(number, forKey:
"NSNumber"
)
number = userDefault.objectForKey(
"NSNumber"
)
as
!
NSNumber
//NSArray类型
var
array:
NSArray
=
NSArray
(array: [
"123"
,
"456"
])
userDefault.setObject(array, forKey:
"NSArray"
)
//NSDictionaryy类型
var
dictionary:
NSDictionary
=
NSDictionary
(dictionary: [
"1"
:
"hangge.com"
])
userDefault.setObject(dictionary, forKey:
"NSDictionary"
)
dictionary = userDefault.objectForKey(
"NSDictionary"
)
as
!
NSDictionary
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
var
userDefault =
NSUserDefaults
.standardUserDefaults()
//UIImage对象存储
//将对象转换成NSData流
var
image =
UIImage
(named:
"apple.png"
)
var
imageData:
NSData
=
NSKeyedArchiver
.archivedDataWithRootObject(image!)
//存储NSData对象
userDefault.setObject(imageData, forKey:
"imageData"
)
//UIImage对象读取
//获取NSData
var
objData:
NSData
= userDefault.objectForKey(
"imageData"
)
as
!
NSData
//还原对象
var
myImage =
NSKeyedUnarchiver
.unarchiveObjectWithData(objData)
as
!
UIImage
println
(myImage)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
var
userDefault =
NSUserDefaults
.standardUserDefaults()
//自定义对象存储
var
model =
UserInfo
(name:
"航歌"
, phone:
"3525"
)
//实例对象转换成NSData
var
modelData:
NSData
=
NSKeyedArchiver
.archivedDataWithRootObject(model)
//存储NSData对象
userDefault.setObject(modelData, forKey:
"myModel"
)
//自定义对象读取
var
myModelData = userDefault.objectForKey(
"myModel"
)
as
!
NSData
var
myModel =
NSKeyedUnarchiver
.unarchiveObjectWithData(myModelData)
as
!
UserInfo
//----- 自定义对象类 -----
class
UserInfo
:
NSObject
{
var
name:
String
var
phone:
String
//构造方法
init
(name:
String
=
""
,phone:
String
=
""
){
self
.name = name
self
.phone = phone
super
.
init
()
}
//从nsobject解析回来
init
(coder aDecoder:
NSCoder
!){
self
.name=aDecoder.decodeObjectForKey(
"Name"
)
as
!
String
self
.phone=aDecoder.decodeObjectForKey(
"Phone"
)
as
!
String
}
//编码成object
func
encodeWithCoder(aCoder:
NSCoder
!){
aCoder.encodeObject(name,forKey:
"Name"
)
aCoder.encodeObject(phone,forKey:
"Phone"
)
}
}
|
Swift - 使用NSUserDefaults来进行本地数据存储