【问题标题】:anyObject converted into a stringanyObject 转换为字符串
【发布时间】:2014-11-15 17:02:38
【问题描述】:

我正在尝试将具有关键价格的对象转换为 NSString,但无论我做什么,我都会在下一行收到 lldb 错误

arrayDic.setValue(object.objectForKey("price") as NSString, forKey: "price")

当我删除它时,没有错误。 object.objectForKey("price") 是解析数据库中的数字类型。如何正确将其转换为字符串?

代码:

func loadItems() {
    var query = PFQuery(className:"Items")
    query.orderByAscending("createdAt")
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil {


            for object in objects {

                arrayDic.setValue(object.objectForKey("price") as NSString, forKey: "price")
                arrayDic.setValue(image, forKey: "image")



            }


        } else {
            // Log details of the failure
            NSLog("Error: %@ %@", error, error.userInfo!)
        }
    }
}

【问题讨论】:

  • 将语句分解成多条语句,找出错误所在。

标签: ios swift parse-platform


【解决方案1】:

object.objectForKey("price")是解析数据库中的数字类型

那你为什么说它是一个字符串?请记住,说as 不会转换任何东西;这是对真理的主张。如果你撒谎,你当然会崩溃。不要撒谎!

我假设它是一个 NSNumber。如果我是对的,请说as NSNumber

既然这个东西一个数字,你不应该想要把它作为一个字符串存储在arrayDic中,所以把它作为一个数字留着吧。 p>

(另外,如果arrayDic 是字典,则不应使用valueForKey: - 可以,但它既愚蠢又迂回,并且可能产生意想不到的后果。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多