[NSObject:AnyObject]?'在Xcode 6 Beta 6中没有名为'subscript'的成员

时间:2014-08-25 22:06:36

标签: ios swift

我正在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.
}

以下是发生错误的地方:

enter image description here

请告诉我为什么会这样。谢谢!

1 个答案:

答案 0 :(得分:35)

错误消息表示您无法对[]Optional执行操作。你需要做的就是打开它。

error.userInfo!["error"] as NSString

或者如果你想要安全

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