标签:blog class code tar ext color
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
#import "RootViewController.h" @interface
RootViewController () @end @implementation
RootViewController { UILabel *txtInteger; UILabel *txtFloat; UILabel *txtDouble; UILabel *txtNSString; UILabel *txtNSDate; UILabel *txtNSArray; UILabel *txtNSDictionary; } |
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 |
- ( void )viewDidLoad { [ super
viewDidLoad]; // Do any additional setup after loading the view. [ self
creatLable]; [ self
creatclearbtn]; [ self
saveNSUserDefaults]; [ self
readNSUserDefaults]; } //保存数据到NSUserDefaults -( void )saveNSUserDefaults { int
myInteger = 1000; float
myFloat = 60.0f; double
myDouble = 30.0; NSString
*myString = @ "我的字符" ; NSDate
*myDate = [ NSDate
date]; NSArray
*myArray = [ NSArray
arrayWithObjects:@ "hello" , @ "world" ,@ "one" ,@ "dream" , nil ]; NSDictionary
*myDictionary = [ NSDictionary
dictionaryWithObjects:[ NSArray
arrayWithObjects:@ "enuo" , @ "20" , nil ] forKeys:[ NSArray
arrayWithObjects:@ "name" , @ "age" , nil ]]; //将上述数据全部存储到NSUserDefaults中 NSUserDefaults
*userDefaults = [ NSUserDefaults
standardUserDefaults]; //存储时,除NSNumber类型使用对应的类型意外,其他的都是使用setObject:forKey: [userDefaults setInteger:myInteger forKey:@ "myInteger" ]; [userDefaults setFloat:myFloat forKey:@ "myFloat" ]; [userDefaults setDouble:myDouble forKey:@ "myDouble" ]; [userDefaults setObject:myString forKey:@ "myString11" ]; [userDefaults setObject:myDate forKey:@ "myDate" ]; [userDefaults setObject:myArray forKey:@ "myArray" ]; [userDefaults setObject:myDictionary forKey:@ "myDictionary" ]; //这里建议同步存储到磁盘中,但是不是必须的 [userDefaults synchronize]; } //从NSUserDefaults中读取数据 -( void )readNSUserDefaults { NSUserDefaults
*userDefaultes = [ NSUserDefaults
standardUserDefaults]; //读取数据到各个label中 //读取整型int类型的数据 NSInteger
myInteger = [userDefaultes integerForKey:@ "myInteger" ]; txtInteger.text = [ NSString
stringWithFormat:@ "整形:%d" ,myInteger]; //读取浮点型float类型的数据 float
myFloat = [userDefaultes floatForKey:@ "myFloat" ]; txtFloat.text = [ NSString
stringWithFormat:@ "浮点型:%f" ,myFloat]; //读取double类型的数据 double
myDouble = [userDefaultes doubleForKey:@ "myDouble" ]; txtDouble.text = [ NSString
stringWithFormat:@ "%f" ,myDouble]; //读取NSString类型的数据 NSString
*myString = [userDefaultes stringForKey:@ "myString11" ]; txtNSString.text = myString; //读取NSDate日期类型的数据 NSDate
*myDate = [userDefaultes valueForKey:@ "myDate" ]; NSDateFormatter
*df = [[ NSDateFormatter
alloc] init]; [df setDateFormat:@ "yyyy-MM-dd HH:mm:ss" ]; txtNSDate.text = [ NSString
stringWithFormat:@ "日期:%@" ,[df stringFromDate:myDate]]; //读取数组NSArray类型的数据 NSArray
*myArray = [userDefaultes arrayForKey:@ "myArray" ]; NSString
*myArrayString = [[ NSString
alloc] init]; for ( NSString
*str in myArray) { NSLog (@ "str= %@" ,str); myArrayString = [ NSString
stringWithFormat:@ "%@ %@" , myArrayString, str]; [myArrayString stringByAppendingString:str]; // [myArrayString stringByAppendingFormat:@"%@",str]; NSLog (@ "myArrayString=%@" ,myArrayString); } txtNSArray.text = myArrayString; //读取字典类型NSDictionary类型的数据 NSDictionary
*myDictionary = [userDefaultes dictionaryForKey:@ "myDictionary" ]; NSString
*myDicString = [ NSString
stringWithFormat:@ "name:%@, age:%d" ,[myDictionary valueForKey:@ "name" ], [[myDictionary valueForKey:@ "age" ] integerValue]]; txtNSDictionary.text = myDicString; } //清除数据UserDefaultes -( void )ClearUserDefaultes { NSString
*appDomain=[[ NSBundle
mainBundle]bundleIdentifier]; [[ NSUserDefaults
standardUserDefaults]removePersistentDomainForName:appDomain]; [ self
readNSUserDefaults]; } //清除按钮 -( void )creatclearbtn { UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame=CGRectMake(100, 400, 100, 30); [btn setTitle:@ "清除"
forState:UIControlStateNormal]; btn.backgroundColor=[UIColor yellowColor]; [ self .view addSubview:btn]; [btn addTarget: self
action: @selector (ClearUserDefaultes) forControlEvents:UIControlEventTouchDown]; } //创建Label -( void )creatLable { txtInteger=[[UILabel alloc]initWithFrame:CGRectMake(0, 40, 200, 30)]; txtInteger.backgroundColor=[UIColor brownColor]; [ self .view addSubview:txtInteger]; txtFloat=[[UILabel alloc]initWithFrame:CGRectMake(0,80, 200, 30)]; txtFloat.backgroundColor=[UIColor brownColor]; [ self .view addSubview:txtFloat]; txtDouble=[[UILabel alloc]initWithFrame:CGRectMake(0, 120, 200, 30)]; txtDouble.backgroundColor=[UIColor brownColor]; [ self .view addSubview:txtDouble]; txtNSString=[[UILabel alloc]initWithFrame:CGRectMake(0, 160, 200, 30)]; txtNSString.backgroundColor=[UIColor brownColor]; [ self .view addSubview:txtNSString]; txtNSDate=[[UILabel alloc]initWithFrame:CGRectMake(0, 200, 200, 30)]; txtNSDate.backgroundColor=[UIColor brownColor]; [ self .view addSubview:txtNSDate]; txtNSArray=[[UILabel alloc]initWithFrame:CGRectMake(0, 240, 200, 30)]; txtNSArray.backgroundColor=[UIColor brownColor]; [ self .view addSubview:txtNSArray]; txtNSDictionary=[[UILabel alloc]initWithFrame:CGRectMake(0, 280, 200, 30)]; txtNSDictionary.backgroundColor=[UIColor brownColor]; [ self .view addSubview:txtNSDictionary]; } |
UserDefaultes 数据存储使用,布布扣,bubuko.com
标签:blog class code tar ext color
原文地址:http://www.cnblogs.com/hl666/p/3709698.html