2016-06-11 13 views
0

こんにちは私はパラメータとして文字列、int、および色を渡すことによってNSMutableAttributeStringの属性を追加するカスタムメソッドを記述するのに苦労しています、私は以下の3つのエラーを取得しています..目的の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

エラーを示す3つの記号はすべてUIKitからのものです。これは、.mファイルの先頭にUIKitをインポートしていないことを意味します。

は、.mファイルの先頭に

#import <UIKit/UIKit.h> 

または

@import UIKit; 

のいずれかを追加します。

strokeWidthstrokeColorにはidを使用することも意味がありません。また、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

ありがとうございました! –