Closed martinfanio closed 8 years ago
I had this issue as well. It was because my class to map to was not extending Mappable.
import Foundation
import RealmSwift
import ObjectMapper
class InventoryCategory: Object, Mappable { // <-- you are probably missing `Mappable`
// Realm and ObjectMapper code here
}
@levibostian I have extended my class to Mappable as :
import RealmSwift
import ObjectMapper
class InventoryCategory: Object, Mappable
{
dynamic var id : Int = 0
dynamic var name : String = ""
dynamic var type : String = ""
dynamic var created_at = NSDate()
dynamic var modified_at = NSDate()
....
}
But the error still persists
Hmmmm...I don't have another idea for you at the moment. I will have to keep playing around with my project and come back if I have any more ideas for you.
I found the solution, changed method to responseJSON and added NSJSONSerialization lines to retrieve the response. It worked, and I still didn't know why using method responseObject produced above error.
import Foundation
import Alamofire
import ObjectMapper
import RealmSwift
import SwiftyJSON
class ProductCategorySync {
static func loadData()-> Void{
Alamofire.request(ProductCategory.ReadAll())
.responseJSON { response in
switch response.result {
case .Success:
let realm = try! Realm()
if let json = response.result.value {
let item = JSON(json)
let results : NSData = try! item["result"].rawData()
do{
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(results, options: []) as? NSArray {
for dict in jsonResult as! [NSDictionary] {
let category = InventoryCategory.mappedCategory(dict as! Dictionary<String, AnyObject>)
try realm.write{
realm.add(category, update: true)
}
}
}
} catch let error as NSError {
print(error)
}
}
break
case .Failure:
break
}
}
}
}
And added this method to InventoryCategory Model class :
static func mappedCategory(dict:Dictionary<String, AnyObject>) -> InventoryCategory{
return Mapper<InventoryCategory>().map(dict)! as InventoryCategory
}
Thank you!
Hmmm...not quite the solution you were looking for as .responseJSON
is an Alamofire function not a AlamofireObjectMapper function.
From viewing your code I didn't see any issues. Not quite sure how to help without seeing more. Glad you have something working though.
I can share some code that is working for myself too.
Yea I know .. but this is the fastest solution to make my code works, actually I want to use .responseObject but time is running to finish the project. If you don't mind you can share your working code here, Thank you.
Hmmm...not quite the solution you were looking for as
.responseJSON
is an Alamofire function not a AlamofireObjectMapper function.From viewing your code I didn't see any issues. Not quite sure how to help without seeing more. Glad you have something working though.
I can share some code that is working for myself too.
Yes, please! Can you please show us your code?
Hi, I got this error when build this class : Cannot convert value of type '(Response<InventoryCategory, NSError>) -> ()' to expected argument type 'Response<_, NSError> -> Void'
Here the class for Alamofire request :
The code worked fine for parsing JSON data, but got error when I added lines for mapping JSON to Realm.
Thank you and appreciate for any help ...