2016-09-21 123 views
0

我得到一個json數據並將其附加到一個元組中有重複的數組是否有一種方法可以刪除它們?這裏是如何我得到的JSON數據,並將其設置爲元組如何從元組中刪除重複

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

上午印像這樣

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

元組,但什麼獲取印刷是

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 
.... 

我想刪除所有來自這個元組的副本

編輯:

使用數組沒有工作,我有一個模型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

的可能的複製[刪除在陣列中重複的對象(http://stackoverflow.com/questions/34709066/remove-duplicate-objects-in-an-array) – dfri

+0

這是一個元組 – lordxx

+0

我只是檢查:/悲傷元組不是一個對象,不適用於AnyObject,所以你不能將它轉換爲Set或NSSet,我認爲元組有相當有限的功能,所以我建議你改變使用Struct或Class,那麼它會更好,將它重構爲Struct,如下所示答案 – Tj3n

回答

1

如果使用類似於結構的模型值而不是元組

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 
} 

您可以利用Set功能刪除重複

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

請注意,在這種情況下,你失去了原來的排序順序。

+0

是的沒有工作 – lordxx

+0

@lordxx:什麼問題? –

+0

好吧,所以當我嘗試使用數組它沒有工作或者我更新了問題 – lordxx