简体   繁体   English

nslog可以打印变量,但变量不保持值?

[英]nslog can print the variable, but variable not holding value?

I have this: 我有这个:

    NSUserDefaults *defauts = [NSUserDefaults standardUserDefaults];
    NSString *namestr = [defauts objectForKey:@"savednamestring"];

    NSLog(@"%@",namestr);
    [unusedtext setText:namestr];
    NSLog(@"%@",unusedtext); 
    NSLog(@"%@",unusedtext.text);

Now, first nslog prints out the content of namestr , for instance "Jack". 现在,首先nslog打印出namestr的内容,例如“Jack”。

But the second nslog and 3rd nslog both give null . 但是第二个nslog和第三个nslog都给出了null

when i try to pass namestr into entity it gives me an unrecognized selector sent to instance error, and i know it is because of namestr not passing anything into the entity, but how nslog print out namestr at the first place. 当我尝试将namestr传递给实体时,它给了我一个unrecognized selector sent to instance错误,我知道这是因为namestr没有传递任何东西进入实体,但是nslog如何在第一个地方打印出namestr And do i need to convert it into some sort of format to make it work? 我是否需要将其转换为某种格式才能使其正常工作?

unusedText可能未初始化。

I'm assuming that the unusedtext is an UILabel. 我假设unusedtext是UILabel。

There are a few questions: 有几个问题:

  • Are you using Interface Builder or Storyboard? 您使用的是Interface Builder还是Storyboard? If yes, did you linked the var with the component? 如果是,您是否将var与组件相关联?
  • If you are not using IB or Storyboard, is this "unusedtext" initialized? 如果您没有使用IB或Storyboard,这个“unusedtext”是否已初始化?
  • Are you using ARC? 你在用ARC吗? If no, isn't the unusedtext autorelease? 如果不是,是不是undetext自动释放? Or released before your code? 或者在你的代码之前发布?
  • If you are using ARC, is the unusedtext marked as STRONG? 如果您使用ARC,那么未使用的文本是否标记为STRONG?

Hope to help! 希望能帮到你!

thats because you haven't initialized unusedtext variable. 那是因为你没有初始化unusedtext变量。

if it is NSString then do this 如果是NSString则执行此操作

NSUserDefaults *defauts = [NSUserDefaults standardUserDefaults];
NSString *namestr = [defauts objectForKey:@"savednamestring"];

NSLog(@"%@",namestr);
unusedtext = [NSString stringWithString:namestr];
NSLog(@"%@",unusedtext); 
NSLog(@"%@",unusedtext.text);

and if it is a UITextView or UITextfield than do this, instead! 如果它是UITextView或UITextfield,而不是这样做!

NSUserDefaults *defauts = [NSUserDefaults standardUserDefaults];
NSString *namestr = [defauts objectForKey:@"savednamestring"];

NSLog(@"%@",namestr);
UITextField *unusedtext = [[UITextField alloc] init];
[unusedtext setText:namestr];
NSLog(@"%@",unusedtext); 
NSLog(@"%@",unusedtext.text);

hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM