[NSObject:AnyObject]?'在Xcode 6 Beta 6中没有名为'subscript'的成员 [英] [NSObject : AnyObject]?' does not have a member named 'subscript' in Xcode 6 Beta 6
问题描述
我正在使用Swift中的Xcode 6 Beta 6构建应用程序并且我一直收到此错误:
[NSObject:AnyObject ]?'没有名为'下标'的成员
我不知道如何解决这个问题。我试过看这个 [NSObject :AnyObject]?'在Xcode 6 beta 6中没有名为'subscript'的成员但我仍然不明白这是如何解决问题的。如果有人可以向我解释,那将是非常好的。如果你想查看我的代码,这里是:
import UIKit
class TimelineTableViewController:UITableViewController {
覆盖func viewDidAppear(动画:Bool){
if((PFUser.currentUser())!= nil){
//如果没有用户
var loginAlert,则创建一个UIAlertController:UIAlertController = UIAlertController(标题:注册/登录,消息:请注册或登录,preferredStyle:UIAlertControllerStyle.Alert)
//在登录警报中为用户名添加textView
loginAlert.addTextFieldWithConfigurationHandler({
textfield in
textfield.placeholder =Your Username
})
//在登录警报中添加一个textView以获取密码
loginAlert.addTextFieldWithConfigurationHandler({
textfield in
textfield.placeholder =Your Password
textfield.se cureTextEntry = true
})
//将用户输入放入数组并相应地设置用户名和密码登录
loginAlert.addAction(UIAlertAction(标题:登录,样式:UIAlertActionStyle.Default,处理程序:{
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!) - >在
中无效if((user)!= nil){
println(Login successfull)
} else {
println(登录失败 )
}
}
}))
//将用户输入放入数组并相应地设置用户名和密码以注册
loginAlert.addAction(UIAlertAction(标题:注册,样式:UIAlertActionStyle.Default,处理程序:{
中的alertAction让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 {
(成功:Bool!,错误:NSError!) - >如果!(错误!=无),
无效{
println(注册成功)
} else {
let errorString = error.userInfo [erro r] as NSString
println(errorString)
}
}
}))
}
}
覆盖func viewDidLoad(){
super.viewDidLoad()
//取消注释以下行以保留演示文稿之间的选择
// self.clearsSelectionOnViewWillAppear = false
//取消注释以下行以在此视图控制器的导航栏中显示编辑按钮。
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
覆盖func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
/ /处置可以重新创建的任何资源。
}
以下是发生错误的地方:
<请告诉我为什么会这样。谢谢!
错误消息说你做不到 []
on 可选
值。您需要做的就是打开它。
error.userInfo![error] as NSString
或者如果你想要安全
如果让errorString = error.userInfo?[error]为NSString {
println(errorString)
}
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)
}
这篇关于[NSObject:AnyObject]?'在Xcode 6 Beta 6中没有名为'subscript'的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!