在Swift中初始化NSDictionary的麻烦

时间:2014-09-21 08:55:45

标签: objective-c xcode swift count nsdictionary

我一直在努力尝试从 Objective-C

翻译此代码
NSDictionary * options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid] forKey:UIPageViewControllerOptionSpineLocationKey];

... Swift

var options:NSDictionary = NSDictionary.dictionaryWithObjects(UIPageViewControllerSpineLocation.Mid, forKeys: UIPageViewControllerOptionSpineLocationKey, count: 0);

不幸的是它不起作用,我不知道为什么! 以下是 XCode 的输出报告:

  

无法使用类型'的参数列表调用'dictionaryWithObjects'(UIPageViewControllerSpineLocation,对于keys:NSString!,count:IntegerLiteralConvertible)'

有人能帮助我吗? 感谢

1 个答案:

答案 0 :(得分:4)

怎么样只是

    let options = [UIPageViewControllerOptionSpineLocationKey: UIPageViewControllerSpineLocation.Mid];`

你面临的问题是构造函数NSDictionary.dictionaryWithObjects - a)不存在,b)枚举不是AnyObject,c)count是0但应该是1

    let o = UIPageViewControllerSpineLocation.Mid.toRaw() //toRaw makes it conform to AnyObject
    let k = UIPageViewControllerOptionSpineLocationKey
    let options = NSDictionary(object: o, forKey: k)