2016-06-11 3 views
0

안녕하세요, 매개 변수로 문자열, int 및 색상을 전달하여 NSMutableAttributeString에 대한 특성을 추가하는 사용자 지정 메서드를 작성하는 데 문제가 있는데, 아래에 세 가지 오류가 발생했습니다 ..객관적인 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을 가져 오지 않습니다.

는하는 .m 파일의 상단에 하나

#import <UIKit/UIKit.h> 

또는

@import UIKit; 

를 추가합니다.

strokeWidthstrokeColorid을 사용하는 것은 의미가 없습니다. 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

모든 의견에 대단히 감사합니다 !! –

관련 문제