Objective-c Category(类别)
NSStringUtilities.h:
#import <Foundation/Foundation.h> @interface NSString(Utilities) -(BOOL) isURL; @end
NSStringUtilities.m
#import "NSStringUtilities.h" @implementation NSString(Utilities) -(BOOL) isURL { if ([self hasPrefix:@"http://"]) { return YES; }else{ return NO; } } @end
main.m:
#import <Foundation/Foundation.h> #import "NSStringUtilities.h" int main(void) { @autoreleasepool { NSString* string1=@"http://www.google.com"; NSString* string2=@"Pixar"; if ([string1 isURL]) { NSLog(@"string is a url"); } if ([string2 isURL]) { NSLog(@"string2 is a url"); } } }
console log:
2013-09-20 12:19:54.708 Obj-c[286:303] string is a url
posted on 2013-09-20 12:24 Ijavascript 阅读(207) 评论(0) 编辑 收藏 举报