CFPropertyListCreateDeepCopy returns nil if any value is NULL

和自甴很熟 提交于 2019-12-10 04:23:07

问题


I am using the following CoreFoundation function CFPropertyListCreateDeepCopy: for converting the immutable objects to mutable objects.If any of the object is NULL the CFPropertyListCreateDeepCopy returning empty .Is there any work around for this.

self.packageArray  = CFBridgingRelease(CFPropertyListCreateDeepCopy(NULL, (CFPropertyListRef)self.packageArray , kCFPropertyListMutableContainersAndLeaves));

CFPropertyListCreateDeepCopy fails to process array / dictionary containing NSNull

sample code

 NSArray *immutable = @[ @"a", [NSNull null], @"c" ];      
 NSMutableArray *mutable = (__bridge 
   id)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (__bridge 
        CFArrayRef)immutable, kCFPropertyListMutableContainers);

sample json response from this link

Thanks in advance.


回答1:


After few hours of workaround, I have solved this issue by below way.

Just place below line when converting API response to JSON Object.

responseString=[responseString stringByReplacingOccurrencesOfString:@"\":null" withString:@"\":\"\""];//To Handle Null Characters

//Search for below line in your parsing library and paste above code
data = [responseString dataUsingEncoding:NSUTF8StringEncoding];

So there will be no null characters in your JSON object, hence no issue with using CFPropertyListCreateDeepCopy.

Cheers!!



来源:https://stackoverflow.com/questions/34219638/cfpropertylistcreatedeepcopy-returns-nil-if-any-value-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!