【问题标题】:(NSObject, AnyObject) is not convertible to DictionaryIndex<NSObject, AnyObject>(NSObject, AnyObject) 不能转换为 DictionaryIndex<NSObject, AnyObject>
【发布时间】:2015-01-02 19:00:56
【问题描述】:

我正在从 Objective-C 教程转换此委托协议函数,但在尝试通过使用下标返回基于键的值来访问字典中的值时遇到错误。我不太确定这里的错误是什么意思。任何帮助将不胜感激!

// Sent to the delegate to determine whether the sign up request should be submitted to the server
func signUpViewController(signUpController: PFSignUpViewController!, shouldBeginSignUp info: [NSObject : AnyObject]!) -> Bool {
  var informationComplete: Bool = true

  // Loop through all of the submitted data
  for key in info {
    var field: String = info[key] // Error occurs at this line on info
    if field.isEmpty {
      informationComplete = false
      break
    }
  }

  // Display an alert if a field was not completed
  if !informationComplete {
    let alertView: UIAlertView = UIAlertView(title: "Missing Information", message: "Please make sure all information is filled out!", delegate: nil, cancelButtonTitle: "ok")
  }

  return informationComplete
}

【问题讨论】:

    标签: ios swift parse-platform


    【解决方案1】:

    您没有发布声明。但很可能您可以通过以下方式解决它:

    for (key, field) in info {
      if field.isEmpty {
        informationComplete = false
        break
      }
    }
    

    【讨论】:

    • 我更改了代码并且错误确实消失了,尽管我必须将 if field.isEmpty 更改为 if field.isEmpty == nil 否则会出现错误。所以理论上这只会循环遍历每个键值对,如果其中一个丢失,它会抛出正确的错误吗?这就是我一直在寻找的
    • 是的。用这种简短的语法获得键/值对是一种很好的语言结冰。
    • 好的,谢谢!我接受了你的回答,再次查看后,我记得在圣诞节收到的 Swift by Tutorials 书中看到过类似的内容。应该认出来哈哈。再次感谢!
    • 需要一些时间来掌握所有新事物。特别是如果 SourceKitService 每 2 分钟崩溃一次 :-(
    • 同意!而且我认为他们在最新版本的 Xcode 6 中解决了这个问题。可能想检查是否有更新!
    猜你喜欢
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多