我在运行代码时遇到此错误,我正在尝试将属性文本分配给按钮。我正在从斯坦福课程cs193p完成作业#2。代码将通过有问题的行,然后randonly它将崩溃,错误 - [__ NSCFString setStroke]:无法识别的选择器发送到实例。有问题的代码行是我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];
}
答案 0 :(得分:1)
替换
NSStrokeColorAttributeName : arraycontaininginfo[3]
与
NSStrokeColorAttributeName : colorPicked
看看会发生什么。
来源:
NSStrokeColorAttributeName
NSColor
默认值为nil,与前景色相同
在OS X v10.3及更高版本中可用。在NSAttributedString.h中声明。