【发布时间】: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