【问题标题】:AnyObject to string in swiftAnyObject 快速字符串化
【发布时间】:2014-11-17 12:26:05
【问题描述】:

我被这个代码卡住了!我想删除选项什么是最好的方法? 我试图将 AnyObject 更改为 NSString 但我不知道如何。

最好的方法是什么?

我的第二个问题是如何将其存储在核心数据中?有一个很酷的框架吗?

Alamofire.request(.GET, urlDb)
        .responseJSON { (request, response, json, error) in
            println(error)
            //println(json)

            if let contactGroups : AnyObject! = json{
                //println("Contact groepen")
                var contactGroups = contactGroups["contact_groups"] as NSArray

                for cG in contactGroups {
                    println(cG["group_id"]!)
                    //println(cG["contact_id"]!)
                }
            }

            if let contacts : AnyObject = json{
                //println(contacts["contacts"])
                println("Contacten")
                var contacts = contacts["contacts"] as NSArray

                for c in contacts{
                    println(c["id"])
                    println(c["name"])
                }
            }
        }    

这是控制台日志

 nil
 Optional(1)
 Optional(2)
 Contacten
 Optional(1)
 Optional(Dirk Sierd de Vries)

我希望你们能帮助我完成我的学校项目:D

【问题讨论】:

    标签: ios json core-data swift nsarray


    【解决方案1】:

    修改for循环如下:

    for c in contacts {
        println(c["id"]  as NSString)
        println(c["name"] as NSString)
    }
    

    说明:

    在 Swift 中,您必须将对象指定为特定类型。如果您不这样做,它将默认显示Optional 关键字。这里你的字典键值直接打印在日志上,你没有指定字典中存储的值类型。

    关于保存核心数据:

    没有任何很酷的框架可以将数据保存到核心数据中。你需要自己做。最好按照以下链接开始学习。

    Swift + Core data

    【讨论】:

    • 现在它在 for 循环后中断 nil Contact groepen Optional(1) Optional(2) Contacten (lldb)
    • 对不起,没关系!对于数字,我只需要使用 Int!谢谢@Kampai
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 2015-06-25
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多