I am building an app in Xcode 6 Beta 6 in Swift and I keep getting this error:
[NSObject : AnyObject]?' does not have a member named 'subscript'
I have no idea how to fix this. I have tried looking at this [NSObject : AnyObject]?' does not have a member named 'subscript' error in Xcode 6 beta 6 but I still don't understand how that solves the problem. If someone could explain that to me, that would be very nice. If you want to see my code, here it is:
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.
}
Here is where the error is occuring:
Please tell me why this is occurring. Thanks!
The error message is saying you can't do []
on Optional
value. What you need to do is unwrap it.
error.userInfo!["error"] as NSString
or if you want to be safe
if let errorString = error.userInfo?["error"] as NSString {
println(errorString)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With