为什么这个没有保存?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上是否将该 ivar 设置为默认对象?
您通常会在 init 方法中执行此类操作。
我猜测这是因为在 ObjectiveC 中 nil 可以调用它的方法,所有这些方法都返回 nil。因此,如果
name
为 nil,那么它将默默地无法保存,然后当您尝试读回它时调用 return nil。Did you actually set that ivar to a defaults object?
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.与大多数对象不同,
UIViewController
的指定初始值设定项不是init
。我现在看到你确实说这是一个 ViewController,抱歉我之前错过了。UIViewController
的指定初始化程序是initWithCoder
或initWithFrame
,将调用其中之一进行初始化。这会导致问题,“我在哪里放置我的设置代码”,这个问题之前已经讨论过。事实上,我敢打赌,如果您将
NSLog(@"Init");
放入init
方法中,您会发现它从未被调用。因此它永远无法分配您的ivar
。为简单起见,如果您要继承
UIViewController
并希望执行不必在 init 方法中的初始化,则可以使用viewDidLoad
。Unlike most Objects the designated initializer for a
UIViewController
is notinit
. I see now that you did say that this is a ViewController, sorry I missed it before. The designated initializer for aUIViewController
is eitherinitWithCoder
orinitWithFrame
, 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 yourinit
method you'd see it's never called. Therefore it can never assign yourivar
.For simplicity if you are subclassing
UIViewController
and want to do initialization that doesn't HAVE to be in an init method the you useviewDidLoad
.