How can i append my HKObjectTypes into a NSSet.
It always returns empty. Any better way???
func getPermsFromOptions(_ options: NSArray) -> NSSet {
var readPermSet = NSSet()
var optionKey: String
var val: HKObjectType
for i in options{
optionKey = i as! String
val = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier(rawValue: optionKey))!
readPermSet.adding(val)
print("set", readPermSet) //always empty
}
return readPermSet;
}
2
Answers
Adding isn’t a mutating method, it returns a new set that has the other value added
Try it with:
and
You cannot add to an
NSSet
. You can add to anNSMutableSet
:adding
is a method from Swift, that returns a new set with all the same elements, plus the new element. You are ignoring its return value here.Since you are in Swift, why not use
Set<HKObjectType>
and[String]
?