Reputation: 1007
When I try to cast an NSManagedObject to my model class (SCUser) while using RestKit 0.23.0 in Swift, the return value is nil. Here's the operation:
var urlString:String = SCApi.sharedInstance.apiUrl + "users/find_or_create?name=\(name)&email=\(email)"
var url = NSURL(string: urlString)!
var request = NSURLRequest(URL: url)
var responseDescriptor = RKResponseDescriptor(mapping: SCUser.entityMapping(), method: RKRequestMethod.Any, pathPattern: nil, keyPath: "user", statusCodes: nil)
var operation = RKManagedObjectRequestOperation(request: request, responseDescriptors: [responseDescriptor])
operation.managedObjectContext = RKManagedObjectStore.defaultStore().mainQueueManagedObjectContext
operation.managedObjectCache = RKManagedObjectStore.defaultStore().managedObjectCache
operation.setCompletionBlockWithSuccess(
{
(operation:RKObjectRequestOperation!, result:RKMappingResult!) -> Void in
println("result: \(result.firstObject())")
var object:NSManagedObject = result.firstObject() as NSManagedObject
if let user = (object as? SCUser) {
println("success: \(user)")
} else {
println("failure")
}
},
failure: {
(operation:RKObjectRequestOperation!, error:NSError!) -> Void in
println("error: \(error)")
}
)
operation.start()
When this runs, it indicates that result.firstObject()
is an SCUser, but doesn't cast it as one:
result: <NSManagedObject: 0x1740aee20> (entity: SCUser; id: 0xd000000000140000 <x-coredata://7CFB18B0-0E2D-4190-B8DF-20470425CFE3/SCUser/p5> ; data: {
authenticationToken = Dt7tjbLtUzSAgBdzCyC3;
name = g;
email = "[email protected]";
userId = "9e99451d-ef9d-4876-bfd3-4a987c2fe316";
})
failure
I've put the SCUser class and RestKit initialization here, in case that's helpful.
Upvotes: 1
Views: 529
Reputation: 119031
This log output
result: <NSManagedObject: 0x1740aee20> (entity: SCUser; id: 0xd000000000140000 <x-coredata://7CFB18B0-0E2D-4190-B8DF-20470425CFE3/SCUser/p5> ; data: {
authenticationToken = Dt7tjbLtUzSAgBdzCyC3;
name = g;
email = "[email protected]";
userId = "9e99451d-ef9d-4876-bfd3-4a987c2fe316";
})
shows that the class is actually NSManagedObject
, even though the entity is SCUser
. Most likely this is because you haven't set the class name in your model, and presumably you have manually generated the SCUser
class code files? You need to set the class name (and preferably auto-generate the class files, either with Xcode or mogenerator).
Upvotes: 1