-1

当我运行我的代码时,我不断收到这个错误,我试图将属性文本分配给一个按钮。我正在做斯坦福课程 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];

}
4

1 回答 1

1

代替

NSStrokeColorAttributeName : arraycontaininginfo[3]

NSStrokeColorAttributeName : colorPicked

看看会发生什么。

资源:

NSStrokeColorAttributeName

NSColor

默认无,与前景色相同

在 OS X v10.3 及更高版本中可用。在 NSAttributedString.h 中声明。

从文档

于 2013-06-17T17:11:39.097 回答