您好我编写一个自定义方法,通过传递字符串,int和颜色作为参数来添加NSMutableAttributeString的属性时遇到麻烦,我在下面遇到三个错误,请帮助..
-(NSMutableAttributedString*)setAttributedSuits: (NSString*) suitString
setwidth:(id)strokeWidth
setColor:(id)strokeColor{
NSMutableAttributedString* attributeSuits = [[NSMutableAttributedString alloc]initWithString:suitString];
if ([strokeWidth isKindOfClass:[NSString class]]&&[strokeWidth isKindOfClass:[UIColor class]]) // error 1 - use of undeclared identifier "UIColor", did you mean '_color'?
{
[attributeSuits addAttributes:@{NSStrokeWidthAttributeName:strokeWidth, // error 2 - use of undeclared identifier "NSStrokeWidthAttributeName"
NSStrokeColorAttributeName:strokeColor} //// error 3 - use of undeclared identifier "NSStrokeColorAttributeName"
range:NSMakeRange(0, suitString.length)];
}
return attributeSuits;
}
答案 0 :(得分:1)
给你错误的所有三个符号来自UIKit。所以这意味着你不是在.m文件的顶部导入UIKit。
添加
@import UIKit;
或
id
到.m文件的顶部。
对strokeWidth
和strokeColor
使用strokeWidth
毫无意义。如果NSString
是NSStrokeWidthAttributeName
,那就没什么意义了。特别是因为NSNumber
密钥需要- (NSMutableAttributedString *)setAttributedSuits:(NSString *)suitString width:(CGFloat)strokeWidth color:(UIColor *)strokeColor {
NSDictionary *attributes = @{
NSStrokeWidthAttributeName : @(strokeWidth),
NSStrokeColorAttributeName : strokeColor
};
NSMutableAttributedString *attributeSuits = [[NSMutableAttributedString alloc] initWithString:suitString attributes:attributes];
return attributeSuits;
}
。我强烈建议您将代码更改为:
{{1}}
当然,您需要更新.h文件中的声明以匹配。