前不久,同学问了我如何快速的获取到那个设备掉线了。就写了个demo。如下
//
// ViewController.m
// 蓝牙demo
//
// Created by mlh on 16/10/24.
// Copyright © 2016年 mlh. All rights reserved.
//
#import "ViewController.h"
#import "Model.h"
@interface ViewController ()
{
NSArray *dataArr1;//上一次的设备数组
NSArray *dataArr2;//最新一次的设备数组
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化设备数组
dataArr1=@[];
dataArr2=[NSArray array];
NSMutableArray *tempArr=[NSMutableArray array];
for (int i=0; i<5 ;i++) {
Model *model=[[Model alloc]init];
model.ID=[NSString stringWithFormat:@"%zd",i];
[tempArr addObject:model];
}
dataArr2=tempArr;
[self scanDevice];
}
#pragma mark - 模拟扫描设备
-(void)scanDevice{
dataArr1=dataArr2;
[dataArr1 enumerateObjectsUsingBlock:^(Model * obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"dataArr2==%@",obj.ID);
}];
NSMutableArray *tempArr=[NSMutableArray array];
for (int i=0; i<5 ;i+=2) {
Model *model=[[Model alloc]init];
model.ID=[NSString stringWithFormat:@"%zd",i];
[tempArr addObject:model];
}
dataArr2=tempArr;
[dataArr2 enumerateObjectsUsingBlock:^(Model * obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"dataArr2==%@",obj.ID);
}];
[self log];
}
-(void)log{
#pragma mark - 两个数组比较,看看那个设备掉线了
//SELF.ID 就是说是model里面的属性
NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@"NOT(SELF.ID IN %@)",[dataArr2 valueForKey:@"ID"] ];
//过滤数组
NSArray * reslutFilteredArray = [dataArr1 filteredArrayUsingPredicate:filterPredicate];
NSLog(@"Reslut Filtered Array = %@",reslutFilteredArray);
[reslutFilteredArray enumerateObjectsUsingBlock:^(Model * obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@,掉线了",obj.ID);
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end