Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 传递nsstring时传递null_Ios_Objective C_Null_Nsstring_Nslog - Fatal编程技术网

Ios 传递nsstring时传递null

Ios 传递nsstring时传递null,ios,objective-c,null,nsstring,nslog,Ios,Objective C,Null,Nsstring,Nslog,我在头文件中定义了一个NSString,但当我尝试从该控制器记录该字符串时,它传递null,但如果未从视图控制器调用它,则记录变量的值,这是一个问题,因为当我尝试将其记录到另一个视图控制器时,它也传递null。 我还发现了其他类似的问题,但似乎没有解决办法,所以如果你有一个解决办法,那就太棒了 我已在头文件中对其进行了定义: #import <UIKit/UIKit.h> @interface QuestionController : UIViewController @prope

我在头文件中定义了一个NSString,但当我尝试从该控制器记录该字符串时,它传递null,但如果未从视图控制器调用它,则记录变量的值,这是一个问题,因为当我尝试将其记录到另一个视图控制器时,它也传递null。 我还发现了其他类似的问题,但似乎没有解决办法,所以如果你有一个解决办法,那就太棒了

我已在头文件中对其进行了定义:

 #import <UIKit/UIKit.h>
@interface QuestionController : UIViewController
@property(weak, nonatomic) NSString *question;
@end
当你写作时

QuestionController *questionController = [[QuestionController alloc]init];
NSLog(@"%@", questionController.question);
您没有“从[current]控制器记录该字符串”。相反,您创建了一个完全独立的
QuestionController
实例(与您刚刚从中设置
question
的当前视图控制器不同),并且由于您在任何时候都没有设置其
question
属性,该
NSLog
将打印零

但是,在您的第一个
NSLog

question = [NSString stringWithFormat:@"hey"];
NSLog(@"%@", question);
事实上,您正在记录
QuestionController
的当前实例中设置的
question
,因此它会按预期打印

要将
questionController
question
属性设置为包含
question
,请尝试:

QuestionController *questionController = [[QuestionController alloc]init];
questionController.question = question;
NSLog(@"%@", questionController.question);

我已经清理了你的代码,让它工作,在底部你会找到一个解释

 #import <UIKit/UIKit.h>
@interface QuestionController : UIViewController
@property(strong, nonatomic) NSString *question;
@end


#import "QuestionController.h"
@implementation QuestionController

-(void) viewDidLoad{
    [super viewDidLoad];
    _question = @"hey";
    NSLog(@"%@", _question);
    QuestionController *questionController = [[QuestionController alloc]init];
    questionController.question = _question;
    NSLog(@"%@", questionController.question);


}
@end
#导入
@接口控制器:UIViewController
@属性(强,非原子)NSString*问题;
@结束
#导入“QuestionController.h”
@执行问题控制器
-(无效)viewDidLoad{
[超级视图下载];
_问题=@“嘿”;
NSLog(@“%@”,_问题);
QuestionController*QuestionController=[[QuestionController alloc]init];
questionController.question=\u问题;
NSLog(@“%@”,questionController.question);
}
@结束
有几件事需要注意

  • NSString*问题现在是一个很强的参考,而不是很弱。如果将其保留为弱,则诸如question=@“Hello”之类的操作将不起作用,因为没有对该字符串对象的强引用,它将被解除分配
  • 我删除了@synthetic,除非您使用的是旧版本的Xcode,否则您不再需要这样做
  • 我将问题改为_问题,您现在直接访问ivar,或者您可以在该实例中使用self.question通过getter/setter访问它
  • 删除了stringWithFormat部分,您并没有真正使用该方法,只做@“您想写的”就足够了
  • 当您执行QuestionController*QuestionController=[[QuestionController alloc]init]时,实际上是在分配一个新版本的QuestionController;这意味着您现在有一个QuestionController,它在加载视图时创建另一个QuestionController
  • questionController.question=\u问题;在第一个QuestionController的视图加载到原始QuestionController中的问题字符串时创建的新QuestionController对象上设置问题字符串对象

希望这能帮助您准确地理解代码中发生了什么。我假设在加载第一个QuestionController时,您不是有意创建一个新的QuestionController。这也会导致某种无休止的循环,因为您创建的每个日志在访问其view属性时都会创建另一个。

为什么NSString很弱?在这种情况下,第二个日志肯定是零,因为您没有在QuestionController的实例中初始化question属性。。。但第一个日志打印也是零吗?并没有特别的原因让我变弱,但当我把它改为强时,它并没有改变任何东西@Rockyth第一个日志没有打印零@Lyndseyscott你认为你在哪里“传递NSString”?
 #import <UIKit/UIKit.h>
@interface QuestionController : UIViewController
@property(strong, nonatomic) NSString *question;
@end


#import "QuestionController.h"
@implementation QuestionController

-(void) viewDidLoad{
    [super viewDidLoad];
    _question = @"hey";
    NSLog(@"%@", _question);
    QuestionController *questionController = [[QuestionController alloc]init];
    questionController.question = _question;
    NSLog(@"%@", questionController.question);


}
@end