我想将我的swiftyJson结果发送到coredata进行保存。
我设法使用我的密钥和详细信息获取数组数组,但我不确定如何正确地将它们传递给匹配的实体?
这是我的代码:
open func getData() {
Alamofire.request("http://demomedia.co.uk/files/contacts.json").responseJSON { (responseData) -> Void in
if ((responseData.result.value) != nil) {
let swiftyValue = JSON(responseData.result.value!)
if let resData = swiftyValue[].arrayObject {
self.result = resData as! [[String:AnyObject]]
}
// if values passed and formatted...
if self.result.count > 0 {
// for each contact result
for contact in self.result {
print(contact)
}
}
}
}
}
以下是打印示例:
["surname": Peters, "firstname": Julia, "email": jpeters@email.com, "phoneNumber": 07865543432, "createdAt": 1501170757, "address": 66 New Street, "updatedAt": 1501170762, "title": Miss]
我有一个与这些属性名称匹配的对象,如何将其传递给它们并保存?
更新代码:
open func getData() {
Alamofire.request("http://demomedia.co.uk/files/contacts.json").responseJSON { (responseData) -> Void in
if ((responseData.result.value) != nil) {
let value = JSON(responseData.result.value!)
if let resData = value[].arrayObject {
self.result = resData as! [[String:AnyObject]]
}
// if values passed and formatted...
for contact in self.result {
let newContact = Contact()
newContact.surname = contact["surname"] as? String
newContact.firstname = contact["firstname"] as? String
newContact.email = contact["email"] as? String
newContact.phoneNumber = (contact["phoneNumber"] as? Int64)!
newContact.createdAt = (contact["createdAt"] as? Int64)!
newContact.address = contact["address"] as? String
newContact.createdAt = (contact["updatedAt"] as? Int64)!
newContact.title = contact["title"] as? String
do {
try self.context.save()
} catch {
fatalError("Failure to save context: \(error)")
}
}
}
}
}