Jak filtrować tablicę za pomocą elementu w innej tablicy w języku Swift?

Mam dwie tablice

let toBeFiltered = ["star0", "star2", "star1", "star0", "star3", "star4"]
let theFilter = ["star1", "star3"]

Jak filtrować pierwszą tablicę używając drugiej tablicy? W rzeczywistości theFilter można zmieniać dynamicznie, np.

let theFilter = ["star2"]
or maybe
let theFilter = ["star0", "star4", "star2"]

Dzięki za pomoc :)

Author: Asep Bagja Priandana, 2015-10-03

5 answers

Use Set Operations

Set(toBeFiltered).intersection(Set(theFilter))

Czytaj więcej: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html

 32
Author: Arsen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-14 12:03:48

Możesz również filtrować tablicę Struct

struct myStruct
        {
          var userid:String;
          var details:String;
          init() {
            userid = "default value";
            details = "default";
          }

    };
    var f1 = myStruct();
    f1.userid = "1";
    f1.details = "Good boy";

    var f2 = myStruct();
    f2.userid = "2";
    f2.details = "Bad boy";

    var f3 = myStruct();
    f3.userid = "3";
    f3.details = "Gentleman";

    var arrNames1:Array = [f1,f3];

    var arrNames2:Array = [f3,f1,f2];

    let filteredArrayStruct =  arrNames1.filter( { (user: myStruct) -> Bool in
      return arrNames2.contains({ (user1: myStruct) -> Bool in
        return user.userid == user1.userid;
      })
    })
print(filteredArrayStruct)

Dla zestawu należy stosować protokół Hashable

class mytestclass: Hashable
{
  var userid:Int ;
  var details:String;

  var hashValue: Int {
    return self.userid
  }
  init(userid: Int, details:String)
 {
  self.userid = userid;
  self.details = details;
  }
}
func ==(lhs: mytestclass, rhs: mytestclass) -> Bool {
  return lhs.userid == rhs.userid
}

var t1 = mytestclass(userid: 1,details: "Good boy");


var t2 = mytestclass(userid: 2,details: "bad boy");

var t3 = mytestclass(userid: 3,details: "gentle man");


var classArrayNames:Set<mytestclass> = [t1,t2];

var classArrayNames2:Set<mytestclass> = [t3,t1,t2];


 let result =  Set(classArrayNames).intersect(classArrayNames2)
 5
Author: karthikPrabhu Alagu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-02 21:43:18

To chyba dzisiaj temat:) bazując na innej świetnej odpowiedzi, sugerowałbym użycie metody intersect(_:) na Set:

let toBeFiltered = ["star0", "star2", "star1", "star0", "star3", "star4"]
let theFilter = ["star1", "star3"]
let filtered = Set(toBeFiltered).intersect(theFilter)

// => ["star1", "star3"] of type Set<String>

// ...if you actually need an array, you can get one using Array(filtered)
 3
Author: fqdn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-03 23:16:30
let toBeFiltered = ["star0", "star2", "star1", "star0", "star3", "star4"]
let theFilter = ["star1", "star3"]

let filtered = toBeFiltered.filter { theFilter.contains($0) }
 2
Author: xcocoader,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-08 16:48:50

Podczas gdy używanie zestawów zgodnie z propozycją Arsena jest zdecydowanie najbardziej eleganckie, czasami chcesz zachować duplikaty i kolejność :

//: Playground - noun: a place where people can play

import Foundation

extension Collection where Element: Equatable {

    func intersection(with filter: [Element]) -> [Element] {
        return self.filter { element in filter.contains { element == $0 } }
    }

}

let toBeFiltered = ["star0", "star2", "star1", "star0", "star3", "star4", "star1"]
let theFilter = ["star1", "star3"]

let filtered = toBeFiltered.intersection(with: theFilter) // ["star1", "star3", "star1"]
 0
Author: Lausbert,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-07 09:48:38