简体   繁体   English

财产不保值

[英]property not holding onto value

I have a property in my view controller (the app only has one view controller) 我的视图控制器中有一个属性(该应用程序只有一个视图控制器)

@property (weak, nonatomic) NSString * correctAnswer;

In viewDidLoad, I set that property like this and the log statement confirms that it's being set 在viewDidLoad中,我像这样设置该属性,并且log语句确认它已被设置

self.correctAnswer = info.correctAnswer;
NSLog(@"correctAnswer %@", self.correctAnswer);

However, when I click a button in the view and try to inspect that same property in the action (in the same view controller) triggered by the button, it's turning out to be (null) Therefore the string comparison is always wrong. 但是,当我单击视图中的按钮并尝试检查由按钮触发的动作(在同一视图控制器中)的同一属性时,结果显示为(null)因此字符串比较总是错误的。 Can you explain what I'm doing wrong? 你能解释我做错了吗?

- (IBAction)checkResponse:(UIButton *)sender {

    UIButton *resultButton = (UIButton *)sender;
    NSLog(@" The button's title is %@.", resultButton.currentTitle);
    NSLog(@"correct answer %@", self.correctAnswer);  //null

    if ([resultButton.currentTitle isEqualToString:self.correctAnswer]){
        NSLog(@"you guessed right");
    }else {
        NSLog(@"You guessed wrong");

    }


}

Change the property from weak to strong (or better yet in this case, copy ). 将属性从weak更改为strong (或者在这种情况下更好,请copy )。

Weak properties are set to nil as soon as the referenced object is deallocated. 释放引用对象后,弱属性将立即设置为nil

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

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