我是目标c的新手。 我不明白为什么我的函数不起作用,它在返回函数时发出错误SIGBART。
+ (NSArray*) jsonToArray:(NSString*)jsonString
{
id mappedObj = nil;
RKObjectMappingProvider* mapProv = [[RKObjectMappingProvider alloc] init];
RKObjectMapping* answerMapping = [SMAnswersWrapper entityMappingForSerializer];
answerMapping.objectClass = [NSMutableDictionary class];
[mapProv setSerializationMapping:answerMapping forClass:[SMAnswersWrapper class]];
RKObjectMapping *proofsEntityMapping = [SMEntityArrayWrapper entityMappingWithContentMapping:answerMapping];
[mapProv setMapping:proofsEntityMapping forKeyPath:@""];
if (jsonString)
{
id obj = [jsonString objectFromJSONString];
if (obj)
{
RKObjectMapper* mapper = [[RKObjectMapper alloc] initWithObject:obj mappingProvider:mapProv];
RKObjectMappingResult* res = [mapper performMapping];
mappedObj = [res asCollection];
[mapper release];
}
}
[mapProv release];
return [[[mappedObj content] retain] autorelease];
}
错误位置在此行:
return [[[mappedObj content] retain] autorelease];
谢谢。
修改
我发现了我的错误,我无法访问[[[mappedObj content] retain] autorelease];
的内容,我必须转换mappedObj。
答案 0 :(得分:0)
请尝试以下操作:
+ (NSArray*) jsonToArray:(NSString*)jsonString
{
id mappedObj = nil;
RKObjectMappingProvider* mapProv = [[RKObjectMappingProvider alloc] init];
RKObjectMapping* answerMapping = [SMAnswersWrapper entityMappingForSerializer];
answerMapping.objectClass = [NSMutableDictionary class];
[mapProv setSerializationMapping:answerMapping forClass:[SMAnswersWrapper class]];
RKObjectMapping *proofsEntityMapping = [SMEntityArrayWrapper entityMappingWithContentMapping:answerMapping];
[mapProv setMapping:proofsEntityMapping forKeyPath:@""];
if (jsonString)
{
id obj = [jsonString objectFromJSONString];
if (obj)
{
RKObjectMapper* mapper = [[RKObjectMapper alloc] initWithObject:obj mappingProvider:mapProv];
RKObjectMappingResult* res = [mapper performMapping];
mappedObj = [res asCollection];
[mapper release];
}
}
[mapProv release];
if (mappedObj)
{
return [[[mappedObj content] retain] autorelease];
}
return [NSArray array];
}
如果jsonString
或obj
为nil,则在您的代码中,不会初始化mappedObj。这种情况可能会在访问[mappedObj content]
时导致崩溃。
试试这个,这会对你有所帮助.....
答案 1 :(得分:0)
我发现了我的错误,我无法访问[[[mappedObj content] retain] autorelease];
的内容,我必须投射mappedObj。