In java we have:
private Set<AutoComplete> hashList = new LinkedHashSet<>();
and:
public class AutoComplete {
private String name;
private String id;
//...geters and setters
@Override
public boolean equals(Object o) {
if (o instanceof AutoComplete) {
AutoComplete autoComplete = (AutoComplete) o;
if (name.equals(autoComplete.name) && id.equals(autoComplete.id))
return true;
}
return false;
}
@Override
public int hashCode() {
int hash = 17;
int hashMultiplikator = 79;
try {
hash = hashMultiplikator * hash
+ getId().hashCode();
} catch (java.lang.Exception e) {
e.printStackTrace();
MLogger.logException("autocomplete id can't be null", e);
}
return hash;
}
Then when you add items to hashList , duplicate values wont be allowed and list is sort.
So my questions is how to do this in Swift?!
I already saw Does there exist within Swift's API an easy way to remove duplicate elements from an array?
and when I pass array of AutoComplete to func uniq ,
var namesAndIds : [AutoComplete] = []
(for remove duplicates:)
namesAndIds.appendContentsOf(SingletonMappingContacts.sharedInstance.autoComplete)
namesAndIds = uniq(namesAndIds)
func uniq<S : SequenceType, T : Hashable where S.Generator.Element == T>(source: S) -> [T] {
var buffer = [T]()
var added = Set<T>()
for elem in source {
if !added.contains(elem) {
buffer.append(elem)
added.insert(elem)
}
}
return buffer
}
I've got this : value of type AutoComplete has no member elemet
AutoComplete:
class AutoComplete{
var id : String
var name : String
init(id: String,name: String) {
self.name = name
self.id = id
}
}