所以我有这个密码。

@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

我没有错误,但我的分析列是一个数字列,因此努力使它成为一个数字。
编辑
2016-01-31 03:58:11.222 ilegalto[3662:186816]试图加载
不允许在视图控制器取消分配时查看它,并且
可能导致未定义的行为()
可选(Swift.String.CharacterView(_core:
Swift._StringCore(_baseAddress:0x00007fb2c3667472,_countAndFlags:4,
_业主:可选(Swift.\u HeapBufferStorage)))2016-01-31 03:58:30.728回肠二段[3662:186900]
[错误]:createdby键的类型无效,应为*用户,但得到
字符串(代码:111,版本:1.12.0)图像可选(错误域=解析
Code=111“键createdby的类型无效,应为*用户,但得到
字符串“UserInfo={code=111,temporary=0,error=key的类型无效
createdby,应为*用户,但得到字符串,
NSLocalizedDescription=键createdby的类型无效,应为
*_用户,但得到字符串})
对上述错误进行修改后编辑:
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)
    }

最佳答案

无法从后台线程触摸用户界面
从主线程中推动视图控制器。罪魁祸首:

saveInBackgroundWithBlock...
    self.presentViewController

解决
- use `performSelectorOnMainThread`
- use dispatch_async(dispatch_get_main_queue(), ^(void) { } })

例子
将警报创建替换为
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)
}

关于ios - 不能将Nil用于PFObject的键或值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35104538/

10-10 21:11