Objective c -[\uu NSCFString setStroke]:发送到实例的选择器无法识别
当我运行我的代码时,我总是遇到这个错误,我试图将属性文本分配给按钮。我正在做斯坦福德课程cs193p的作业2。代码通过有问题的行,然后它将崩溃,错误-[\uu NSCFString setStroke]:无法识别的选择器发送到实例。有问题的代码行是当我为state设置AttributeTitle时。这是我正在使用的代码Objective c -[\uu NSCFString setStroke]:发送到实例的选择器无法识别,objective-c,cs193p,Objective C,Cs193p,当我运行我的代码时,我总是遇到这个错误,我试图将属性文本分配给按钮。我正在做斯坦福德课程cs193p的作业2。代码通过有问题的行,然后它将崩溃,错误-[\uu NSCFString setStroke]:无法识别的选择器发送到实例。有问题的代码行是当我为state设置AttributeTitle时。这是我正在使用的代码 for(UIButton * setButton in self.setButtons){ Card *card = [self.game cardAtIndex:[s
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];
}
替换
NSStrokeColorAttributeName:arraycontaininginfo[3]
与
NSStrokeColorAttributeName:colorPicked
看看会发生什么
资料来源:
NSStrokeColorAttributeName
五颜六色
默认为零,与前景色相同
在OS X v10.3及更高版本中提供。在NSAttributedString.h中声明
哇,真管用!非常感谢你!但这怎么会起作用呢?@user2076774:因为该键的值应该是一种颜色
arraycontaininginfo[3]
是一个字符串,而colorPicked
是一种颜色。