0

我在 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 上下文错误中,这是为什么呢?

4

1 回答 1

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
于 2012-11-18T00:31:50.687 回答