There seems to be a bug in Swift Playground with the use of NSCountedSet.
This code works as intended
let numbers = [1,2,2,4,6,7,8,8,5,8,1]
let set = NSSet(array: numbers)
but when I try to create an NSCountedSet in the same fashion
var bag = NSCountedSet(array: numbers)
I get this error
Playground execution failed: /var/folders/bl/1tnlvfzd4mqb9gkpx0h8rxy00000gp/T/lldb/6514/playground599.swift:56:31: error: 'Int' is not identical to 'AnyObject' var bag = NSCountedSet(array: numbers)
I did try casting numbers
let nums = numbers as [AnyObject]
var bag = NSCountedSet(array: nums)
then I get this error
Playground execution failed: /var/folders/bl/1tnlvfzd4mqb9gkpx0h8rxy00000gp/T/lldb/6514/playground732.swift:58:23: error: extra argument 'array' in call var bag = NSCountedSet(array: nums)
Am I missing something here?
I can work around the problem by doing this
var bag = NSCountedSet()
for number in numbers {
bag.addObject(number)
}
But it is not very elegant
Update: As @carbo18 reported, this has been fixed in Xcode 6.3 beta 4.
Old answer: That definitely looks like a bug. NSCountedSet
has initializers
convenience init(array: [AnyObject])
convenience init(set: NSSet)
but
let b1 = NSCountedSet(array: []) // extra argument 'array' in call
let b2 = NSCountedSet(set: NSSet()) // extra argument 'set' in call
both fail to compile.
This was also reported in the Apple Developer Forum (https://devforums.apple.com/message/1081850#1081850), where the following workaround is given:
let numbers = [1,2,2,4,6,7,8,8,5,8,1]
let bag = NSCountedSet()
bag.addObjectsFromArray(numbers)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With