NSDate的initWithString:返回nil [英] NSDate's initWithString: is returning nil
问题描述
我使用 Stig Brautaset 的JSON框架序列化一些对象,包括NSDates (不直接支持)。
我决定使用NSDate的描述作为日期的JSONFragment表示形式(我不在乎这样做)。
要扩展 Stig Brautaset 的JSON框架包含NSDates,我定义了一个类别:
@interface NSDate(NSDate_JSON)< JSONInitializer> ;
- (NSString *)JSONFragment;
@end
要重新创建一个NSDate ,我使用以下初始化程序定义了一个协议:
@protocol JSONInitializer< NSObject&
- (id)initWithJSONRepresentation:(NSString *)aJSONRepresentation;
@end
我遇到这个初始化程序的问题。在NSDate的情况下,它只是调用initWithString :,这是我陷入麻烦:它总是返回nil。这是实现:
#importNSDate + 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
我不知道发生了什么。此外,编译器警告我,无法找到initWithJSONRepresentation:中的initWithString:方法。
任何人都知道可能发生什么?
测试用例的完整源代码可用此处。
您的测试计划是:
NSDate * d1 = [[[NSDate alloc] init] autorelease];
NSString * repr = [d1 JSONFragment];
NSDate * dd = [[[NSDate alloc] initWithString:[d1 description]] autorelease];
NSDate * d2 = [[[NSDate alloc] initWithJSONRepresentation:repr] autorelease];
您的 -JSONFragment
类别方法是: / p>
- (NSString *)JSONFragment {
NSString * strRepr = [self description];
return [strRepr JSONFragment];
}
在这个方法中发生的是你获得该日期的字符串表示 -description
,然后使用 -JSONFragment
的字符串的JSON表示。
在SBJSON中, -JSONFragment
返回给定对象的表示形式为JSON数据。 JSON规范要求字符串被引用。在您的程序中:
NSString * repr = [d1 JSONFragment];
repr
$ c> @\2011-04-29 10:20:30 -0600 \。因为引号,该字符串不是要与 - [NSDate initWithString:]
使用的有效字符串。
如果您更改:
NSString * repr = [d1 JSONFragment];
到:
code> NSString * repr = [[d1 JSONFragment] JSONFragmentValue];
,以便SBJSON解析该片段并返回一个不带引号的字符串, >
I'm using Stig Brautaset's JSON framework serialize some objects, including NSDates (which are not directly supported).
I decided to use NSDate's description as the JSONFragment representation of a date (I don't care about the minor loss of precision incurred in doing so).
To extend Stig Brautaset's JSON framework to include NSDates, I defined a category:
@interface NSDate (NSDate_JSON) <JSONInitializer>
-(NSString *) JSONFragment;
@end
To recreate an NSDate (and other classes) from JSON, I defined a protocol with the following initializer:
@protocol JSONInitializer <NSObject>
-(id) initWithJSONRepresentation: (NSString *) aJSONRepresentation;
@end
I'm having issues with this initializer. In NSDate's case, it just calls initWithString:, and that's were I get into trouble: it always returns nil. 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's going on. Besides, the compiler warns me that the initWithString: method in initWithJSONRepresentation: could not be found.
Anybody knows what might be going on?
The full source code for a test case is available here.
Your test program is:
NSDate *d1 = [[[NSDate alloc] init] autorelease];
NSString *repr = [d1 JSONFragment];
NSDate *dd = [[[NSDate alloc] initWithString:[d1 description]] autorelease ];
NSDate *d2 = [[[NSDate alloc] initWithJSONRepresentation:repr] autorelease];
Your -JSONFragment
category method is:
-(NSString *) JSONFragment{
NSString *strRepr = [self description];
return [strRepr JSONFragment];
}
What’s happening in this method is that you obtain a string representation of that date using -description
, and then a JSON representation of that string using -JSONFragment
.
In SBJSON, -JSONFragment
returns the representation of a given object as JSON data. The JSON specification requires that strings are quoted. In your program:
NSString *repr = [d1 JSONFragment];
repr
contains a string like @"\"2011-04-29 10:20:30 -0600\""
. Because of the quotes, that string is not a valid string to be used with -[NSDate initWithString:]
.
If you change:
NSString *repr = [d1 JSONFragment];
to:
NSString *repr = [[d1 JSONFragment] JSONFragmentValue];
so that SBJSON parses that fragment and returns a string without the quotes, it should work.
这篇关于NSDate的initWithString:返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!