swift& parse.com: converting String to AnyObject?

时间:2022-08-13 15:50:19

to update the value in the table in parse I wrote this line of code:

更新解析表中的值我写了这行代码:

                  object["score"] = self.selectedScore

but it gave me the error:

但它给了我错误:

cannot assign value of type 'Int' to a value of type 'AnyObject?!'

So I thought of casting self.selectedScore to make it of type AnyObject?!

所以我考虑将self.selectedScore转换为AnyObject类型?!

this is how I did it:

这就是我做到的:

if let var castScore = self.selectedScore as! AnyObject?
{

                      object["score"] = castScore
}

but another error still appears which says that castScore is of type AnyObject? while object["score"] is of type AnyObject?!

但仍出现另一个错误,表明castScore属于AnyObject类型?而object [“score”]的类型是AnyObject?!

is there a way where I can castScore to be of type AnyObject?! ?

有没有一种方法可以将castScore转换为AnyObject类型?! ?

Table Track:

表跟踪:

table

CODE:

码:

 //test
            var taskQuery = PFQuery(className: "Track")
            //run query
            taskQuery.findObjectsInBackgroundWithBlock({
                (success:[AnyObject]?, error: NSError?) -> Void in
                if let objects = success {
                    for object in objects  {




                        var name  = sucess["taskName"]
                        var score  = sucess["score"]

                        var unwrappedName = ""
                        var unwrappedScore = ""

                        if let name = name {
                            unwrappedName = "\(name!)"
                        }

                        if let score = score {
                            unwrappedScore = "\(score!)"
                        }


                        if unwrappedName == Track {


                                object["score"] = self.selectedScore //#ERROR

                            sucess.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
                                println("Object has been saved.")
                            }



                        }
                        //trackMgr.addTrack(unwrappedName, score: unwrappedScore.toInt()!, view: false)
                    }



                }


            })

            //test

2 个解决方案

#1


1  

I think u shoule unwrap success!["score"] when u use it.

当你使用它时,我想你应该解开成功![“得分”]。

 if let _ = sucess["score"] {

    var score = success!["score"]
}

#2


-1  

Remove the question mark at the end of the line.

删除行尾的问号。

if let var castScore = self.selectedScore as! AnyObject
{
   object["score"] = castScore
}

#1


1  

I think u shoule unwrap success!["score"] when u use it.

当你使用它时,我想你应该解开成功![“得分”]。

 if let _ = sucess["score"] {

    var score = success!["score"]
}

#2


-1  

Remove the question mark at the end of the line.

删除行尾的问号。

if let var castScore = self.selectedScore as! AnyObject
{
   object["score"] = castScore
}