pragma mark 分类基本概念
pragma mark 概念
/**
Category 有很多种说法: 分类、类别、类目 (一般情况下都叫分类)
Category是OC特有的语法,其他语言没有的语言
作用:
可以在不修改原来 类 的基础上, 为这个类扩充一些方法
一个庞大的类 可以分模块开发 (人有体育运动、日常生活的方法 可以拆分2个类)
一个庞大的类 可以由多个人来编写, 更利于团队合作
*/
pragma mark 代码
#import <Foundation/Foundation.h>
#pragma mark 类
#import "Person.h"
#pragma mark 分类
#import "Person+LYH.h"
#pragma mark main函数
/**
方法:
方法的声明:
方法的实现:
所以通过分类给某一个类扩充方法, 也分为 声明和实现 两个部分
#warning 分类的声明
@interface ClassName (CategoryName)
NewMethod; // 在类别中添加方法
// 不允许在类别添加变量
@end
ClassName : 需要给那个类扩张方法
CategoryName : 分类的名称
NewMethod : 扩充的方法
#warning 分类的实现
@implementation ClassName (CategoryName)
NewMethod
... ...
@end
ClassName : 需要给那个类扩张方法
CategoryName : 分类的名称
NewMethod : 扩充的方法
*/
int main(int argc, const char * argv[])
{
Person *p = [[Person alloc]init];
p.age = 30;
[p say];
[p playFootball];
[p playBasketBall];
#warning 在不修改类的情况下, 为类添加方法 可以有两种方法: 一、继承 。二、分类Category
// NSString;
#warning 1.系统的NSString 的基本方法
// @interface NSString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding>
//
// #pragma mark *** String funnel methods ***
//
// /* NSString primitives. A minimal subclass of NSString just needs to implement these two, along with an init method appropriate for that subclass. We also recommend overriding getCharacters:range: for performance.
// */
// @property (readonly) NSUInteger length;
// - (unichar)characterAtIndex:(NSUInteger)index;
//
// /* The initializers available to subclasses. See further below for additional init methods.
// */
// - (instancetype)init NS_DESIGNATED_INITIALIZER;
// - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
//
// @end
#warning 2.系统的 NSString 扩充的方法
// @interface NSString (NSStringExtensionMethods)
NSArray;
#warning 1.系统的NSArray基础的方法
/*
@interface NSArray<__covariant ObjectType> : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration>
@property (readonly) NSUInteger count;
- (ObjectType)objectAtIndex:(NSUInteger)index;
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithObjects:(const ObjectType [])objects count:(NSUInteger)cnt NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
@end
*/
#warning 1.系统的NSArray扩充的方法
/**
@interface NSArray<ObjectType> (NSExtendedArray)
- (NSArray<ObjectType> *)arrayByAddingObject:(ObjectType)anObject;
- (NSArray<ObjectType> *)arrayByAddingObjectsFromArray:(NSArray<ObjectType> *)otherArray;
- (NSString *)componentsJoinedByString:(NSString *)separator;
- (BOOL)containsObject:(ObjectType)anObject;
@property (readonly, copy) NSString *description;
- (NSString *)descriptionWithLocale:(nullable id)locale;
- (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level;
- (nullable ObjectType)firstObjectCommonWithArray:(NSArray<ObjectType> *)otherArray;
- (void)getObjects:(ObjectType __unsafe_unretained [])objects range:(NSRange)range;
- (NSUInteger)indexOfObject:(ObjectType)anObject;
- (NSUInteger)indexOfObject:(ObjectType)anObject inRange:(NSRange)range;
- (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject;
- (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject inRange:(NSRange)range;
- (BOOL)isEqualToArray:(NSArray<ObjectType> *)otherArray;
@property (nullable, nonatomic, readonly) ObjectType firstObject NS_AVAILABLE(10_6, 4_0);
@property (nullable, nonatomic, readonly) ObjectType lastObject;
- (NSEnumerator<ObjectType> *)objectEnumerator;
- (NSEnumerator<ObjectType> *)reverseObjectEnumerator;
@property (readonly, copy) NSData *sortedArrayHint;
- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void * __nullable))comparator context:(nullable void *)context;
- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void * __nullable))comparator context:(nullable void *)context hint:(nullable NSData *)hint;
- (NSArray<ObjectType> *)sortedArrayUsingSelector:(SEL)comparator;
- (NSArray<ObjectType> *)subarrayWithRange:(NSRange)range;
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;
- (void)makeObjectsPerformSelector:(SEL)aSelector NS_SWIFT_UNAVAILABLE("Use enumerateObjectsUsingBlock: or a for loop instead");
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(nullable id)argument NS_SWIFT_UNAVAILABLE("Use enumerateObjectsUsingBlock: or a for loop instead");
- (NSArray<ObjectType> *)objectsAtIndexes:(NSIndexSet *)indexes;
- (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0);
- (void)enumerateObjectsUsingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSArray<ObjectType> *)sortedArrayUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);
- (NSArray<ObjectType> *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);
typedef NS_OPTIONS(NSUInteger, NSBinarySearchingOptions) {
NSBinarySearchingFirstEqual = (1UL << 8),
NSBinarySearchingLastEqual = (1UL << 9),
NSBinarySearchingInsertionIndex = (1UL << 10),
};
- (NSUInteger)indexOfObject:(ObjectType)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp NS_AVAILABLE(10_6, 4_0); // binary search
@end
*/
return 0;
}
Person+LYH.h //分类的名称
#import "Person.h"
// 此处的Person 的名字 一定要和 扩充类的那个名字 一样
@interface Person (LYH)
// 扩充方法
- (void)playFootball;
- (void)playBasketBall;
@end
Person+LYH.m
#import "Person+LYH.h"
@implementation Person (LYH)
// 实现扩充方法
- (void)playFootball
{
NSLog(@"%s",__func__);
}
- (void)playBasketBall
{
NSLog(@"%s",__func__);
}
@end
Person.h //人类
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,assign)int age;
- (void)say;
@end
Person.m
#import "Person.h"
@implementation Person
- (void)say
{
NSLog(@"age = %i",_age);
}
@end