Objective-C中的类别

时间:2012-11-18 00:29:38

标签: objective-c objective-c-category

我在NSString上写了以下类别:

 @interface NSString (Checks)

 -(BOOL) isUppercase;
 -(BOOL) containsStr: (NSString *) str;

 @end;

 @implementation NSString (Checks)

-(BOOL) isUppercase
 { return [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[self         characterAtIndex:0]]
 }
 -(BOOL) containstStr: (NSString *) str
 { return [self rangeOfString:str].location != NSNotFound;
 }
@end

我缺少方法声明的上下文,期望的方法体,@ end必须出现在.h文件中的Objective C上下文错误中,为什么会这样?

1 个答案:

答案 0 :(得分:0)

@end之后你不应该有分号。还要确保导入Foundation框架:

#import <Foundation/Foundation.h>

@interface NSString (Checks)

- (BOOL)isUppercase;
- (BOOL)containsStr:(NSString *)str;

@end


#import "NSString+Checks.h"  // or whatever your .h is called.

@implementation NSString (Checks)

- (BOOL)isUppercase {
    return [[NSCharacterSet uppercaseLetterCharacterSet] 
           characterIsMember:[self characterAtIndex:0]];
}

- (BOOL)containsStr:(NSString *)str {
    return [self rangeOfString:str].location != NSNotFound;
}

@end