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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值