I keep getting this error when I run my code, I am trying to assign attributed text to a button. I am doing assignment #2 from the standford course cs193p. The code go pass the line in question and then randonly it will crash with the error -[__NSCFString setStroke]: unrecognized selector sent to instance. The line of code in question is when I setAttributedTitle forState. Here is the code I am using
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];
}
Replace
NSStrokeColorAttributeName : arraycontaininginfo[3]
with
NSStrokeColorAttributeName : colorPicked
and see what happens.
Source:
NSStrokeColorAttributeName
NSColor
Default nil, same as foreground color
Available in OS X v10.3 and later. Declared in NSAttributedString.h.