接上: KVC(二)
接下来我们来看看 集合、集合操作符(集合作为属性)
参考官网:Accessing Collection Properties、Using Collection Operators、Defining Collection Methods
参考其他:Key-Value Coding、Key-Value Coding(键值编码)
1.固定有序集合属性(NSArray)
#import "ViewController8.h"
@interface Person_8 : NSObject
@property (nonatomic,strong)NSString *name;
@end
@implementation Person_8
@end
@interface Students_8 : NSObject
//@property (nonatomic,strong)NSArray<Person_8*> *personArr;
@end
@implementation Students_8
/**
返回数组的个数
*/
-(NSInteger)countOfPersonArr{
NSLog(@"countOfPersonArr");
return 5;
}
/**
传入需要取的 indexes下标,返回一个数组
*/
-(NSArray *)personArrAtIndexes:(NSIndexSet *)indexes{
NSLog(@"personArrAtIndexes---");
return @[@"a",@"b",@"c",@"d",@"e"];
}
/**
传入一个 index 返回数组内的 index下标对应的值
*/
-(id)objectInPersonArrAtIndex:(NSUInteger)index{
NSLog(@"objectInPersonArrAtIndex");
return @"*_*";
}
-(void)getPersonArr:(Person_8 **)buffer range:(NSRange)inRange{
NSLog(@"getPersonArr-buffer-inRange");
}
@end
//===================================
@interface ViewController8 ()
@end
@implementation ViewController8
- (void)viewDidLoad {
[super viewDidLoad];
Students_8 *stu = [Students_8 new];
//调用了countOfPersonArr、getPersonArr-buffer-inRange
NSLog(@"%@",[stu valueForKey:@"personArr"]);
NSLog(@"=========11111========");
//调用了countOfPersonArr
NSLog(@"%lu",[[stu valueForKey:@"personArr"] count]);
NSLog(@"=========22222========");
//调用了objectInPersonArrAtIndex
NSLog(@"%@",[[stu valueForKey:@"personArr"] objectAtIndex:0]);
NSLog(@"=========33333========");
//调用personArrAtIndexes
NSLog(@"%@",[[stu valueForKey:@"personArr"] objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 3)]]);
NSLog(@"=========44444========");
}
/*
* 索引访问器(index accessor) 针对NSArray这样的有序集合
* getter索引访问器:(需实现的方法)
- countOf<Key>
- objectIn<Key>AtIndex: 或者 -<Key>AtIndexes:
- get<Key>:range:
*/
@end
2.固定无序集合属性(NSSet)
#import "ViewController9.h"
@interface Person_9 : NSObject
//@property (nonatomic,strong)NSSet *namesSet;
@end
@implementation Person_9
/**
返回set的个数
*/
-(NSUInteger)countOfNamesSet{
NSLog(@"countOfNamesSet");
return 5;
}
/**
是否包含对象
*/
-(NSString *)memberOfNamesSet:(NSString *)object{
NSLog(@"memberOfNamesSet");
return object;
}
/**
返回 遍历器
*/
-(NSEnumerator *)enumeratorOfNamesSet{
NSLog(@"enumeratorOfNamesSet");
return [[[NSSet alloc]init] objectEnumerator];
}
@end
//====================
@interface ViewController9 ()
@end
@implementation ViewController9
- (void)viewDidLoad {
[super viewDidLoad];
Person_9 *p = [[Person_9 alloc]init];
NSLog(@"=======111========");
NSLog(@"%lu",[[p valueForKey:@"namesSet"] count]);//调用countOfNamesSet
NSLog(@"=======222========");
NSLog(@"%@",[[p valueForKey:@"namesSet"] member:@"AAA"]);//调用memberOfNamesSet
NSLog(@"=======333========");
NSLog(@"%d",[[p valueForKey:@"namesSet"] containsObject:@"BBB"]);//调用memberOfNamesSet
NSLog(@"=======444========");//调用enumeratorOfNamesSet
[[p valueForKey:@"namesSet"] objectEnumerator];
NSLog(@"=======555========");
}
/*
无序访问器: 针对 NSSet样的无序集合
getter无序访问器其实现如下:
- countOf<Key>
- enumeratorOf<Key>
- memberOf<Key>:
*/
@end
3.可变有序集合属性(NSMutableArray)
#import "ViewController10.h"
@interface Person_10 : NSObject
//@property (nonatomic,strong)NSMutableArray *namesArrM;
@end
@implementation Person_10
//--------------------------
//插入一个数组对象
-(void)insertNamesArrM:(NSArray *)array atIndexes:(NSIndexSet *)indexes{
NSLog(@"insertNamesArrM");
}
//插入一个对象
-(void)insertObject:(NSString *)object inNamesArrMAtIndex:(NSUInteger)index{
NSLog(@"insertObject");
}
//移除一个数组
-(void)removeNamesArrMAtIndexes:(NSIndexSet *)indexes{
NSLog(@"removeNamesArrMAtIndexes");
}
//移除一个对象
-(void)removeObjectFromNamesArrMAtIndex:(NSUInteger)index{
NSLog(@"removeObjectFromNamesArrMAtIndex");
}
//根据下标,替换数组
-(void)replaceNamesArrMAtIndexes:(NSIndexSet *)indexes withNamesArrM:(NSArray *)array{
NSLog(@"replaceNamesArrMAtIndexes");
}
//根据下标替换一个对象
-(void)replaceObjectInNamesArrMAtIndex:(NSUInteger)index withObject:(id)object{
NSLog(@"replaceObjectInNamesArrMAtIndex");
}
@end
//====================================================
@interface ViewController10 ()
@end
@implementation ViewController10
- (void)viewDidLoad {
[super viewDidLoad];
Person_10 *p = [[Person_10 alloc]init];
NSLog(@"======111=====");
//会调用:insertObject
[[p mutableArrayValueForKey:@"namesArrM"] insertObject:@"1" atIndex:0];
NSLog(@"======222=====");
//会调用:insertNamesArrM
[[p mutableArrayValueForKey:@"namesArrM"] insertObjects:@[@"q",@"w"] atIndexes:[NSIndexSet indexSetWithIndex:0]];
NSLog(@"======333=====");
//会调用:removeNamesArrMAtIndexes
[[p mutableArrayValueForKey:@"namesArrM"] removeObjectsAtIndexes:[NSIndexSet indexSetWithIndex:0]];
NSLog(@"======444=====");
//会调用:removeObjectFromNamesArrMAtIndex
[[p mutableArrayValueForKey:@"namesArrM"] removeObjectAtIndex:0];
NSLog(@"======555=====");
//会调用:replaceObjectInNamesArrMAtIndex
[[p mutableArrayValueForKey:@"namesArrM"] replaceObjectAtIndex:0 withObject:@"1"];
NSLog(@"======666=====");
//会调用 :replaceNamesArrMAtIndexes
[[p mutableArrayValueForKey:@"namesArrM"] replaceObjectsAtIndexes:[NSIndexSet indexSetWithIndex:0] withObjects:@[@"1",@"2"]];
}
/*
mutable有序索引访问器: 需实现
-insertObject:in<Key>AtIndex:或者-insert<Key>:atIndexes:
-removeObjectFrom<Key>AtIndex:或者-remove<Key>AtIndexes:
-replaceObjectIn<Key>AtIndex:withObject:或者-replace<Key>AtIndexes:with<Key>:
*/
@end
4.可变无序集合属性(NSMutablSet)
#import "ViewController11.h"
@interface Person_11 : NSObject
//@property (nonatomic,strong)NSMutableSet *namesSetM;
@end
@implementation Person_11
//添加多个对象、并集
-(void)addNamesSetM:(NSSet *)objects{
NSLog(@"addNamesSetM");
}
//添加单个对象
-(void)addNamesSetMObject:(NSString *)object{
NSLog(@"addNamesSetMObject");
}
//移除多个对象
-(void)removeNamesSetM:(NSSet *)objects{
NSLog(@"removeNamesSetM");
}
//移除单个对象
-(void)removeNamesSetMObject:(NSString *)object{
NSLog(@"removeNamesSetMObject");
}
//交集
-(void)intersectNamesSetM:(NSSet *)objects{
NSLog(@"intersectNamesSetM");
}
@end
@interface ViewController11 ()
@end
@implementation ViewController11
- (void)viewDidLoad {
[super viewDidLoad];
Person_11 *p = [[Person_11 alloc]init];
NSLog(@"=====1111======");
//调用:addNamesSetMObject
[[p mutableSetValueForKey:@"namesSetM"] addObject:@"111"];
NSLog(@"=====2222======");
//调用:addNamesSetM
[[p mutableSetValueForKey:@"namesSetM"] unionSet:[NSSet setWithObjects:@"1",@"2",nil]];//并集
NSLog(@"=====3333======");
//调用:removeNamesSetMObject
[[p mutableSetValueForKey:@"namesSetM"] removeObject:@"1"];
NSLog(@"=====4444======");
//调用:removeNamesSetM
[[p mutableSetValueForKey:@"namesSetM"] minusSet:[NSSet setWithObjects:@"1", nil]];//删除前者中与后者相同的元素
NSLog(@"=====5555======");
//调用:intersectNamesSetM
[[p mutableSetValueForKey:@"namesSetM"] intersectSet:[NSSet setWithObjects:@"1",@"2",nil]]; //交集
}
/*
mutable无序访问器实现如下方法:
- add<Key>Object:或者-add<Key>:
- remove<Key>Object:或者-remove<Key>:
- intersect<Key>:
*/
@end
5.可变有序集合(NSMutableOrderedSet)
#import "ViewController16.h"
@interface ViewController16 ()
//@property (nonatomic,strong)NSMutableOrderedSet *orderedSetM;
@end
@implementation ViewController16
////插入单个对象
//-(void)insertObject:(NSString *)object inOrderedSetMAtIndex:(NSUInteger)index{
// NSLog(@"insertObject");
//}
//插入多个对象
-(void)insertOrderedSetM:(NSArray *)array atIndexes:(NSIndexSet *)indexes{
NSLog(@"insertOrderedSetM");
}
//替换单个对象
-(void)replaceObjectInOrderedSetMAtIndex:(NSUInteger)index withObject:(id)object{
NSLog(@"replaceObjectInOrderedSetMAtIndex");
}
//替换多个对象(set)
-(void)replaceOrderedSetMAtIndexes:(NSIndexSet *)indexes withOrderedSetM:(NSArray *)array{
NSLog(@"replaceOrderedSetMAtIndexes");
}
//移除单个对象
-(void)removeObjectFromOrderedSetMAtIndex:(NSUInteger)index{
NSLog(@"removeObjectFromOrderedSetMAtIndex");
}
//移除多个对象
-(void)removeOrderedSetMAtIndexes:(NSIndexSet *)indexes{
NSLog(@"removeOrderedSetMAtIndexes");
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"====1111=====");
[[self mutableOrderedSetValueForKey:@"orderedSetM"] insertObject:@"1" atIndex:0];
NSLog(@"====2222=====");
[[self mutableOrderedSetValueForKey:@"orderedSetM"] insertObjects:@[@"1",@"2"] atIndexes:[NSIndexSet indexSetWithIndex:0]];
NSLog(@"====3333=====");
[[self mutableOrderedSetValueForKey:@"orderedSetM"] replaceObjectAtIndex:0 withObject:@"1"];
NSLog(@"====4444=====");
[[self mutableOrderedSetValueForKey:@"orderedSetM"] replaceObjectsAtIndexes:[NSIndexSet indexSetWithIndex:0] withObjects:@[@"1",@"2"]];
NSLog(@"====5555=====");
[[self mutableOrderedSetValueForKey:@"orderedSetM"] removeObjectAtIndex:0];
NSLog(@"====6666=====");
[[self mutableOrderedSetValueForKey:@"orderedSetM"] removeObjectsAtIndexes:[NSIndexSet indexSetWithIndex:0]];
}
/*
mutable有序的集合 需实现
insertObject:in<Key>AtIndex: 或 insert<Key>:atIndexes:
removeObjectFrom<Key>AtIndex: 或 remove<Key>AtIndexes:
replaceObjectIn<Key>AtIndex:withObject: 或 replace<Key>AtIndexes:with<Key>:
注意:可以实现多个对象的方法,当有对于多个对象的操作(需要实现多个处理的相应的方法)
*/
@end
6.集合操作符
#import "ViewController15.h"
//#import <objc/runtime.h>
@interface Transaction : NSObject
@property (nonatomic) NSString* payee; // To whom
@property (nonatomic) NSNumber* amount; // How much
@property (nonatomic) NSDate* date; // When
@end
@implementation Transaction
@end
//==========================
@interface ViewController15 ()
@property (nonatomic,strong)NSArray *transactions;
@end
@implementation ViewController15
- (void)viewDidLoad {
[super viewDidLoad];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
Transaction *t1 = [[Transaction alloc]init];
t1.payee = @"Jack";
t1.amount = @(100);
t1.date = [formatter dateFromString:@"2018-12-01 01:23:10"];
Transaction *t2 = [[Transaction alloc]init];
t2.payee = @"Lucy";
t2.amount = @(200);
t2.date = [formatter dateFromString:@"2018-09-10 01:20:00"];
Transaction *t3 = [[Transaction alloc]init];
t3.payee = @"Lucy";
t3.amount = @(300);
t3.date = [formatter dateFromString:@"2018-10-10 09:20:00"];
self.transactions = @[t1,t2,t3];
//集合操作符(例子)
//格式:keyPathToCollection.@collectionOperator.keypathTpProperty
//集合的keypath.操作符.要操作的属性
//1.Aggregation Operators(聚合运算符)
NSNumber *transactionAverage = [self valueForKeyPath:@"transactions.@avg.amount"];
NSLog(@"平均消费:%@",transactionAverage);//平均消费:200
NSNumber *numberOfTransactions = [self.transactions valueForKeyPath:@"@count"];
NSLog(@"消费者个数:%@",numberOfTransactions);//消费者个数:3
NSDate *latestDate = [self.transactions valueForKeyPath:@"@max.date"];
NSLog(@"最近消费的时间:%@",latestDate);//最近消费的时间:2018-11-30 17:23:10 +0000
NSDate *earliestDate = [self.transactions valueForKeyPath:@"@min.date"];
NSLog(@"最早消费的时间:%@",earliestDate);//最早消费的时间:2018-09-09 17:20:00 +0000
NSNumber *amountSum = [self.transactions valueForKeyPath:@"@sum.amount"];
NSLog(@"消费的总和:%@",amountSum);//消费的总和:600
//2.Array Operators(集合操作符)
//@distinctUnionOfObjects(返回操作对象内部的不同对象,返回值为数组)
NSArray *distinctPayees = [self.transactions valueForKeyPath:@"@distinctUnionOfObjects.payee"];
NSLog(@"消费者是:%@",distinctPayees);
//@unionOfObjects(返回操作对象内部的所有对象)
NSArray *payees = [self.transactions valueForKeyPath:@"@unionOfObjects.payee"];
NSLog(@"所有消费记录上的消费者(或许有重复的):%@",payees);
//3.Nesting Operators(嵌套操作符)
Transaction *t4 = [[Transaction alloc]init];
t4.payee = @"Jack";
t4.amount = @(1000);
t4.date = [formatter dateFromString:@"2019-12-01 01:23:10"];
NSArray* moreTransactions = @[t4];
NSArray* arrayOfArrays = @[self.transactions, moreTransactions,self.transactions];
//@distinctUnionOfArrays(返回操作对象(且操作对象内对象必须是数组/集合)中数组/集合的不同对象,返回值为数组->去重)
NSArray *collectedDistinctPayees = [arrayOfArrays valueForKeyPath:@"@distinctUnionOfArrays.payee"];
NSLog(@"消费者(去重):%@",collectedDistinctPayees);
//@unionOfArrays(返回操作对象(且操作对象内对象必须是数组/集合)中数组/集合的所有对象,返回值为数组)
NSArray *collectedPayees = [arrayOfArrays valueForKeyPath:@"@unionOfArrays.payee"];
NSLog(@"所有消费记录上的消费者数组(或许有重复的)%@",collectedPayees);
NSSet *setA = [NSSet setWithObjects:t1, t2, nil];
NSSet *setB = [NSSet setWithObjects:t3, t4, nil];
NSSet *set = [NSSet setWithObjects:setA, setB, nil];
//@distinctUnionOfSets(返回操作对象(且操作对象内对象必须是数组/集合)中数组/集合的所有对象,返回值为集合)
NSSet *payeesSet = [set valueForKeyPath:@"@distinctUnionOfSets.payee"];
NSLog(@"所有消费记录上的消费者集合(没有重复的)%@",payeesSet);
}
@end
友情链接: