我在向Parse发送字符串时遇到问题。这是代码停止的行:
这是我的代码:
var friends: Array<AnyObject>?
var recipients: NSMutableArray?
var message: String!
var sendMessage:String!
override func viewDidLoad() {
super.viewDidLoad()
sendMessage = message
friends = []
recipients = []
let editButton = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.Plain, target: self, action: "edit")
navigationItem.rightBarButtonItem = editButton
tableView = UITableView(frame: view.bounds, style: UITableViewStyle.Plain)
tableView?.delegate = self
tableView?.dataSource = self
view.addSubview(tableView!)
let sendButton = [UIBarButtonItem(title: "Send", style: .Done, target: self, action: "sendPressed")]
toolbar.frame = CGRectMake(0, self.view.frame.size.height - 46, self.view.frame.size.width, 48)
toolbar.sizeToFit()
toolbar.setItems(sendButton, animated: true)
self.view.addSubview(toolbar)
}
func sendPressed () {
sendMessagetoParse()
}
func sendMessagetoParse(){
let messageString = message
let data = messageString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)! as NSData
// NSConcreteMutableData
let file = PFFile(name:message, data:data)
file.saveInBackground()
let sentMessage = PFObject(className:"Messages")
sentMessage["messages"] = file
sentMessage["username"] = PFUser.currentUser()?.username
sentMessage.setObject(self.recipients!, forKey: "recipientIds")
sentMessage.setObject(PFUser.currentUser()!.objectId!, forKey: "senderId")
sentMessage.setObject(PFUser.currentUser()!.username!, forKey: "senderName")
sentMessage.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
if (success) {
println("Message sent!")
我想将字符串对象发送到我的解析帐户。我还使用了Parse提供的方法,即dataUsingEncoding(NSUTF8StringEncoding),但也返回了相同的错误。关于正确格式的任何线索或我需要如何修复代码?
编辑:
好的,我不再收到错误,但我唯一的问题是,一旦按下“发送”,它就不会做任何事情。这是我编辑过的代码:
func sendMessagetoParse(){ 让messageString = message
if messageString != nil
{
let data = messageString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)! as NSData
// NSConcreteMutableData
let file = PFFile(name:message, data:data)
file.saveInBackground()
let sentMessage = PFObject(className:"Messages")
sentMessage["messages"] = file
sentMessage["username"] = PFUser.currentUser()?.username
sentMessage.setObject(self.recipients!, forKey: "recipientIds")
sentMessage.setObject(PFUser.currentUser()!.objectId!, forKey: "senderId")
sentMessage.setObject(PFUser.currentUser()!.username!, forKey: "senderName")
sentMessage.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
if (success) {
println("Message sent!")
var alert = UIAlertController(title: "Awesome!", message: "Message was Sent", preferredStyle: UIAlertControllerStyle.Alert);
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: nil));
//show it
self.navigationController?.popToRootViewControllerAnimated(true)
} else {
println("error")
}
}
}
}
来自上一个视图控制器的代码,名为ConfirmViewController,我尝试使用segue将文本标签传递到当前视图控制器的messageString中,这称为FriendsViewController:
override func prepareForSegue(segue: (UIStoryboardSegue!), sender:
AnyObject!) {
if (segue.identifier == "addFriends") {
var mvc = segue!.destinationViewController as!
FriendsViewController;
mvc.sendMessage = messageView.text
}
}
在ConfirmViewController
我设置了segue,然后将sendMessage设置为viewDidLoad
中的消息,如我的第一次编辑所示。
答案 0 :(得分:0)
检查
let messageString
值。您获得的运行时错误可能是隐式解包可选值为零的结果。您可能希望在if语句中执行代码,如:
if let messageString != nil
{
////put your saving code here
}
答案 1 :(得分:0)
试试这个:
override func viewDidLoad() {
super.viewDidLoad()
// sendMessage = message
message = sendMessage