本文介绍了Swift NSCountedSet init有数组bug吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift Playground中似乎存在使用NSCountedSet的错误。

There seems to be a bug in Swift Playground with the use of NSCountedSet.

此代码按预期工作

let numbers = [1,2,2,4,6,7,8,8,5,8,1]

let set = NSSet(array: numbers)

但是当我尝试以相同的方式创建NSCountedSet时

but when I try to create an NSCountedSet in the same fashion

var bag = NSCountedSet(array: numbers)

我收到此错误

我确实尝试过投射数字

let nums = numbers as [AnyObject]
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)
}

但它不是很优雅

推荐答案

更新:正如@ carbo18报道的那样,这已在Xcode 6.3 beta 4中得到修复。

Update: As @carbo18 reported, this has been fixed in Xcode 6.3 beta 4.

旧答案:这绝对看起来像个错误。 NSCountedSet 有初始值设定项

Old answer: That definitely looks like a bug. NSCountedSet has initializers

convenience init(array: [AnyObject])
convenience init(set: NSSet)

但是

let b1 = NSCountedSet(array: [])     // extra argument 'array' in call
let b2 = NSCountedSet(set: NSSet())  // extra argument 'set' in call

的额外参数'set'都无法编译。

both fail to compile.

Apple开发者论坛(),
,其中给出了以下解决方法:

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)

这篇关于Swift NSCountedSet init有数组bug吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 20:08