用空字符串替换从服务器接收过来的NSNULL类型的数据
2013-11-22 16:04
113 查看
很多时候服务器传过来的空数据是NSNULL类型的,客户端没有做好判断,很容易造成程序崩溃。我们可以将传过来的空数据用@""代替来解决问题。分别为字典和数组添加一个类目方法,将接收的字典或数组调用该方法即可。代码如下。
本文出自 “破虏的冬天” 博客,请务必保留此出处http://chenpolu.blog.51cto.com/4292751/1330091
//字典 @interface NSDictionary (JRAdditions) - (NSDictionary *)dictionaryByReplacingNullsWithStrings; @end @implementation NSDictionary (JRAdditions) - (NSDictionary *)dictionaryByReplacingNullsWithStrings { const NSMutableDictionary *replaced = [self mutableCopy]; const id nul = [NSNull null]; const NSString *blank = @""; for (NSString *key in self) { id object = [self objectForKey:key]; if (object == nul) [replaced setObject:blank forKey:key]; else if ([object isKindOfClass:[NSDictionary class]]) [replaced setObject:[object dictionaryByReplacingNullsWithStrings] forKey:key]; else if ([object isKindOfClass:[NSArray class]]) [replaced setObject:[object arrayByReplacingNullsWithBlanks] forKey:key]; } return [NSDictionary dictionaryWithDictionary:[replaced copy]]; } @end //数组 @interface NSArray (NullReplacement) - (NSDictionary *)arrayByReplacingNullsWithBlanks; @end @implementation NSArray (NullReplacement) - (NSArray *)arrayByReplacingNullsWithBlanks { NSMutableArray *replaced = [self mutableCopy]; const id nul = [NSNull null]; const NSString *blank = @""; for (int idx = 0; idx < [replaced count]; idx++) { id object = [replaced objectAtIndex:idx]; if (object == nul) [replaced replaceObjectAtIndex:idx withObject:blank]; else if ([object isKindOfClass:[NSDictionary class]]) [replaced replaceObjectAtIndex:idx withObject:[object dictionaryByReplacingNullsWithStrings]]; else if ([object isKindOfClass:[NSArray class]]) [replaced replaceObjectAtIndex:idx withObject:[object arrayByReplacingNullsWithBlanks]]; } return [replaced copy]; } @end
本文出自 “破虏的冬天” 博客,请务必保留此出处http://chenpolu.blog.51cto.com/4292751/1330091
相关文章推荐
- 服务器返回的数据,用数据类型接收(int,NSNumber)?
- json 解析double类型多出几位小数,利用NSDecimalNumber修复 , 服务器返回的数据,用数据类型接收(int,NSNumber)?
- 服务器如何接收GPS定位器发送过来的数据
- Android 程序中只接收特定类型的数据
- PHP POST接收处理 IOS上传NSData图片数据,上传图片到服务器
- live555从RTSP服务器读取数据到使用接收到的数据流程分析
- linux下如何控制RTS?为了控制RS485发送正常,但无法接收到端发送过来的数据
- mysql从csv文件导入数据时提示int类型出现' '(空字符串)
- 提高生产力:SpringMVC中,使用扩展数据类型TypedMap接收Web请求参数
- 用httpwebrequest/httpwebresponse向服务器POST发送及接收数据
- Android开发:客户端与服务器通过传递和接收json数据进行交互
- Flex接收51单片机发送过来的16进制数据转换为String
- ajax处理服务器返回的三种数据类型方法
- 批量替换数据库中所有用户数据表中字段数据类型为char和varchar到nvarchar的脚本
- 服务器用map来接收数据
- VLC通过rtsp请求服务器,服务器接收rtp数据并转发
- 设计网页录入信息与自定义服务器数据接收
- java接收到大数据类型
- NSString的各种用法总结(创建、截取、判断比较、转化数据类型、拼接、替换、添加、追加、读取、写入、删去、改变)