版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2017.08.26 |
前言
NSArray
是數組的不變數組類,不邊數組在初始化的時候元素就是不變的,不能更改任何一個元素,實際上我們用的較多的是可變數組,因為很多時候我們都需要對數組元素進行增刪改查,其中增刪改也只有可變數組可以做,也就是說可變數組相對來說更加靈活,這幾篇我們就說一下可變數組的這個類及其相關知識,還是老規矩從整體到局部,從淺入深進行講解,謝謝大家。感興趣的可以看我寫的上面幾篇。
1. NSMutableArray簡單細說(一)—— 整體了解
2. NSMutableArray簡單細說(二)—— 創建和初始化
3. NSMutableArray簡單細說(三)—— 數組元素的增加
4. NSMutableArray簡單細說(四)—— 數組元素的刪除
5. NSMutableArray簡單細說(五)—— 更換數組中的對象
一、- (void)filterUsingPredicate:(NSPredicate *)predicate;
該方法的作用是:根據數組的內容評估給定的謂詞,只留下匹配的對象。
下面看示例代碼
- (void)demoFilterUsingPredicate
{
NSArray *array1 = [NSArray arrayWithObjects:@1,@2,@3,@5,@6,@7, nil];
NSArray *array2 = [NSArray arrayWithObjects:@4,@5, nil];
// 表示篩選array1在array2中的元素!YES!其中SELF指向filteredArrayUsingPredicate的調用者。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF in %@",array2];
NSArray *resultArr = [array1 filteredArrayUsingPredicate:predicate];
NSLog(@"resultArr = %@", resultArr);
}
下面看輸出結果
2017-08-26 23:39:08.135 JJOC[18027:483439] resultArr = (
5
)
結論:數組元素的過濾。
二、- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;
該方法的作用就是:交換數組中兩個索引處的對象。
下面看示例代碼
- (void)demoExchangeObjectAtIndex
{
NSMutableArray *arrM = [NSMutableArray arrayWithObjects: @"one", @"two", @"three", @"Four", @"Five", nil];
[arrM exchangeObjectAtIndex:0 withObjectAtIndex:3];
NSLog(@"arrM = %@", arrM);
}
下面看輸出結果
2017-08-26 23:42:17.248 JJOC[18162:486420] arrM = (
Four,
two,
three,
one,
Five
)
結論:交換兩個索引處的對象。
三、- (void)sortUsingDescriptors:(NSArray<NSSortDescriptor *> *)sortDescriptors;
該方法的作用就是:利用給定的排序描述符,對對象進行排序。
看一下參數
-
sortDescriptors
:包含用于對接收數組的內容進行排序的NSSortDescriptor
對象的數組。
看示例代碼
- (void)demoSortDescription
{
NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"2030",@"year", @"1",@"month",nil];
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"2010",@"year", @"2",@"month", nil];
NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"2050",@"year", @"3",@"month" ,nil];
NSDictionary *dic4 = [NSDictionary dictionaryWithObjectsAndKeys:@"2014",@"year", @"4",@"month",nil];
NSDictionary *dic5 = [NSDictionary dictionaryWithObjectsAndKeys:@"2050",@"year", @"4",@"month",nil];
NSMutableArray *arrM = [NSMutableArray arrayWithObjects:dic1, dic2, dic3, dic4, dic5, nil];
NSSortDescriptor *descripor = [NSSortDescriptor sortDescriptorWithKey:@"year" ascending:YES];
NSSortDescriptor *descripor2 = [NSSortDescriptor sortDescriptorWithKey:@"month" ascending:YES];
[arrM sortUsingDescriptors:[NSArray arrayWithObjects:descripor, descripor2, nil]];
NSLog(@"resultArr = %@", arrM);
}
看輸出結果
2017-08-26 23:51:46.047 JJOC[18488:495473] resultArr = (
{
month = 2;
year = 2010;
},
{
month = 4;
year = 2014;
},
{
month = 1;
year = 2030;
},
{
month = 3;
year = 2050;
},
{
month = 4;
year = 2050;
}
)
結論:利用排序描述符進行排序。
四、- (void)sortUsingComparator:(NSComparator)cmptr;
該方法的作用是:使用給定的NSComparator
塊指定的比較方法對接收器進行升序排序。
下面看示例代碼
- (void)demoSortUsingComparator
{
NSMutableArray *arrM = [NSMutableArray arrayWithObjects: @"one", @"two", @"three", @"Four", @"Five", nil];
[arrM sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj2 compare:obj1];
}];
NSLog(@"arrM = %@", arrM);
}
看輸出結果
2017-08-27 00:01:05.027 JJOC[18709:503123] arrM = (
two,
three,
one,
Four,
Five
)
結論:比較器進行數組元素的比較。
五、- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr;
該方法的作用就是:使用指定的選項和由給定的NSComparator
塊指定的比較方法按升序對接收方進行排序。
下面看一下參數:
-
opts
:指定排序選項的位掩碼(是否應同時執行,是否應穩定執行)。 -
cmptr
:比較器的塊代碼。
下面看示例代碼
- (void)demoSortWithOptions
{
NSMutableArray *arrM = [NSMutableArray arrayWithObjects: @"one", @"two", @"three", @"Four", @"Five", nil];
[arrM sortWithOptions:NSSortStable usingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj2 compare:obj1];
}];
NSLog(@"arrM = %@", arrM);
}
看輸出結果
2017-08-27 00:06:11.983 JJOC[18900:508769] arrM = (
two,
three,
one,
Four,
Five
)
結論:帶有選項比較器的排序。
六、- (void)sortUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void *))compare context:(void *)context;
該方法的作用是:按照比較功能比較定義的升序對接收器進行排序。
看一下參數:
-
compare
:用于比較兩個元素的比較函數。函數的參數是要比較的兩個對象和上下文參數,上下文。 如果第一個元素小于第二個元素,則該函數應返回NSOrderedAscending
,如果第一個元素大于第二個元素,則返回NSOrderedDescending
,如果元素相等則返回NSOrderedSame
。 -
context
:傳遞比比較函數的上下文。
還要注意:
- 該方法允許比較基于一些外部參數,例如字符排序是區分大小寫還是不區分大小寫。
結論:使用函數進行排序。
七、- (void)sortUsingSelector:(SEL)comparator;
該方法的作用是:按照由給定選擇器指定的比較方法確定的升序排序接收器。
這里看一下參數:
-
comparator
:一個選擇器,指定比較方法來比較數組中的元素。比較器消息被發送到數組中的每個對象,并且在數組中具有另一個對象。 如果數組小于參數,則比較器方法應返回NSOrderedAscending
,如果數組大于參數,NSOrderedDescending
,如果它們相等則返回NSOrderedSame
。
下面看示例代碼
- (void)demoSortedArrayUsingSelector
{
NSMutableArray *arrM = [NSMutableArray arrayWithObjects: @"six", @"two", @"three", @"four", @"five", nil];
[arrM sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"arrM = %@", arrM);
}
下面看輸出結果
2017-08-27 00:32:38.649 JJOC[20166:534702] arrM = (
six,
two,
three,
four,
five
)
結論:采用指定方法直接排序。
后記
未完,待續~~~