代码改变世界

在XCode6中 增加NSString 扩展方法

2014-10-30 12:22  wlhc  阅读(263)  评论(0)    收藏  举报

1.新建Category 类 在Xcode6 中建立如下:

 

 

 

 

 

 

 

为NSString 增加扩展 是否为空的方法:

 

 

#import <Foundation/Foundation.h>

 

@interface NSString (IsEmptyOrNil)

-(BOOL) isEmptyOrNll;

@end

 

 

#import "NSString+IsEmptyOrNil.h"

 

@implementation NSString (IsEmptyOrNil)

-(BOOL)isEmptyOrNll

{

    if(self==nil)

        return YES;

    else if(self.length <=0)

        return  YES;

    return NO;

}

@end

 

————————

应用:

#import "NSString+IsEmptyOrNil.h”

 

- (IBAction)loginAction:(id)sender {

    NSString *un=self.userName.text;

    NSString *pas=self.password.text;

//应用扩展方法

    if([un isEmptyOrNll]==YES || [un isEmptyOrNll] ==YES)

    {

        NSLog(@"用户名或密码为空");

        return;

    }

    LoginService *service=[[LoginService alloc] init];

   BOOL isSuccess=  [service userLogin:un userPassword:pas];

    if(isSuccess==YES)

    {

        NSLog(@"Login success");

    }

}