在objective C中写一个添加属性的方法
write an add attribute method in objective C
您好,我在编写自定义方法时遇到问题,通过将字符串、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;
}
给你错误的所有三个符号都来自 UIKit。所以这意味着您没有在 .m 文件的顶部导入 UIKit。
添加其中一个
#import <UIKit/UIKit.h>
或
@import UIKit;
到 .m 文件的顶部。
将 id
用于 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;
}
当然你需要更新 .h 文件中的声明来匹配。
您好,我在编写自定义方法时遇到问题,通过将字符串、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;
}
给你错误的所有三个符号都来自 UIKit。所以这意味着您没有在 .m 文件的顶部导入 UIKit。
添加其中一个
#import <UIKit/UIKit.h>
或
@import UIKit;
到 .m 文件的顶部。
将 id
用于 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;
}
当然你需要更新 .h 文件中的声明来匹配。