objective-c - 在 Swift 中初始化 NSDictionary 时出现问题

标签 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 的输出报告:

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/

相关文章:

swift - 所选图像不会更改之前显示的图像

swift - WKWebview:如何防止 Laravel Page 链接在 safari 中打开?

ios - 在 iOS 应用程序上安装谷歌分析时出错

swift - 关闭时缺少返回预计返回 'Void?'

swift - 在 Xcode 中哪里可以找到带有 Swift 语法的框架头文件

swift - iOS/swift : QLPreviewController shows blank page in iOS11

objective-c - 在 iPhone 应用程序中使用 UiWebView(用于支付)会被拒绝吗?应用程序是一种网络服务

objective-c - iOS 7 - 为什么 CGAffineTransform 在 iOS8 而不是 iOS7 上正常工作?

ios - 使用 Magical Record 在 NSDictionary 中导入数组

objective-c - 在 iOS 框架内播放视频(并将其居中)