我正在 Swift 中的 Xcode 6 Beta 6 中构建一个应用程序,但我不断收到此错误:
[NSObject : AnyObject]?' does not have a member named 'subscript'
我不知道如何解决这个问题。我试过看这个[NSObject:任何对象]?' Xcode 6 beta 6 中没有名为“下标”的成员错误 https://stackoverflow.com/questions/25381338/nsobject-anyobject-does-not-have-a-member-named-subscript-error-in-xcode但我仍然不明白这是如何解决问题的。如果有人可以向我解释这一点,那就太好了。如果你想看我的代码,这里是:
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.
}
这是发生错误的地方:
请告诉我为什么会发生这种情况。谢谢!
错误消息表明您无法执行此操作[]
on Optional
价值。您需要做的就是打开它。
error.userInfo!["error"] as NSString
或者如果你想安全
if let errorString = error.userInfo?["error"] as NSString {
println(errorString)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)