iOS 根据条件判断数组元素是否存在及找出该元素

在开发中我们经常会遇到根据某个条件来判断数组中某个元素是否存在的情况,一般我们会使用for循环,然后在循环里使用if进行判断。其实我们也可以使用NSPredicate过滤器来快速实现该功能。

其核心代码如下:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.type == %d", (int)type];
NSArray *results = [devices filteredArrayUsingPredicate:predicate];
return results.count > 0;

NSPredicate过滤器的更多使用请参考👉:iOS开发 NSPredicate的使用方法

1. 使用示例
Device *device0 = [[Device alloc] init];
device0.mac = @"A1CC2D1F18A7";
device0.type = 1;

Device *device1 = [[Device alloc] init];
device1.mac = @"A1CC2D1F18A8";
device1.type = 2;

NSArray *devices = @[device0, device1];
    
BOOL exists0 = [Device existsDeviceForMac:@"A1CC2D1F18A8" inDevices:devices];
BOOL exists1 = [Device existsDeviceForType:3 inDevices:devices];
Device *device = [Device deviceForMac:@"A1CC2D1F18A8" inDevices:devices];
    
NSLog(@"设备[A1CC2D1F18A8] %@ 于设备列表中", exists0 ? @"已存在" : @"不存在");
NSLog(@"设备[类型为3] %@ 于设备列表中", exists1 ? @"已存在" : @"不存在");
NSLog(@"根据MAC地址[A1CC2D1F18A8]获取的设备: %@", device);

// Xcode控制台日志:
// 2022-03-21 11:30:33.512211+0800 Demo[19114:1324068] 设备[A1CC2D1F18A8] 已存在 于设备列表中
// 2022-03-21 11:30:33.512313+0800 Demo[19114:1324068] 设备[类型为3] 不存在 于设备列表中
// 2022-03-21 11:30:33.512375+0800 Demo[19114:1324068] 根据MAC地址[A1CC2D1F18A8]获取的设备: <Device: 0x280fd5860>
2. Device模型类

Device.h

//
//  Device.h
//  Demo
//
//  Created by Brian Wang on 2022/3/21.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/// 设备
@interface Device : NSObject

/// 设备MAC地址
@property (nonatomic, copy) NSString *mac;

/// 设备类型
@property (nonatomic, assign) NSInteger type;


#pragma mark - Tool Methods

/// 根据 MAC地址 判断某设备是否已存在于列表中
/// @param mac MAC地址 (不区分大小写)
/// @param devices 设备列表
+ (BOOL)existsDeviceForMac:(NSString *)mac inDevices:(NSArray<Device *> *)devices;

/// 根据 设备类型 判断某设备是否已存在于列表中
/// @param type 设备类型
/// @param devices 设备列表
+ (BOOL)existsDeviceForType:(NSInteger)type inDevices:(NSArray<Device *> *)devices;

/// 根据 MAC地址 获取设备
/// @param mac MAC地址 (不区分大小写)
/// @param devices 设备列表
+ (Device *)deviceForMac:(NSString *)mac inDevices:(NSArray<Device *> *)devices;


@end

NS_ASSUME_NONNULL_END

Device.m

//
//  Device.m
//  Demo
//
//  Created by Brian Wang on 2022/3/21.
//

#import "Device.h"

@implementation Device


#pragma mark - Tool Methods

/// 根据 MAC地址 判断某设备是否已存在于列表中
/// @param mac MAC地址 (不区分大小写)
/// @param devices 设备列表
+ (BOOL)existsDeviceForMac:(NSString *)mac inDevices:(NSArray<Device *> *)devices {
    // [c] 表示不区分大小写
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.mac LIKE[c] %@", mac];
    NSArray *results = [devices filteredArrayUsingPredicate:predicate];
    return results.count > 0;
}

/// 根据 设备类型 判断某设备是否已存在于列表中
/// @param type 设备类型
/// @param devices 设备列表
+ (BOOL)existsDeviceForType:(NSInteger)type inDevices:(NSArray<Device *> *)devices {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.type == %d", (int)type];
    NSArray *results = [devices filteredArrayUsingPredicate:predicate];
    return results.count > 0;
}

/// 根据 MAC地址 获取设备
/// @param mac MAC地址 (不区分大小写)
/// @param devices 设备列表
+ (Device *)deviceForMac:(NSString *)mac inDevices:(NSArray<Device *> *)devices {
    // [c] 表示不区分大小写
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.mac LIKE[c] %@", mac];
    NSArray *results = [devices filteredArrayUsingPredicate:predicate];
    if (results.count > 0) {
        return results.firstObject;
    }
    return nil;
}

@end
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容