【问题标题】:Why is my property nil when setting it using self.propertyname instead of _propertyname为什么我的属性在使用 self.propertyname 而不是 _propertyname 设置时为零
【发布时间】:2014-01-02 17:54:22
【问题描述】:

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

//
//  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

【问题讨论】:

    标签: ios objective-c properties


    【解决方案1】:

    因为你有一个空的二传手。

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

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

    像这样更改您的自定义实现:

    -(void)setTextToAnalyze:(NSAttributedString *)textToAnalyze
    {
        _textToAnalyze = textToAnalyze;
    }
    

    或者干脆删除它,假设您已在 .h 文件中将 textToAnalyze 声明为 @property:

    @property (nonatomic) NSAttributedString *textToAnalyze;
    

    (另外,如果要保留传递的值,属性必须是强的)

    【讨论】:

    • 最好完全删除自定义设置器。自定义 setter/getter 实现通常是复杂设计的标志。 setter 和 getter 应该非常简单——应该是默认的综合行为——并且任何与突变相关的业务逻辑都应该分开。
    • @bbum 这是一个有趣的见解。但是,如果我需要在其属性更改时使我的自定义视图无效怎么办?我应该通过例如观察变化吗? KVO——尽管那是我自己的财产——并且从观察者的方法中使我的观点无效?当我可以通过一些检查和setNeedsDisplay 调用(如有必要)来实现我的自定义设置器时,这对我来说也很复杂。你怎么看?
    • @AlaksiejN。在集合点使用这种逻辑通常会更好。也就是说,最好先进行各种状态更改,然后告诉一切根据当前包含的状态更新其视觉状态。如果您让每个对象根据每个属性更改触发重新计算渲染状态,那么您很快就会因大量不必要的计算而导致巨大的性能瓶颈。
    • @AlaksiejN。简单的例子; Person 对象有firstName 和lastName。它们有时会一个接一个地改变,但并非总是如此。如果你有一个firstName 的观察者重新计算东西,然后lastName 的一个观察者重新计算一组重叠的东西,那么你最终会做不必要的工作。 但是如果只更改了一个,您仍然需要触发重新计算,这会导致将“完成更改,更新视觉状态”逻辑移到原始类/属性之外。
    猜你喜欢
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多