【问题标题】:[NSObject : AnyObject]?' does not have a member named 'subscript' in Xcode 6 Beta 6[NSObject:AnyObject]?在 Xcode 6 Beta 6 中没有名为“下标”的成员
【发布时间】:2014-10-19 03:29:37
【问题描述】:

我正在使用 Swift 的 Xcode 6 Beta 6 构建一个应用程序,但我不断收到此错误:

[NSObject : AnyObject]?' does not have a member named 'subscript'

我不知道如何解决这个问题。我试过查看这个[NSObject : AnyObject]?' does not have a member named 'subscript' error in Xcode 6 beta 6,但我仍然不明白这是如何解决问题的。如果有人可以向我解释,那就太好了。如果你想看我的代码,这里是:

import UIKit

class TimelineTableViewController: UITableViewController {

 override func viewDidAppear(animated: Bool) {

    if ((PFUser.currentUser()) != nil) {

        //Create an UIAlertController if there isn't an user
        var loginAlert:UIAlertController = UIAlertController(title: "Sign Up/ Log In", message: "Please sign up or log in", preferredStyle: UIAlertControllerStyle.Alert)

        //Add a textView in the Log In Alert for the username
        loginAlert.addTextFieldWithConfigurationHandler({
            textfield in
            textfield.placeholder = "Your Username"
        })

        //Add a textView in the Log In Alert for the password
        loginAlert.addTextFieldWithConfigurationHandler({
            textfield in
            textfield.placeholder = "Your Password"
            textfield.secureTextEntry = true
        })

        //Place the user-input into an array and set the username and password accordingly for Log In
        loginAlert.addAction(UIAlertAction(title: "Login", style: UIAlertActionStyle.Default, handler: {
            alertAction in
            let textFields:NSArray = loginAlert.textFields as NSArray
            let usernameTextfield:UITextField = textFields.objectAtIndex(0) as UITextField
            let passwordTextfield:UITextField = textFields.objectAtIndex(1) as UITextField

            PFUser.logInWithUsernameInBackground(usernameTextfield.text, password: passwordTextfield.text){
                (user:PFUser!, error:NSError!)->Void in
                if ((user) != nil){
                    println("Login successfull")
                }else{
                    println("Login failed")
                }


            }

        }))

        //Place the user-input into an array and set the username and password accordingly for Sign Up
        loginAlert.addAction(UIAlertAction(title: "Sign Up", style: UIAlertActionStyle.Default, handler: {
            alertAction in
            let textFields:NSArray = loginAlert.textFields as NSArray
            let usernameTextfield:UITextField = textFields.objectAtIndex(0) as UITextField
            let passwordTextfield:UITextField = textFields.objectAtIndex(1) as UITextField

            var sweeter:PFUser = PFUser()
            sweeter.username = usernameTextfield.text
            sweeter.password = passwordTextfield.text

            sweeter.signUpInBackgroundWithBlock{
                (success:Bool!, error:NSError!)->Void in
                if !(error != nil){
                    println("Sign Up successfull")
                }else{
                    let errorString = error.userInfo["error"] as NSString
                    println(errorString)
                }


            }

        }))
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

这是发生错误的地方:

请告诉我为什么会发生这种情况。谢谢!

【问题讨论】:

  • 哪一行?你可以做foo![bar]来解决这个问题
  • 对不起,完全忘记添加错误所在。现在添加它,'foo![bar]'是什么意思@BryanChen
  • error.userInfo!["error"] as NSString
  • 天啊,非常感谢。像魅力一样工作。您可能希望将其添加为答案,以便其他人也可以看到它。非常感谢!

标签: ios swift


【解决方案1】:

错误消息是说您不能对 Optional 值执行 []。你需要做的就是打开它。

error.userInfo!["error"] as NSString

或者如果你想安全

if let errorString = error.userInfo?["error"] as NSString {
     println(errorString)
}

【讨论】:

  • 非常感谢!我已经挠头 2 天了!
  • 谢谢!!!但是在文档和教程视频中是这样写的,他们没有得到任何错误。如何?为什么他们没有收到那个错误?
猜你喜欢
  • 2014-10-12
  • 1970-01-01
  • 2015-03-19
  • 2017-01-16
  • 1970-01-01
  • 2020-01-01
  • 2014-10-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多