2016-06-11 31 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

非常感謝您的意見! –