【发布时间】:2016-01-30 17:08:00
【问题描述】:
所以我有这个代码。
@IBAction func updateRatelabel(sender: AnyObject) {
let rateValue = Int(rateSlider.value)
if rateValue == 0 {
rateLabel.text = "Budget (min.)"
}
else {
rateLabel.text = "$ \(rateValue)"
}
}
func createdoctor()
{
let doctors = PFObject(className: "doctors")
if doctorName.text?.characters.count > 5 {
doctorName.textColor = UIColor.redColor()
}
else {
let currentUser = String( PFUser.currentUser())
let rateValue = rateLabel.text
let rateValueInt = String(rateValue?.characters.dropFirst(2))
print(rateValueInt)
doctors["doctorName"] = doctorName.text
doctors["Rate"] = Int(rateValueInt)
doctors["doctorContent"] = doctorDetails.text
doctors["createdby"] = currentUser
doctors.saveInBackgroundWithBlock ({(success: Bool, error: NSError?) -> Void in
if error == nil {
print("created doctor")
}
else {
print("image \(error)")
}
let alert = UIAlertController(title: "Success", message: "New doctor Created" , preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
})
}
}
此代码启动时出现错误,这是正在发生的错误
> 016-01-31 01:00:07.921 idoctortwo[3294:151254] Attempting to load the
> view of a view controller while it is deallocating is not allowed and
> may result in undefined behavior (<UIAlertController: 0x7f8258c8af80>)
> Optional(Swift.String.CharacterView(_core:
> Swift._StringCore(_baseAddress: 0x00007f8258c9b9c2, _countAndFlags: 4,
> _owner: Optional(Swift._HeapBufferStorage<Swift._StringBufferIVars, Swift.UInt16>)))) 2016-01-31 01:00:12.771 idoctortwo[3294:151254] ***
> Terminating app due to uncaught exception
> 'NSInvalidArgumentException', reason: 'Can't use nil for keys or
> values on PFObject. Use NSNull for values.'
我设法发现下面这行代码是罪魁祸首
doctors["Rate"] = Int(rateValueInt)
如果我这样做
doctors["Rate"] = rateValueInt
我没有错误,但我的 Parse 列是一个数字列,因此努力将其设为数字。
编辑
2016-01-31 03:58:11.222 iLegaltwo[3662:186816] 尝试加载 视图控制器在释放时的视图是不允许的,并且 可能导致未定义的行为 () 可选(Swift.String.CharacterView(_core: Swift._StringCore(_baseAddress: 0x00007fb2c3667472, _countAndFlags: 4, _owner:可选(Swift._HeapBufferStorage))))2016-01-31 03:58:30.728 iLegaltwo [3662:186900] [错误]:创建的密钥类型无效,应为 *_User,但得到了 字符串(代码:111,版本:1.12.0)图像可选(错误域=解析 Code=111 "创建的密钥类型无效,预期为 *_User,但得到了 string" UserInfo={code=111,temporary=0,error=key 类型无效 createdby,预期 *_User,但得到字符串, NSLocalizedDescription=key createdby 的无效类型,预期 *_User,但得到了字符串})
针对上述错误修改后编辑:
func createCase()
{
let cases = PFObject(className: "Cases")
if caseName.text?.characters.count > 5
{
caseName.textColor = UIColor.redColor()
}
else
{
let currentUser = String( PFUser.currentUser())
let rateValue = rateLabel.text
let rateValueInt = String(rateValue?.characters.dropFirst(2))
print(rateValueInt)
cases["CaseName"] = caseName.text
cases["Rate"] = Int(rateValueInt) ?? Int(0)
cases["CaseContent"] = caseDetails.text
cases["createdby"] = currentUser
cases.saveInBackgroundWithBlock ({(success: Bool, error: NSError?) -> Void in
if error == nil
{
print("created case")
}
else
{
print("image \(error)")
}
self.performSelectorOnMainThread("success", withObject: nil, waitUntilDone: true)
})
}
}
func success() {
let alert = UIAlertController(title: "Success", message: "New doctor Created" , preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
【问题讨论】:
-
你现在在一个帖子中有 2 个不同的问题,这很遗憾:你当然可以两个都 +1,但你不能同时批准两个......
-
我相信你的第二个答案和我的第二个问题是这个问题的重点,正如我提到的,我设法弄清楚如何让它 INT 面对这个丑陋的输出。
标签: ios swift parse-platform int