OC之类别总结

前言:

上一篇我写的是“非正式协议”,这一篇是类别。其实两篇的顺序应该颠倒一下,等我写完这一篇再做调整,大家先尝尝鲜。和Android开发不同的是,ios开发是不能查看源代码的,所以很多库函数的实现还有很多类的内部封装我们都看不到。为了能够更好的进行开发,让我们对代码的控制度更高,oc里面支持了“类别”,所谓类别,就是我们可以随意扩展一个类的方法,无论是自定义的类还是NSString之类的框架类。我能力有限,理解大概就这么多。其实非正式协议是NSObject的类别,就是对NSObject进行扩展,因为所有的类都继承自NSObject,这样我们只要导入我们非正式协议的头文件,就可以使用在非正式协议中定义的类了。

1.Demo解释:

在类别这一块,我准备写两个demo,一个是NSString的类别,另一个是自定义类的类别。这两个demo都非常的简单,NSString主要添加两个方法,一个是对URL的判断,另一个是将非URL转换成URL,自定义类只实现一个显示对象属性的方法。不多说了。

2.Demo结构及代码:

OC-类别之NSString
Category
NSString+URL.h
#import <Foundation/Foundation.h>

@interface NSString (URL)
-(BOOL)isURL;//判断是否为URL字符串
-(NSString *)toURL;//将普通字符串转换为URL字符串
@end
NSString+URL.m
#import "NSString+URL.h"

@implementation NSString (URL)
-(BOOL)isURL{
    if (([self hasPrefix:@"http://"]||[self hasPrefix:@"www."])&&([self hasPrefix:@".com"]||[self hasSuffix:@".cn"])) {
        return YES;
    }else{
        return NO;
    }
}
-(NSString *)toURL{
    if (([self hasPrefix:@"http://"]||[self hasPrefix:@"www."])&&!([self hasSuffix:@".com"]||[self hasSuffix:@".cn"])) {//添加后缀
        NSString *str=[NSString stringWithFormat:@"%@%@",self,@".com"];
        return str;
    }
    else if(([self hasSuffix:@".com"]||[self hasSuffix:@".cn"])&&!([self hasPrefix:@"http://"]||[self hasPrefix:@"www."])){//添加前缀
        NSString *str=[NSString stringWithFormat:@"%@%@",@"http://",self];
        return str;
    }
    else if(!([self hasPrefix:@"http://"]||[self hasPrefix:@"www."]||[self hasSuffix:@".com"]||[self hasSuffix:@".cn"])){//添加前缀和后缀
        NSString *str=[NSString stringWithFormat:@"%@%@%@",@"http://",self,@".com"];
        return str;
    }
    else{//不用添加前缀和后缀
        return self;
    }
}
@end
main.m
//
//  main.m
//  OC-类别之NSString
//
//  Created by mac on 15/10/10.
//  Copyright (c) 2015年 macb. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "NSString+URL.h"//引入类别头文件
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str=@"google";
//      调用类别中定义的判断URL方法
        if ([str isURL]) {
            NSLog(@"%@是URL",str);
        }else{
            NSLog(@"%@不是URL",str);
            //      调用类别中定义的将普通字符串转换为URL的方法
            NSLog(@"%@转换为URL后为%@",str,[str toURL]);
        }
    }
    return 0;
}
OC-类别之Person
Interface
Person.h
//
//  Person.h
//  OC-类别之Person
//
//  Created by mac on 15/10/11.
//  Copyright (c) 2015年 macb. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject
@property(nonatomic,copy)NSString *name;
@property int age;
@property char sex;
@end
Person.m
//
//  Person.m
//  OC-类别之Person
//
//  Created by mac on 15/10/11.
//  Copyright (c) 2015年 macb. All rights reserved.
//

#import "Person.h"

@implementation Person

@end
Category
Person+Show.h
//
//  Person+Show.h
//  OC-类别之Person
//
//  Created by mac on 15/10/11.
//  Copyright (c) 2015年 macb. All rights reserved.
//

#import "Person.h"

@interface Person (Show)//小括号中是类别名称
-(void)show;
@end
Person+Show.m
//
//  Person+Show.m
//  OC-类别之Person
//
//  Created by mac on 15/10/11.
//  Copyright (c) 2015年 macb. All rights reserved.
//

#import "Person+Show.h"

@implementation Person (Show)
-(void)show{
//    类别中self指针指向的是Person类型
    NSLog(@"姓名:%@,年龄:%i,性别:%c",self.name,self.age,self.sex);
}
@end
main.m
//
//  main.m
//  OC-类别之Person
//
//  Created by mac on 15/10/11.
//  Copyright (c) 2015年 macb. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person+Show.h"//导入类别头文件
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Person *p=[[Person alloc] init];
        p.name=@"尼古拉耶维奇奥斯特洛夫斯基~";
        p.age=38;
        p.sex='m';
//        直接调用在类别中定义的Person的show方法
        [p show];
    }
    return 0;
}
Demo运行结果:

Demo1:
这里写图片描述
Demo2:
这里写图片描述

4.总结:

这只是两个非常简单的demo,根据我自己的理解,类别就是把自己想要为某个类添加的方法单独放在一个.h和.m文件中,然后在需要的时候在main中添加这个.h头文件,就可以实现动态添加方法的功能,有利于扩展类的功能,也有利于代码的灵活性。
ok,下一篇是Foundation框架中的结构体。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值