2016-09-21 4 views
0

Ich erhalte eine JSON-Daten und Anhängen an ein Tupel gibt es Duplikate in diesem Array gibt es eine Möglichkeit, ich kann sie entfernen? Hier ist, wie ich die json Daten bekommen und an die TupelSo entfernen Sie Duplikat aus Tupel

if let hru = ($0["Menu"]["title"]).string{ 
    let uw = ($0["name"]).string 
    let tagloc = (tag: hru, location: uw) 
    self.resultTuples.append(tagloc) 
    } 

Am Drucken des Tupels ähnliche

for var i = 0; i < self.resultTuples.count; ++i{ 
     print(self.resultTuples[i]) 
} 

Aber was gedruckt wird ist

tag: Rice Dishes 
tag: Rice Dishes 
tag: Cakes and Creams 
tag: Cakes and Creams 
tag: Cakes and Creams 
tag: Pastries and Others 
tag: Pastries and Others 
tag: Pastries and Others 
tag: Pastries and Others 
tag: Pastries and Others 
tag: Pastries and Others 
tag: Pastries and Others 
tag: Pastries and Others 
tag: Pastries and Others 
tag: Pastries and Others 
tag: Soups and Sauces 
tag: Soups and Sauces 
.... 

ich entferne Einstellung alle Duplikate aus diesem Tupel

EDIT:

Array hat nicht funktioniert, ich habe ein Modell Menus

if let hru = ($0["Menu"]["title"]).string{ 

    var men = Menus(nam: hru) 
     let set = NSSet(array: self.menus) 
    self.menus = set.allObjects as! [Menus] 
     self.menus.append(men) 

    } 

    for i in self.menus{ 
      print("MENUSS \(i.name)") 

     } 
+0

Mögliche Duplikat [Entfernen Sie doppelte Objekte in einem Array] (http://stackoverflow.com/questions/34709066/remove-duplicate-objects-in-an-array) – dfri

+0

Dies ist ein Tupel – lordxx

+0

Ich habe gerade überprüft :/sadly tuple ist kein Objekt, funktioniert nicht mit AnyObject, also kann man es nicht in Set oder NSSet umwandeln, ich denke Tupel hat ziemlich die eingeschränkte Funktion, also schlage ich vor, dass man Struct oder Class verwendet, dann wird es besser, Refactor zu Struct wie unten Antwort bereitgestellt – Tj3n

Antwort

1

Wenn Sie einen Modellwert wie eine Struktur anstelle eines Tupels

struct TagAndLocation: Hashable { 

    let tag: String 
    let location: String 

    var hashValue: Int { return tag.hashValue } 
} 

func ==(left:TagAndLocation, right: TagAndLocation) -> Bool { 
    return left.tag == right.tag && left.location == right.location 
} 

können Sie nutzen, um die Set Funktionalitäten Duplikate

let results: [TagAndLocation] = ... 
let uniqueResults = Array(Set(results)) 

Bitte beachten Sie, dass in diesem Fall, dass Sie die ursprüngliche Sortierreihenfolge verlieren zu entfernen.

+0

Ja, das hat nicht funktioniert – lordxx

+0

@lordxx: Was ist das Problem? –

+0

Ok, wenn ich versucht habe mit Array funktioniert es nicht, entweder ich aktualisierte die Frage – lordxx

0

Sie überprüfen können, wenn ein Tupel in einer bestimmten Anordnung vor dem Einsetzen mit der folgenden Funktion enthalten ist:

func containsTuple(arr: [(String, String)], tup:(String, String)) -> Bool {  
    let (c1, c2) = tup 

    for (v1, v2) in arr { 
     if v1 == c1 && v2 == c2 { 
      return true 
     } 
    } 

    return false 
} 

Mehr Infos hier How do I check if an array of tuples contains a particular one in Swift?

Verwandte Themen