After spending too much time trying to find the best practices, here I am once again asking for some help, hoping I am not the only one struggling with this :
I have NSManaged Objects like these :
import Foundation
import CoreData
class Credential: NSManagedObject {
@NSManaged var credentialArrivalDate: String?
@NSManaged var credentialBarCode: String?
@NSManaged var credentialComment: String?
@NSManaged var credentialCountCheckIn: Int
@NSManaged var credentialCountCheckOut: Int
@NSManaged var credentialDepartureDate: String?
@NSManaged var credentialId: String?
@NSManaged var credentialIsArrived: Bool
@NSManaged var credentialIsCheckedOut: Bool
@NSManaged var credentialIsHost: Bool
@NSManaged var credentialLastModificationDate: String?
@NSManaged var credentialMemberControlled: String?
@NSManaged var credentialNumberOfPeople: Int
@NSManaged var credentialRSVP: Int
@NSManaged var credentialTypeId: String?
@NSManaged var credentialTypeName: String?
@NSManaged var credentialZoneId: String?
@NSManaged var credentialZoneName: String?
@NSManaged var credentialHosts: Set<Host>?
@NSManaged var credentialMember: Member
@NSManaged var credentialExtensionFields: Set<ExtensionFields>?
@NSManaged var credentialDeltaFirstCheckIn: String?
@NSManaged var credentialDeltaFirstCheckOut: String?
@NSManaged var credentialIsCheckedIn: Bool
}
I have a relationship with a Member Entity :
import Foundation
import CoreData
class Member: NSManagedObject {
@NSManaged var memberCompany: String
@NSManaged var memberEMail: String
@NSManaged var memberFirstName: String
@NSManaged var memberId: String
@NSManaged var memberJob: String
@NSManaged var memberLastModificationDate: String
@NSManaged var memberLastName: String
@NSManaged var memberPhoneNumber: String
@NSManaged var memberTitle: String
@NSManaged var memberCredential: Credential
}
What I am trying to do is to serialize my Credential object to JSON using NSJSONSerialization.dataWithJSONObject to generate my JSON and POST it to a Server.
However, I've got an error while serializing my object, and I believe this is because my object does not respect this (from Apple documentation) :
An object that may be converted to JSON must have the following properties:
- The top level object is an NSArray or NSDictionary.
- All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
- All dictionary keys are instances of NSString.
- Numbers are not NaN or infinity.
So how can I Serialize my Object to JSON ? Should I find some way to convert a Member to a Dictionary, then try to find a way to set this dictionary as the value for my relationship with a credential ?
Have I missed a built-in mapper that handles it ?
I am sorry if that is not clear but any help would be very nice.
In advance, thank you.
Hugo
EDIT :
As suggested in an answer, I tried to fetch a dictionary instead of a NSManagedObject but the logged dictionary is empty (however, the same code worked for a very simple class with a few string attributes and no relationship), here is the code :
/*******************************
TEST : Fetch dictionary instead of NSManagedObject
*/
// create Credential entity
let newCredential = NSEntityDescription.insertNewObjectForEntityForName("Credential", inManagedObjectContext: self.managedObjectContext!) as! Credential
// create Credential entity
let newMember = NSEntityDescription.insertNewObjectForEntityForName("Member", inManagedObjectContext: self.managedObjectContext!) as! Member
// instanciate some fields
newCredential.credentialId = "44444"
newMember.memberFirstName = "TEST"
// set the relationship
newCredential.credentialMember = newMember
// retrieve currentCredential
let requestGuest = NSFetchRequest(entityName: "Credential")
// get a dictionary instead of NSManagedObject
requestGuest.resultType = NSFetchRequestResultType.DictionaryResultType
let fetchedGuest = (managedObjectContext!.executeFetchRequest(requestGuest, error:nil))! // no need to cast here ?
println(fetchedGuest.description)
/*******************************/