2014-09-01 64 views
0

所有我想要做的就是在字典中,以反键/值,我也得到了以下错误:Swift - 调用中额外的参数'count'?

额外的参数“计数”在CAL

var dictionary = SWIFT_DICTIONARY 

var inverseDictionary = NSDictionary.dictionaryWithObjects(dictionary?.keys, forKeys: dictionary?.values, count: dictionary?.count) 

回答

4

使用这个代替:

var inverseDictionary = NSDictionary(objects: dictionary.keys.array, forKeys: dictionary.values.array) 

我注意到你在你的代码中打开dictionary,但它被声明为非可选的。代码错误或只是复制&粘贴错误?

附录 - 如果你尝试静态版本没有count参数:

var inverseDictionary = NSDictionary.dictionaryWithObjects(dictionary.keys.array, forKeys: dictionary.values.array) 

编译器与此消息抱怨:

'dictionaryWithObjects(_:forKeys:) is unavailable: use object construction 'NSDictionary(objects:forKeys:)' 

我认为同样的情况在其他的方法,你想要使用,但编译器不报告正确的错误消息。

+0

是的错误,代码是正确的。你的答案奏效了,但我仍然很好奇我的为什么没有。 dictionaryWithObjects:forKeys:count: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/occ/clm/NSDictionary/dictionaryWithObjects :forKeys: – aryaxt 2014-09-01 20:16:57

+0

查看编辑答案 – Antonio 2014-09-01 20:45:20

+0

感谢您的回答,我没有得到那个错误。我收到了一个不同的错误。可能是一个Xcode测试版的bug。我得到如此多的编译错误,与实际的错误没有任何关系 – aryaxt 2014-09-01 20:58:52