2016-06-11 43 views
0

您好我有麻烦编写一个自定义的方法添加属性为NSMutableAttributeString通过传递字符串,int和颜色作为参数,我得到三个错误,请帮助..编写一个添加属性方法在目标C

-(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

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

添加任何

#import <UIKit/UIKit.h> 

@import UIKit; 

到.m文件的顶部。

它也没有任何意义,你使用idstrokeWidthstrokeColor。如果strokeWidthNSString,那就更没有意义了。特别是因为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文件中的声明来匹配。

+0

感谢rmaddy的建议,它现在工作正常。我对编程非常陌生,有什么建议可以改善我的代码?非常感谢 –

+0

看到我更新的答案。 – rmaddy

+0

非常感谢您的意见! –