在目标C中编写一个add属性方法

时间:2016-06-11 20:25:26

标签: objective-c methods nsmutableattributedstring

您好我编写一个自定义方法,通过传递字符串,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;
}

1 个答案:

答案 0 :(得分:1)

给你错误的所有三个符号来自UIKit。所以这意味着你不是在.m文件的顶部导入UIKit。

添加

@import UIKit;

id

到.m文件的顶部。

strokeWidthstrokeColor使用strokeWidth毫无意义。如果NSStringNSStrokeWidthAttributeName,那就没什么意义了。特别是因为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文件中的声明以匹配。