gpt4 book ai didi

ios - 获取NSUserdefaults时出现问题

转载 作者:行者123 更新时间:2023-12-01 17:56:51 25 4
gpt4 key购买 nike

我刚开始使用XCode进行编码,因此我尝试了很多方法来保存信息,但效果不佳。我用谷歌搜索,但仍然没有找到任何东西。

我采用了一个名为classNames的可变数组,并尝试保存文本字段(classField)中的信息,但该信息未与数组一起存储。

我还有另一个问题:我正在使用具有iOS 5.1的XCode,并且我已升级到具有iOS 6.1的Xcode,但是它给我一个错误,提示“连接中断”,并且所有按钮或功能都不起作用,但是它可以与具有iOS的XCode一起使用5.1
我需要帮助,谢谢。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize orignalClass;
@synthesize otherClassName;
@synthesize editClassLabel;
@synthesize displayLabel;
@synthesize doSomethingButton;
@synthesize classField;
@synthesize testField;
@synthesize classFieldFireTheme;
@synthesize testFieldFireTheme;

- (void)viewDidLoad
{
[super viewDidLoad];

_classNames = [[NSMutableArray alloc] init];
_classNames = [defaults objectForKey:@"classNames"];
//NSLog(@" %@", [defaults objectForKey:@"classNames"]);


}


- (void)viewDidUnload
{
[self setDoSomethingButton:nil];
[self setClassField:nil];
[self setTestField:nil];
[self setClassFieldFireTheme:nil];
[self setTestFieldFireTheme:nil];
[self setDisplayLabel:nil];
[self setOrignalClass:nil];
[self setEditClassLabel:nil];
[self setOtherClassName:nil];
[self setTestClass:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)classtextFieldDoneEdtiting:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)testClassTextFieldDoneEdtiting:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)TestTextFieldDoneEdtiting:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)ChangeClassTextFieldDoneEdtiting:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)ChangeClassToTextFieldDoneEdtiting:(id)sender {
[sender resignFirstResponder];
}

- (IBAction)addClass:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

_classNames = [defaults objectForKey:@"classNames"];
_classSize = [_classNames count];
NSLog(@" %d", [_classNames count]);

if(_classSize <=9){

[_classNames insertObject: classField.text atIndex: [_classNames count]];
NSLog(@"ClassNames#2::: %@", _classNames);
NSLog(@"textfieldvalue::: %@", classField.text);
[defaults setObject:_classNames forKey:@"classNames"];
[[NSUserDefaults standardUserDefaults] synchronize];

NSLog(@" %d", [_classNames count]);
NSLog(@"ClassNames#3::: %@", _classNames);

}else{
displayLabel.text = @"You exceeded the limit of classes being stores!!!!";
}

}

最佳答案

问题是,当数组从NSUserDefaults中出来时,它不是可变数组。即使您输入的是NSMutableArray,输出的还是普通的NSArray。您需要在该数组上使用mutableCopy使其可变。现在您可以存储到其中,然后将其放回NSUserDefaults中。

关于ios - 获取NSUserdefaults时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15930544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com