我正在创建一个使用Backendless来保存数据的应用。当他们对here说话时,我已经把所有东西都搞定了。
但是,当调用persistenceService
方法时,应用程序崩溃:
backendless.persistenceService.of(UserSchema.ofClass()).save(user)
以下是文件的代码:
import UIKit
class SignUpViewController: UIViewController {
@IBOutlet weak var userName: UITextField!
@IBOutlet weak var email: UITextField!
@IBOutlet weak var password: UITextField!
@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var lastName: UITextField!
@IBOutlet weak var dateOfBirth: UITextField!
@IBOutlet weak var zip: UITextField!
let backendless = Backendless.sharedInstance()
// Create a new instance of UserSchema for the use of createing a new user.
let user = UserSchema()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func signUp() {
// Check to make sure all fields have proper values and are not nill.
if userName.text != nil && email.text != nil && password.text != nil && firstName.text != nil
&& lastName.text != nil && dateOfBirth != nil && zip.text != nil && Int(zip.text!) != nil {
// Asign text from fields to the variables for the new user.
user.userName = userName.text
user.email = email.text
user.password = password.text
user.firstName = firstName.text
user.lastName = lastName.text
user.dateOfBirth = dateOfBirth.text
user.zip = zip.text
backendless.persistenceService.of(UserSchema.ofClass()).save(user)
shouldPerformSegueWithIdentifier("succesfulSignUp", sender: nil)
} else {
// Alert the user if any of the values are nil so they can fix the issue.
let emptyTextFieldAlert = UIAlertController(title: "Empty Field", message: "Please make sure all fields have proper values", preferredStyle: .Alert)
emptyTextFieldAlert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action -> Void in
// Do nothing
}))
self.presentViewController(emptyTextFieldAlert, animated: true, completion: nil)
}
}
}
以下是UserSchema
类:
import Foundation
// Class for data that gets sent to the Backendless database
class UserSchema: NSObject {
var zip: String?
var userName: String?
var password: String?
var firstName: String?
var lastName: String?
var email: String?
var dateOfBirth: String?
}
导致坠机的原因是什么?
以下是架构的屏幕截图:
这是一个repo,因此您可以克隆并测试代码。我做了一些与问题中的代码不同的更改:
https://github.com/calebkleveter/chatter
重现崩溃;运行应用程序,选择登录页面上的注册按钮,用适当的信息填写所有字段,然后点击"注册!"按钮。该应用程序将崩溃。
更新
我设法通过在此行放置制动点来获得日志输出
backendless.persistenceService.of(UserSchema.ofClass()).save(user)
然后踩到它,然后我穿过应用程序直到它崩溃了,我得到了这个:
2016-04-11 12:04:09.025 Chatter[21686:11538370] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
(
0 CoreFoundation 0x0000000106c64e65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001090f5deb objc_exception_throw + 48
2 CoreFoundation 0x0000000106b23652 -[__NSPlaceholderArray initWithObjects:count:] + 290
3 CoreFoundation 0x0000000106b807e4 +[NSArray arrayWithObjects:count:] + 52
4 Chatter 0x00000001053ffc53 -[PersistenceService save:] + 409
5 Chatter 0x00000001052e2eb5 _TFC7Chatter20SignUpViewController6signUpfS0_FT_T_ + 8709
6 Chatter 0x00000001052e3452 _TToFC7Chatter20SignUpViewController6signUpfS0_FT_T_ + 34
7 UIKit 0x0000000107bd3194 -[UIApplication sendAction:to:from:forEvent:] + 92
8 UIKit 0x0000000107d426fc -[UIControl sendAction:to:forEvent:] + 67
9 UIKit 0x0000000107d429c8 -[UIControl _sendActionsForEvents:withEvent:] + 311
10 UIKit 0x0000000107d41af8 -[UIControl touchesEnded:withEvent:] + 601
11 UIKit 0x0000000107c4249b -[UIWindow _sendTouchesForEvent:] + 835
12 UIKit 0x0000000107c431d0 -[UIWindow sendEvent:] + 865
13 UIKit 0x0000000107bf1b66 -[UIApplication sendEvent:] + 263
14 UIKit 0x0000000107bcbd97 _UIApplicationHandleEventQueue + 6844
15 CoreFoundation 0x0000000106b90a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
16 CoreFoundation 0x0000000106b8695c __CFRunLoopDoSources0 + 556
17 CoreFoundation 0x0000000106b85e13 __CFRunLoopRun + 867
18 CoreFoundation 0x0000000106b85828 CFRunLoopRunSpecific + 488
19 GraphicsServices 0x000000010b743ad2 GSEventRunModal + 161
20 UIKit 0x0000000107bd1610 UIApplicationMain + 171
21 Chatter 0x00000001052ea13d main + 109
22 libdyld.dylib 0x0000000109c4692d start + 1
23 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
答案 0 :(得分:0)
原来我不小心给了APP_ID
和SECRET_KEY
变量相同的值。修好后一切正常!