Objective-C NSUserDefaults 无法存储我的信息
Objective-C NSUserDefaults cannot store my info
我多次尝试使用 NSUserDefaults
来存储我的信息,但都失败了。不知道怎么解决
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (tableView == mySearchController.searchResultsTableView)
{
//BMKPoiInfo* info = poiResultArray[indexPath.row];
NSString *nn = poiResultArray[indexPath.row];
//dw
NSUserDefaults *userDef =[NSUserDefaults standardUserDefaults];
[userDef setInteger:1 forKey:@"poiPosition"];
// [userDef setObject:@"aa" forKey:@"poiPosition"];
[userDef synchronize];
[_selectBtn setTitle:nn/*info.name*/ forState:UIControlStateNormal];
NSInteger nu = [userDef integerForKey:@"poiPosition"];
这些是我的代码,但是 "nu" 没有结果,你可以看到我有
NSUserDefaults *userDef =[NSUserDefaults standardUserDefaults];
[userDef setInteger:1 forKey:@"poiPosition"];
// [userDef setObject:@"aa" forKey:@"poiPosition"];
[userDef synchronize];
为什么"nu"什么都没有?
您可以使用 -[NSUserDefaults setInteger:forKey:]
存储值并使用 -[NSUserDefaults integerForKey]
检索它。
确保您使用相同的密钥从 NSUserDefaults
中保存和检索对象
我多次尝试使用 NSUserDefaults
来存储我的信息,但都失败了。不知道怎么解决
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (tableView == mySearchController.searchResultsTableView)
{
//BMKPoiInfo* info = poiResultArray[indexPath.row];
NSString *nn = poiResultArray[indexPath.row];
//dw
NSUserDefaults *userDef =[NSUserDefaults standardUserDefaults];
[userDef setInteger:1 forKey:@"poiPosition"];
// [userDef setObject:@"aa" forKey:@"poiPosition"];
[userDef synchronize];
[_selectBtn setTitle:nn/*info.name*/ forState:UIControlStateNormal];
NSInteger nu = [userDef integerForKey:@"poiPosition"];
这些是我的代码,但是 "nu" 没有结果,你可以看到我有
NSUserDefaults *userDef =[NSUserDefaults standardUserDefaults];
[userDef setInteger:1 forKey:@"poiPosition"];
// [userDef setObject:@"aa" forKey:@"poiPosition"];
[userDef synchronize];
为什么"nu"什么都没有?
您可以使用 -[NSUserDefaults setInteger:forKey:]
存储值并使用 -[NSUserDefaults integerForKey]
检索它。
确保您使用相同的密钥从 NSUserDefaults