我一直在努力尝试从 Objective-C 翻译这段代码
NSDictionary * options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid] forKey:UIPageViewControllerOptionSpineLocationKey];
...到Swift
var options:NSDictionary = NSDictionary.dictionaryWithObjects(UIPageViewControllerSpineLocation.Mid, forKeys: UIPageViewControllerOptionSpineLocationKey, count: 0);
不幸的是它不起作用,我不知道为什么! 以下是 XCode 的输出报告:
Cannot invoke 'dictionaryWithObjects' with an argument list of type '(UIPageViewControllerSpineLocation, for keys: NSString!, count: IntegerLiteralConvertible)'
有人可以帮助我吗? 谢谢
最佳答案
就这样怎么样
let options = [UIPageViewControllerOptionSpineLocationKey: UIPageViewControllerSpineLocation.Mid];`
您面临的问题是构造函数 NSDictionary.dictionaryWithObjects -- a) 不存在,b) 枚举不是 AnyObject,c) 计数为 0 但应该为 1
let o = UIPageViewControllerSpineLocation.Mid.toRaw() //toRaw makes it conform to AnyObject
let k = UIPageViewControllerOptionSpineLocationKey
let options = NSDictionary(object: o, forKey: k)
关于objective-c - 在 Swift 中初始化 NSDictionary 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25957522/