5

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
    }
}
Community
  • 1
  • 1
Hoven
  • 563
  • 1
  • 5
  • 24

2 Answers2

0

For One whom like to know, I ended up avoiding duplicate insertion issue like below. But THIS WONT BE SORTED LIKE LinkedHashSet So problem still exist!

i've created

var namesAndIds = Set<AutoComplete>()
var finalAutoCompleteList = [AutoComplete]()

then corrected AutoComplete class:

public class AutoComplete:NSObject{
    var id : String
    var name : String
    init(id: String,name: String) {
        self.name = name
        self.id = id
    }
    override public func isEqual(object: AnyObject?) -> Bool {
        if let object = object as? AutoComplete {
            return id == object.id
        } else {
            return false
        }
    }

    override public var hash: Int {
        return id.hashValue
    }

}

and finally added Set to Array

finalAutoCompleteList = Array(namesAndIds)
Hoven
  • 563
  • 1
  • 5
  • 24
-1

In Swift you can use Set.

As in the documentation:

A set stores distinct values of the same type in a collection with no defined ordering. You can use a set instead of an array when the order of items is not important, or when you need to ensure that an item only appears once.

A type must be hashable in order to be stored in a set—that is, the type must provide a way to compute a hash value for itself.

Simply conform to the Hashable protocol from Swift’s standard library.

Then you can use it like:

var letters = Set<Character>()  //letters == []
letters.insert("a")             //letters == ["a"]
letters.insert("a")             //letters == ["a"] - no duplicates
Matan Lachmish
  • 1,235
  • 12
  • 17