为什么这个没有保存?

发布于 2024-12-17 05:42:48 字数 825 浏览 3 评论 0原文

NSString *senderName = [sender currentTitle];
NSLog(@"Text to be Saved = %@",senderName);
[name setObject:senderName forKey:@"key"];
NSString *TextActuallySaved = [name stringForKey:@"key"];
NSLog(@"Text actually Saved = %@", TextActuallySaved);

所以我有这段代码,当我运行它时,我在控制台中看到:

appName[19556:f803] Text to be Saved = SCV
appName[19556:f803] Text actually Saved = (null)

那么,我做错了什么?我在 @interface 的标头中声明了一个 NSUserDefault 对象,如下所示:

@interface viewController : UIViewController  {
NSUserDefaults *name;
}

所有这些代码都在被推送时位于 ViewController 中。所以是因为这个吗?如果是这样,有办法解决这个问题吗? 这严重困扰着我。任何帮助将不胜感激。谢谢!

这是我的初始化代码

- (id)init {
if (self = [super init])
{
    name = [NSUserDefaults standardUserDefaults];
}
return self;
}
NSString *senderName = [sender currentTitle];
NSLog(@"Text to be Saved = %@",senderName);
[name setObject:senderName forKey:@"key"];
NSString *TextActuallySaved = [name stringForKey:@"key"];
NSLog(@"Text actually Saved = %@", TextActuallySaved);

So I have this code, And when I run it I get this in the console:

appName[19556:f803] Text to be Saved = SCV
appName[19556:f803] Text actually Saved = (null)

So, what did I do wrong? I declared a NSUserDefault object in the header at the @interface like so:

@interface viewController : UIViewController  {
NSUserDefaults *name;
}

All this code is in a ViewController as it is getting pushed. So is that why? If so is there a way to get around that?
This is seriously bugging me. Any help would be much appreciated. THANKS!

Here is my Init code

- (id)init {
if (self = [super init])
{
    name = [NSUserDefaults standardUserDefaults];
}
return self;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的韩爷 2024-12-24 05:42:48

您实际上是否将该 ivar 设置为默认对象?

name = [NSUserDefaults standardUserDefaults];

您通常会在 init 方法中执行此类操作。


我猜测这是因为在 ObjectiveC 中 nil 可以调用它的方法,所有这些方法都返回 nil。因此,如果 name 为 nil,那么它将默默地无法保存,然后当您尝试读回它时调用 return nil。

Did you actually set that ivar to a defaults object?

name = [NSUserDefaults standardUserDefaults];

You would typically do such thing in your init method.


Im guessing this because in ObjectiveC nil can have methods called on it, all of what return nil. So if name is nil, then it will silently fail to save, then call return nil when you try read it back.

软甜啾 2024-12-24 05:42:48

与大多数对象不同,UIViewController 的指定初始值设定项不是 init。我现在看到你确实说这是一个 ViewController,抱歉我之前错过了。 UIViewController 的指定初始化程序是 initWithCoderinitWithFrame,将调用其中之一进行初始化。这会导致问题,“我在哪里放置我的设置代码”,这个问题之前已经讨论过。

事实上,我敢打赌,如果您将 NSLog(@"Init"); 放入 init 方法中,您会发现它从未被调用。因此它永远无法分配您的ivar

为简单起见,如果您要继承 UIViewController 并希望执行不必在 init 方法中的初始化,则可以使用 viewDidLoad

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

Unlike most Objects the designated initializer for a UIViewController is not init. I see now that you did say that this is a ViewController, sorry I missed it before. The designated initializer for a UIViewController is either initWithCoder or initWithFrame, one of those to will be called for initialization. Which leads to problems, 'where do I put my setup code', this has been discussed on SO before.

In fact I'd wager that if you put NSLog(@"Init"); in your init method you'd see it's never called. Therefore it can never assign your ivar.

For simplicity if you are subclassing UIViewController and want to do initialization that doesn't HAVE to be in an init method the you use viewDidLoad.

- (void)viewDidLoad{
    [super viewDidLoad];
    name = [NSUserDefaults standardUserDefaults];
    // Do any additional setup after loading the view, typically from a nib.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文