iOS中比較兩個數組是否一樣

<pre name="code" class="objc">     //把數據源拿出來創建臨時的數組,不要直接使用數據源
    NSArray *answer = @[@1, @2, @3];//答案數組
    NSArray *select = @[@3, @2,@1];    //用戶1選的選項
    if ([answer isEqualToArray:select]) {
        //一樣就是對的
        NSLog(@"1對的 ");
    }else
    {
        //不一樣就是錯的
        //拿出來answer 和 select 中一樣的
        NSArray *selectTure = [answer filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in %@", select]];        NSLog(@"用戶1選擇對的 -> %@", selectTure);
        NSArray *selectWrong = [select filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT (SELF in %@)", answer]];
        NSLog(@"用戶1選擇是錯的 -> %@", selectWrong);
        NSArray *unselectTure = [answer filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT (SELF in %@)", selectTure]];
        NSLog(@"用戶1沒選擇的正確答案 -> %@", unselectTure);
    }
    
    //把數據源拿出來創建臨時的數組,不要直接使用數據源
    NSArray *answer1 = @[@1, @2, @3,@4,@5];//答案數組
    NSArray *select1 = @[@3, @4,@7];    //用戶2選的選項
    if ([answer isEqualToArray:select]) {
        //一樣就是對的
        NSLog(@"2對的 ");
    }else
    {
        //不一樣就是錯的
        //拿出來answer 和 select 中一樣的
        NSArray *selectTure = [answer1 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in %@", select1]];        NSLog(@"用戶2選擇對的 -> %@", selectTure);
        NSArray *selectWrong = [select1 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT (SELF in %@)", answer1]];
        NSLog(@"用戶2選擇是錯的 -> %@", selectWrong);
        NSArray *unselectTure = [answer1 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT (SELF in %@)", selectTure]];
        NSLog(@"用戶2沒選擇的正確答案 -> %@", unselectTure);
    }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章