ios - `CountedSet` initialization issue -
i'm comparing characters contained within 2 words. in seeking accomplish this, set
(aka nsset
) seemed way go accomplish task. i've discovered returns false positives on matches, attempting use countedset
(aka nscountedset
) instead.
i'm able initialize set
without issue, can't countedset
initializer work. here's i've done...
i start string
:
// let's mytextfield.text = "test" let textfieldcharacters = mytextfield.text?.characters // word string enable list of words let wordcharacters = word.characters
then dump characters array
:
var wordcharactersarray = [character]() character in wordcharacters { wordcharacterarray.append(character) } var textfieldcharactersarray = [character]() character in textfieldcharacters { wordcharacterarray.append(character) }
then create set
character arrays:
let textfieldset = set<character>(textfieldcharactersarray) let wordset = set<character>(wordcharactersarray)
finally, test see if textfieldset
superset of wordset
following:
textfieldset.issuperset(of: wordset)
going example, if mytextfield.text
"test", i'm returning values word
characters superset of wordset
, counts of individual elements don't match character counts of mytextfield.text
in researching issue, i've found countedset
(fka nscountedset
), think resolve issue. has 2 method signatures:
public convenience init(array: [anyobject]) public convenience init(set: set<nsobject>)
i've tried initializing 2 sets of characters so:
let textfieldset = countedset(array: textfieldcharacterarray) let wordset = countedset(array: wordcharacterarray)
i following error sets
cannot convert value of type '[character]' expected argument type '[anyobject]'.
so tried initializing set this:
let textfieldset = countedset(array: textfieldcharacterarray as! [anyobject])
which yields following error:
'anyobject' not subtype of 'character'
i've tried initialize countedset
set
, per method signature, errors when try that, too.
any suggestions how initialize countedset
appreciated.
you correct if need compare not presents of elements count, should use countedset
, renaming of nscountedset
swift 3.0. problem running countedset
can accept elements objects , characters
not. eric d points out in comment, easies way around mapping [character]
[string]
bridge [nsstring]
.
you not running problem using set
, because native swift collection type initialize elements of type. why can initialize set
[character]
.
to see difference:
let word = "helo" let wordcharacters = array(word.characters) let wordset = set(wordcharacters) let wordcharstrings = wordcharacters.map{string($0)} let wordcountedset = countedset(array: wordcharstrings) let textfield = "hello" let textfieldcharacters = array(textfield.characters) let textset = set(textfieldcharacters) let textfieldcharstrings = textfieldcharacters.map{string($0)} let textfieldcountedset = countedset(array: textfieldcharstrings) textfieldcountedset.issubset(of: wordcountedset as! set<nsobject>) // returns false, if word had 2 or more l's return true textset.issubset(of: wordset) // returns true
Comments
Post a Comment