欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS 對NSMutableArray進(jìn)行排序和過濾的實例

 更新時間:2018年01月15日 09:17:36   作者:鍵盤舞者113  
下面小編就為大家分享一篇iOS 對NSMutableArray進(jìn)行排序和過濾的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

根據(jù)實體類的屬性進(jìn)行排序,這個屬性排序還可以增加幾個排序規(guī)則,前后的規(guī)則先判斷,如果一樣的則根據(jù)第二個排序規(guī)則來判斷

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"data" ascending:YES];//其中,price為數(shù)組中的對象的屬性,這個針對數(shù)組中存放對象比較更簡潔方便 
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1]; 
[self.dataArray sortUsingDescriptors:sortDescriptors]; 

過濾不會對原來的數(shù)據(jù)產(chǎn)生影響,而是生成符合過濾條件的NSArray數(shù)據(jù)

NSPredicate *apredicate=[NSPredicate predicateWithFormat:@"id>=%ld AND id<=%ld",3,5]; 
 
NSArray *newArr=[self.dataArray filteredArrayUsingPredicate:apredicate];

下面是個例子可以運行看看

#import <Foundation/Foundation.h> 
 
@interface TestBean : NSObject 
@property(nonatomic,assign)int id; 
@property(nonatomic,strong)NSString* data; 
@end 
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) NSArray
*list;

@end

//
// ViewController.m
// First
//
// Created by shanreal-iOS on 17/10/16.
// Copyright © 2017年 shanreal.LongZhenHao. All rights reserved.
//
#import "ViewController.h"
#import "TestBean.h"
@interface ViewController ()
@property(nonatomic,strong)NSMutableArray* dataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view.
 _dataArray = [NSMutableArray array];
 
 TestBean* one = [[TestBean alloc]init];
 one.id = 1;
 one.data = @"one";
 [_dataArray addObject:one];
 
 one = [[TestBean alloc]init];
 one.id = 2;
 one.data = @"two";
 [_dataArray addObject:one];
 
 one = [[TestBean alloc]init];
 one.id = 22;
 one.data = @"two2";
 [_dataArray addObject:one];
 
 one = [[TestBean alloc]init];
 one.id = 3;
 one.data = @"three";
 [_dataArray addObject:one];
 
 one = [[TestBean alloc]init];
 one.id = 4;
 one.data = @"four";
 [_dataArray addObject:one];
 
 
 
 one = [[TestBean alloc]init];
 one.id = 44;
 one.data = @"four4";
 [_dataArray addObject:one];
 
 one = [[TestBean alloc]init];
 one.id = 5;
 one.data = @"five";
 [_dataArray addObject:one];
 
 one = [[TestBean alloc]init];
 one.id = 6;
 one.data = @"six";
 [_dataArray addObject:one];
 
 [self oneClick];
 [self twoClick];
 
}
-(void)oneClick{
 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"data" ascending:YES];//其中,price為數(shù)組中的對象的屬性,這個針對數(shù)組中存放對象比較更簡潔方便
 NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
 [self.dataArray sortUsingDescriptors:sortDescriptors];
 
 [self logArray:self.dataArray];
}
-(void)twoClick{
 NSPredicate *apredicate=[NSPredicate predicateWithFormat:@"id>=%ld AND id<=%ld",3,5];
 
 NSArray *newArr=[self.dataArray filteredArrayUsingPredicate:apredicate];
 [self logArray:newArr];
}
-(void)logArray:(NSArray*)array{
 NSLog(@"---------------------------------");
 for(TestBean* bean in array){
  NSLog(@"%d %@",bean.id,bean.data);
 }
}

@end

以上這篇iOS 對NSMutableArray進(jìn)行排序和過濾的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS App開發(fā)中UITextField組件的常用屬性小結(jié)

    iOS App開發(fā)中UITextField組件的常用屬性小結(jié)

    這篇文章主要介紹了iOS App開發(fā)中UITextField組件的常用屬性小結(jié),文中還介紹了UITextField隱藏鍵盤及為內(nèi)容增加校驗的兩個使用技巧,需要的朋友可以參考下
    2016-04-04
  • iOS在頁面銷毀時如何優(yōu)雅的cancel網(wǎng)絡(luò)請求詳解

    iOS在頁面銷毀時如何優(yōu)雅的cancel網(wǎng)絡(luò)請求詳解

    這篇文章主要給大家介紹了關(guān)于iOS在頁面銷毀時如何優(yōu)雅的cancel網(wǎng)絡(luò)請求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • iOS的UI開發(fā)中UITabBarControlle的基本使用教程

    iOS的UI開發(fā)中UITabBarControlle的基本使用教程

    這篇文章主要介紹了iOS的UI開發(fā)中UITabBarControlle的基本使用教程,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-12-12
  • iOS自定義View實現(xiàn)卡片滑動

    iOS自定義View實現(xiàn)卡片滑動

    這篇文章主要為大家詳細(xì)介紹了ios自定義View實現(xiàn)卡片滑動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • iOS10推送教程詳解

    iOS10推送教程詳解

    這篇文章主要為大家詳細(xì)介紹了iOS10推送開發(fā)教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • iOS實現(xiàn)圖片保存與搜索功能

    iOS實現(xiàn)圖片保存與搜索功能

    這篇文章主要介紹了iOS實現(xiàn)圖片保存與搜索功能的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • xcode 4 制作靜態(tài)庫圖文詳解

    xcode 4 制作靜態(tài)庫圖文詳解

    我這個文檔的靜態(tài)庫的開發(fā)是基于Xcode4.2和iOS SDK5.0編寫的。Xcode4跟之前的Xcode3還是有不少的差別的
    2013-06-06
  • iOS實現(xiàn)賬號、密碼記住功能

    iOS實現(xiàn)賬號、密碼記住功能

    這篇文章主要為大家詳細(xì)介紹了iOS實現(xiàn)賬號、密碼記住功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • IOS 遠(yuǎn)程通知兼容(IOS7,IOS8)實例詳解

    IOS 遠(yuǎn)程通知兼容(IOS7,IOS8)實例詳解

    這篇文章主要介紹了IOS 遠(yuǎn)程通知兼容(IOS7,IOS8)實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • IOS判斷字符串是否有空格實例

    IOS判斷字符串是否有空格實例

    在我們大家日常開發(fā)的時候,經(jīng)常會需要對注冊,登錄,忘記密碼等功能的密碼進(jìn)行判斷是否包含空格,下面這篇文章給大家分享了自己封裝的一個方法,有需要的可以參考借鑒。
    2016-09-09

最新評論