How to find objects from first array in second array? Swift iOS -


i have question , unfortunately not found anywhere responses.

i'll describe problem in simple example. have array of objects. each object contains id, name, count. array result of parsing data server. taken first.

i have second array taken server. same array id, name, count.

my question follows. first array has 20 elements, each of element want compare array of other. second array parse. may need use loop , check whether every element in first array in second or not?

// //  viewcontroller.swift //  parsesearching // //  created mateusz fraczek on 21.07.2015. //  copyright (c) 2015 universeldev. rights reserved. //  import uikit import parse  struct model : equatable {     var ide: string!     var name: string!     var count: string! }  func ==(a: model, b: model) -> bool {     return a.ide == b.ide && a.name == b.name && a.count == b.count }  extension array {      func indexesofsubset<t : equatable>(objects : [t]) -> [int] {          // create storage filtered objects , results         var unusedobjects = objects         var result : [int] = []          // enumerate through objects in array         (index, obj) in enumerate(self) {              // enumerate again through objects has not been found             x in unusedobjects {                  // if hit match, append result, remove usused objects                 if obj as! t == x {                     result.append(index)                     unusedobjects = unusedobjects.filter( { $0 != x } )                     break                 }             }         }          // results         return result     } }  class viewcontroller: uiviewcontroller {      override func viewdidload() {         super.viewdidload()          var firstarray = [model]()         var resultsarray = [model]()           firstarray.append(model(ide: "xyz1", name: "name1", count: "0"))         firstarray.append(model(ide: "xyz2", name: "name2", count: "0"))         firstarray.append(model(ide: "xyz3", name: "name3", count: "0"))         firstarray.append(model(ide: "xyz4", name: "name4", count: "0"))  //        let testobject = pfobject(classname: "testobject") //        testobject["ide"] = "xyz1" //        testobject["name"] = "name1" //        testobject["count"] = "0" //        testobject.saveinbackgroundwithblock { (success: bool, error: nserror?) -> void in //            println("object has been saved.") //        }           var query = pfquery(classname: "testobject")          query.findobjectsinbackgroundwithblock { (objects, error) -> void in             if let object = objects as? [pfobject] {                 obj in object {                     var id = obj.objectforkey("ide") as! string                     var name = obj.objectforkey("name") as! string                     var count = obj.objectforkey("count") as! string                      var model = model(ide: id, name: name, count: count)                      resultsarray.append(model)                     println(resultsarray)                     let indexes = firstarray.indexesofsubset(resultsarray)                      println("indexes \(indexes) ")                 }             }         }        }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }    } 

in order find indexes of subset, better use new in swift, say? lets use generics , make solution work in future, when object changes, or use different objects altogether. lets create extension array:

extension array {      func indexesofsubset<t : equatable>(objects : [t]) -> [int] {          // create storage filtered objects , results         var unusedobjects = objects         var result : [int] = []          // enumerate through objects in array         (index, obj) in enumerate(self) {              // enumerate again through objects has not been found             x in unusedobjects {                  // if hit match, append result, remove usused objects                 if obj as! t == x {                     result.append(index)                     unusedobjects = unusedobjects.filter( { $0 != x } )                     break                 }             }         }          // results         return result     } } 

now can use method find objects using:

let sameindexes = alldata.indexesofsubset(fetcheddata) 

of course, fetched data , data must same data type, in case [model]. have make sure can compare 2 objects properly, that, have create comparator taking data type this:

public func ==(a: model, b: model) -> bool {     return a.id == b.id && a.name == b.name && a.count == b.count } 

also, in order work, have make object conform equatable protocol, that:

struct model : equatable { 

now when compare 2 same model objects, compare == true , not error :) hope helps!

edit: here test gist can find including stuff comments + how use it


Comments