当我运行我的代码时,我不断收到这个错误,我试图将属性文本分配给一个按钮。我正在做斯坦福课程 cs193p 的作业 #2。代码通过了有问题的行,然后它会随机崩溃并出现错误 -[__NSCFString setStroke]: unrecognized selector sent to instance. 有问题的代码行是当我 setAttributedTitle forState 时。这是我正在使用的代码
for(UIButton * setButton in self.setButtons){
Card *card = [self.game cardAtIndex:[self.setButtons indexOfObject:setButton]];
NSDictionary* fullType = @{ @"triangle" : @"▲", @"circle" : @"●", @"square" : @"■"};
NSDictionary* hollowType = @{ @"triangle" : @"△", @"circle" : @"○", @"square" : @"☐"};
NSLog(@"%@", card.contents);
NSArray * arraycontaininginfo = [card.contents componentsSeparatedByString:@","];
NSLog(@"%@", arraycontaininginfo);
NSLog(@"%@", arraycontaininginfo[2]);
NSLog(@"%@", arraycontaininginfo[3]);
if([arraycontaininginfo[2] isEqualToString:@"0.0"]){ // This helps me choose object in UIButton
NSString * displayType = [hollowType objectForKey:arraycontaininginfo[1]];
if ([[arraycontaininginfo objectAtIndex:0] isEqualToString:@"1"]) {
self.displayView = [[NSString alloc] initWithString:displayType];
} else if ([[arraycontaininginfo objectAtIndex:0] isEqualToString:@"2"]){
NSMutableString *temp = [[NSMutableString alloc] initWithString:displayType];
[temp appendString:displayType];
self.displayView = [temp copy];
} else if ([[arraycontaininginfo objectAtIndex:0] isEqualToString:@"3"]){
NSMutableString *temp = [[NSMutableString alloc] initWithString:displayType];
[temp appendString:displayType];
[temp appendString:displayType];
self.displayView = [temp copy];
}
} else{
NSString * displayType = [fullType objectForKey:arraycontaininginfo[1]];
if ([[arraycontaininginfo objectAtIndex:0] isEqualToString:@"1"]) {
self.displayView = [[NSString alloc] initWithString:displayType];
} else if ([[arraycontaininginfo objectAtIndex:0] isEqualToString:@"2"]){
NSMutableString *temp = [[NSMutableString alloc] initWithString:displayType];
[temp appendString:displayType];
self.displayView = [temp copy];
} else if ([[arraycontaininginfo objectAtIndex:0] isEqualToString:@"3"]){
NSMutableString *temp = [[NSMutableString alloc] initWithString:displayType];
[temp appendString:displayType];
[temp appendString:displayType];
self.displayView = [temp copy];
}
}
NSLog(@"%@", self.displayView);
NSLog(@"%@", arraycontaininginfo[2]);
CGFloat alphaValue = [[arraycontaininginfo objectAtIndex:2] floatValue]; // gets color from dictionary by searching key using NSString method and returns UIColor
UIColor *colorPicked = [self.colorTable objectForKey:arraycontaininginfo[3]];
NSLog(@"%@", [colorPicked class]);
NSLog(@"%@", arraycontaininginfo[3]);
NSLog(@"%@", colorPicked);
// CODE CRASHES HERE!!!
NSDictionary * attributeDictionary = @{NSForegroundColorAttributeName: [colorPicked colorWithAlphaComponent:alphaValue], NSStrokeColorAttributeName : arraycontaininginfo[3], NSStrokeWidthAttributeName: @-4};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.displayView attributes:attributeDictionary];
[setButton setAttributedTitle:attributedString forState:UIControlStateSelected];
[setButton setAttributedTitle:attributedString forState:UIControlStateNormal];
}