objective-c中Category(类别)

Category(类别)

 

NSStringUtilities.h文件

 

//  NSStringUtilities.h

//  Twopage

//

//  Created by Evis on 12-7-28.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

@interface NSString (Utilities)

 

-(BOOL)isURL;

 

@end

 

NSStringUtilities.m文件

 

//  NSStringUtilities.m

//  Twopage

//

//  Created by Evis on 12-7-28.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//

 

#import “NSStringUtilities.h”

 

@implementation NSString (Utilities)

 

-(BOOL)isURL{

    if([self hasPrefix:@”http://”]){

        return YES;

    }else {

        return NO;

    }

}

 

@end

 

main.m文件

 

//  main.m

//  Twopage

//

//  Created by Evis on 12-7-16.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//

 

#import <Foundation/Foundation.h>

#import “NSStringUtilities.h”

 

int main(int argc, const char * argv[])

{

 

    @autoreleasepool {

       

        // insert code here…

        NSString *string1 = @”http://www.gdcool.net”;

        NSString *string2 = @”elvis”;

       

        if ([string1 isURL]) {

            NSLog(@”string1 is a URL”);

        }else {

            NSLog(@”string2 is not a URL”);

        }

       

        if ([string2 isURL]) {

            NSLog(@”string1 is a URL”);

        }else {

            NSLog(@”string2 is not a URL”);

        }    }

    return 0;

}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注