ios - 为什么使用self.propertyname而不是_propertyname设置我的属性为零

标签 ios objective-c properties

在下面的viewDidLoad()中,将我的属性设置为self.textToAnalyze会导致property在调试器中为nil,而直接使用_textToAnalyze设置该属性则表明该属性不再为nil。为什么是这样?

//
//  TextStatsViewController.m
//  colorSwitcher

#import "TextStatsViewController.h"

@interface TextStatsViewController ()
@property (weak, nonatomic) IBOutlet UILabel *colorfulCharactersLabel;
@property (weak, nonatomic) IBOutlet UILabel *outlinedCharactersLabel;

@end

@implementation TextStatsViewController

-(void)setTextToAnalyze:(NSAttributedString *)textToAnalyze
{

}
-(void)viewDidLoad
{
    _textToAnalyze=[[NSAttributedString alloc] initWithString:@"test" attributes:@{NSForegroundColorAttributeName : [UIColor greenColor],NSStrokeWidthAttributeName :@-3}]; //setting it here with the underscore shows that this property is not nil in the debugger
  //self.textToAnalyze=[[NSAttributedString alloc] initWithString:@"test" attributes:@{NSForegroundColorAttributeName : [UIColor greenColor],NSStrokeWidthAttributeName :@-3}];  //setting it here with the accessor shows that this property is nil in the debugger
}

-(void)updateUI
{
    self.colorfulCharactersLabel.text =[NSString stringWithFormat:@"%d colorful characters",[[self charactersWithAttribute:NSForegroundColorAttributeName] length]];

    self.outlinedCharactersLabel.text =[NSString stringWithFormat:@"%d outlined characters",[[self charactersWithAttribute:NSStrokeWidthAttributeName] length]];
}
-(NSAttributedString *)charactersWithAttribute:(NSString *)attributeName
{
    int index=0;
    NSMutableAttributedString* characters=[[NSMutableAttributedString alloc] init];
    while(index < [self.textToAnalyze length])
    {
        NSRange range;
        id value = [self.textToAnalyze attribute:attributeName atIndex:index effectiveRange:&range];
        if(value)
        {
            [characters appendAttributedString:[self.textToAnalyze attributedSubstringFromRange:range]];
            index=range.location+range.length;
        }else{
            index++;
        }
    }
    return characters;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self updateUI];
}
@end


//
//  TextStatsViewController.h
//  colorSwitcher


#import <UIKit/UIKit.h>

@interface TextStatsViewController : UIViewController
@property (nonatomic,strong)NSAttributedString* textToAnalyze;
@end

最佳答案

因为您有一个空的塞特犬。

-(void)setTextToAnalyze:(NSAttributedString *)textToAnalyze
{

}

当执行self.textToAnalyze = something时与执行[self setTextToAnalyze:something]相同,因此永远不会设置实例变量。

像这样更改您的自定义实现:
-(void)setTextToAnalyze:(NSAttributedString *)textToAnalyze
{
    _textToAnalyze = textToAnalyze;
}

或简单地将其删除,假设您已在.h文件中将textToAnalyze声明为@property:
@property (nonatomic) NSAttributedString *textToAnalyze;

(此外,如果您希望保留传递的值,则该属性必须很强)

关于ios - 为什么使用self.propertyname而不是_propertyname设置我的属性为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20888784/

相关文章:

ios - 资源文件大小超过 2GB 的企业应用程序

objective-c - 带字符串的系统日志?

properties - 如果属性在安装时未设置,为什么在卸载时会被忽略?

java - 依赖jar未加载自己的资源文件

python - 检查类属性是否有 setter

ios - iphone - [NSNull getCharacters :]:

ios - 线程 1 : EXC_BAD_ACCESS (code=1, 地址=0x48)

java - Objective-C 中的字节数组操作像 Java 一样吗?

ios - 设置导航栏返回按钮图片

objective-c - 通过 __block 与不通过 __block 捕获 block 中的外部变量的性能差异