xcode NSMutableArray和NSUserdefaults问题iPhone

wswtfjt7  于 2023-05-19  发布在  其他
关注(0)|答案(5)|浏览(133)
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSUserDefaults *somet = [NSUserDefaults standardUserDefaults];

    NSMutableArray *anArr = [somet objectForKey:@"somedata"] ;

    NSLog(@"anArr::: %@",anArr);

}

-(IBAction)addsomething:(id)sender{

   if (something == nil) {
        array = [[NSMutableArray alloc] init];
    }
    NSLog(@"textfieldvalue::: %@", textfield.text);

    [array addObject:textfield.text];

    NSUserDefaults *something = [NSUserDefaults standardUserDefaults];

    [something setObject:array forKey:@"somedata"];

    array = [something objectForKey:@"somedata"];

    NSLog(@"array:: %@", array);

}

使用上面的代码,我尝试在NSMutableArray中动态保存数据,然后将其保存在NSUserDefaults中。但在检索时,我得到了(null)。即使数据没有保存在数组中。
如何正确保存数据?

EDITED:

2012-03-15 15:17:18.431 check[2686:40b] anArr::: (null)
2012-03-15 15:17:30.160 check[2686:40b] textfieldvalue::: a
2012-03-15 15:17:30.162 check[2686:40b] array::: (null)
w8biq8rn

w8biq8rn1#

无意冒犯但你的代码就是一堆垃圾
1.名为something的变量没有用。句号。
1.你的局部方法中有一个名为something的变量。你有一个名为something的变量,它是你的类的一部分。“局部变量隐藏示例变量”警告是有原因的!
也许这就是你所有问题的来源。我认为你并没有真正理解局部变量的概念。如果是这样的话,你应该阅读更多关于Objective-C的基本内容。
1.你检查是否有值为nil,然后你不改变什么,只分配一个数组。这两个人没有任何关系。不要写这样的代码。
我帮你修好了:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSArray *anArr = [userDefaults objectForKey:@"somedata"];
    NSLog(@"anArr::: %@",anArr);
}

-(IBAction)addsomething:(id)sender{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    NSLog(@"textfieldvalue::: %@", textfield.text);
    [array addObject:textfield.text];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:array forKey:@"somedata"];
    array = [userDefaults objectForKey:@"somedata"];

    // synchronize is only needed while debugging (when you use the stop button in Xcode)
    // you don't need this in production code. remove it for release
    [userDefaults synchronize];

    NSLog(@"array:: %@", array);
}

但是,您很可能希望将对象保存到从userDefaults获得的数组中。这对局部变量不起作用!
你会用这样的东西。但别一字不差地抄。试着理解它,并阅读更多关于局部变量的内容。

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
array = [[userDefaults objectForKey:@"somedata"] mutableCopy];
if (!array) {
    // create array if it doesn't exist in NSUserDefaults
    array = [[NSMutableArray alloc] init];
}
NSLog(@"array in viewDidLoad: %@",array);


NSLog(@"textfieldvalue::: %@", textField.text);
[array addObject: textField.text];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:array forKey:@"somedata"];

// synchronize is only needed while debugging (when you use the stop button in Xcode)
// you don't need this in production code. remove it for release
[userDefaults synchronize];
hgncfbus

hgncfbus2#

请尝试以下代码:

- (void)viewDidLoad
{
 [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

NSUserDefaults *somet = [NSUserDefaults standardUserDefaults];

NSMutableArray *anArr = [[NSMutableArray alloc] init];
 anArr = [somet objectForKey:@"somedata"] ;
[somet synchronize];

NSLog(@"anArr::: %@",anArr);

}

-(IBAction)addsomething:(id)sender{

NSLog(@"textfieldvalue::: %@", textfield.text);
//Here u have to check array is in memory allocate or not 
[array addObject:textfield.text];

NSUserDefaults *something = [NSUserDefaults standardUserDefaults];

[something setObject:array forKey:@"somedata"];
[something synchronize];

array = [something objectForKey:@"somedata"];

NSLog(@"array:: %@", array);

}
bjg7j2ky

bjg7j2ky3#

您需要在setObject:forKey之后调用[[NSUserDefaults standardUserDefaults] synchronize];

[something setObject:array forKey:@"somedata"];
[something synchronize];
ie3xauqp

ie3xauqp4#

添加这一行,

[something synchronize]

在检索数据之前。

epggiuax

epggiuax5#

在将值保存到NSUserDefaults中时尝试此操作

- (void)savingObject:(NSString *)keyName data:(id)val
{
    NSData *dataVal = [NSKeyedArchiver archivedDataWithRootObject:val];
    [defaults setObject:dataVal forKey:keyName];
    [defaults synchronize];
}

然后当你想要的时候用这个

- (id)gettingObject:(NSString *)keyName
{
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:keyName];
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

我想这对你有帮助

相关问题