- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试转换这段 ojb-c 代码:
MPMovieFinishReason finishReason = [notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
快速,因此我有这个:
var finishReason: MPMovieFinishReason = notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey.integerValue];
但是这给了我标题中的错误。我在这里做错了什么?
最佳答案
你有几个问题,首先是你的直接翻译:
var finishReason: MPMovieFinishReason = notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey.integerValue];
]
位置错误,因此您尝试在 MPMoviePlayer...
上调用 integerValue
而不是在字典上查找。
var finishReason: MPMovieFinishReason = notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey].integerValue;
这仍然失败,因为 userInfo 是一个 NSDictionary 而不是一个字典,所以它被映射到 [AnyObject:AnyObject]
CFString
不可映射到 AnyObject。
如果我们把它分解得更多,这个过程会变得更清晰一些:
// Recover the original dictionary, and cast is by input and output, to it
// all makes sense. This will allow String as a parameter and return an NSNumber
// as a result.
let userInfo = notification.userInfo as [String:NSNumber]
// From the userInfo, let's extract the raw NSNumber using lookup
let reason = userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]
// Convert the NSNumber into an MPMovieFinishReason enum. Note that it's
// 0215 and I'm on ambien so I don't care about the force unwrapping that
// should be optional unwrapping
let finishReason = MPMovieFinishReason.fromRaw(reason!.integerValue)
如果您一次只采取一个步骤,那么许多问题都可以分解为更简单的问题。现代编译器甚至不会太在意它,因为它们可以识别何时不再使用变量并进行适当的回收。另请注意,对这些临时值使用 let 而不是 var,因为它们无论如何都不应该被重用。
关于ios - Swift Int 不能转换为 DictionaryIndex?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25356923/
我在 Swift 中使用字典,它需要保存不同类型的值,尽管键几乎都是字符串。这在 Objective-C 中非常容易,但 Swift 通常非常有用的类型安全似乎使这变得更加困难。 我一直在使用 [St
我有一个循环遍历包含字符串或字典的数组并为类型为 [String:AnyObject] 的新字典设置值的函数,使用原始数组中的值作为键。 sections是一组自定义 Section对象及其方法 ge
我正在尝试转换这段 ojb-c 代码: MPMovieFinishReason finishReason = [notification.userInfo[MPMoviePlayerPlaybackD
根据 Dictionary 上的标题文档在 Swift 中: A hash-based mapping from Key to Value instances. Also a collection o
大家好,我有大麻烦了。这是我的代码: let listOfQuestionsAndAnswers = ["Who’s Paul?": "An American", "Who’s Joao?": "A
我正致力于从 Objective-C 教程转换此委托(delegate)协议(protocol)函数,但在尝试通过使用下标返回基于键的值来访问字典中的值时遇到错误。我不太确定这里的错误是什么意思。任何
我尝试将一些 objective-c 代码重写为 swift。现在我得到一个非常奇怪的错误。我在 playgroud 中尝试了代码,完全没有受到其他代码的影响。 这是我要翻译的部分: NSDictio
当我从 Xcode 6 beta 4 切换到 beta 5 时,我在引用我的 plays 变量时遇到此错误(Int 不可转换为“DictionaryIndex”)。我不知道如何解决这个问题。 这是代码
我正在尝试制作一个非常简单的字典,将字符串映射到 Swift 中的字符串数组。代码如下所示: class FirstViewController: UIViewController { var
我是一名优秀的程序员,十分优秀!