I use Stig Brautaset. The JSON structure serializes some objects, including NSDates (which are not directly supported).
I decided to use the NSDate description as a representation of the JSONFragment date (I don't care about the slight loss in accuracy caused by this).
To extend the Stig Brautaset JSON Framework to include NSDates, I defined a category:
@interface NSDate (NSDate_JSON) <JSONInitializer> -(NSString *) JSONFragment; @end
To recreate NSDate (and other classes) from JSON, I defined a protocol with the following initializer:
@protocol JSONInitializer <NSObject> -(id) initWithJSONRepresentation: (NSString *) aJSONRepresentation; @end
I am having problems with this initializer. In the case of NSDate, it just calls initWithString :, and I got into trouble: it always returns zero. This is the implementation:
#import "NSDate+JSON.h" @implementation NSDate (NSDate_JSON) -(NSString *) JSONFragment{ NSString *strRepr = [self description]; return [strRepr JSONFragment]; } -(id) initWithJSONRepresentation:(NSString *)aJSONRepresentation{ return [self initWithString: aJSONRepresentation]; //returns nil! } @end
I'm not sure what is going on. In addition, the compiler warns me that the initWithString: method in initWithJSONRepresentation: could not be found.
Does anyone know what might happen?
The full source code for the test case is available here .
objective-c cocoa nsdate protocols categories
cfischer Apr 29 2018-11-21T00: 00Z
source share