iOS 多层级的immutable objects 转换成 mutable objects
第一种方法是:将多层级的递归转换
方法;
+(id) recursiveMutable:(id)object
{
if([object isKindOfClass:[NSDictionary class]])
{
NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithDictionary:object];
for(NSString* key in [dict allKeys])
{
[dict setObject:[App recursiveMutable:[dict objectForKey:key]] forKey:key];
}
return dict;
}
else if([object isKindOfClass:[NSArray class]])
{
NSMutableArray* array = [NSMutableArray arrayWithArray:object];
for(int i=0;i<[array count];i++)
{
[array replaceObjectAtIndex:i withObject:[App recursiveMutable:[array objectAtIndex:i]]];
}
return array;
}
else if([object isKindOfClass:[NSString class]])
return [NSMutableString stringWithString:object];
return object;
}
测试:
NSDictionary* testDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSDictionary dictionaryWithObject:@"Test" forKey:@"Key"],@"A",[NSArray arrayWithObjects:@"1",[NSNumber numberWithInt:2],@"3",nil],@"B",@"Test String",@"C",nil];
NSMutableDictionary* testMutableDict = [App recursiveMutable:testDict];
// Test A
[[testMutableDict objectForKey:@"A"] setObject:@"Test2" forKey:@"Key2"];
[[[testMutableDict objectForKey:@"A"] objectForKey:@"Key"] appendString:@" -- This Works"];
// Test B
[[testMutableDict objectForKey:@"B"] addObject:@"4"];
// Test C
[[testMutableDict objectForKey:@"C"] appendString:@" - Just testing NSMutableString again"];
NSLog(@"testMutableDict = %@",testMutableDict);
结果:
testMutableDict = { A = { Key = “Test — This Works”; Key2 = Test2; }; B = ( 1, 2, 3, 4 ); C = “Test String – Just testing NSMutableString again”; }
第二种:
使用coreFoundation框架里的CFPropertyListCreateDeepCopy,使用选项kCFPropertyListMutableContainersAndLeaves,意思是深度mutable
非ARC使用:
NSArray *immutableArray = [JSON objectForKey:@"result"];
self.myMutableArray = [(NSMutableArray *)CFPropertyListCreateDeepCopy(NULL, immutableArray, kCFPropertyListMutableContainersAndLeaves) autorelease];
ARC使用:
CFBridgingRelease(CFPropertyListCreateDeepCopy(NULL, (__bridge CFPropertyListRef)(immutableArray), kCFPropertyListMutableContainersAndLeaves))
版权声明:本文为博主原创文章,未经博主允许不得转载。
iOS 多层级的immutable objects 转换成 mutable objects的更多相关文章
- iOS 通过(lame)将录制音频转换成Mp3
版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处,保留原帖地址及作者署名. Url:http://blog.csdn.net/ysy441088327/article/detail ...
- IOS假设将一个十六进制的color转换成UIColor,非常有用
UI给开发的效果图非常多时候标注着十六进制的Color,而程序中用到的往往是UIColor能够用例如以下方法去转换: (UIColor *)RGBColorFromHexString:(NSStrin ...
- ios 把毫秒值转换成日期 NSDate
ios 把毫秒值转换成日期 (比较好用) 1343359790000 这是毫秒值------最佳解决方案-------------------- long long time=134335979000 ...
- Bodymovin:Bodymovin和Lottie:把AE动画转换成HTML5/Android/iOS原生动画
转自:https://www.cnblogs.com/zamhown/p/6688369.html 大杀器Bodymovin和Lottie:把AE动画转换成HTML5/Android/iOS原生动画 ...
- 安卓、ios时间转换成时间戳的形式
将日期转换成时间戳的形式,在安卓和ios不同的系统下转正会有兼容性的问题 安卓系统下Date.parse(new Date('2018-03-30 12:00:00'))会直接转换成时间戳的形式(简单 ...
- iOS 16进制字符串转换成int十进制
NSRange rangeErr; rangeErr.location = 6; rangeErr.length = 2; NSString *strings = [value substringWi ...
- iOS 秒数转换成时间,时,分,秒
//转换成时分秒 - (NSString *)timeFormatted:(int)totalSeconds{ int seconds = totalSeconds % 60; int min ...
- iOS swift HandyJSON组合Alamofire发起网络请求并转换成模型
在swift开发中,发起网络请求大部分开发者应该都是使用Alamofire发起的网络请求,至于请求完成后JSON解析这一块有很多解决方案,我们今天这里使用HandyJSON来解析请求返回的数据并转化成 ...
- iOS swift NSClassFromString将字符串转换成类名
在oc中将字符串转换成类名直接调用NSClassFromString("classname")即可,但是到了swift中变的麻烦多了 swift中如果要将字符串转换为类型需要以下几 ...
随机推荐
- Python:验证码识别
说明:此验证方法很弱,几乎无法识别出正确的验证码
- composer安装后台模板
先下载composer的windows安装包 cmd切换到源代码所在目录 https://www.cnblogs.com/wgphp/p/8001434.html 安装过程可以参照这篇文章 一点问题是 ...
- vue 复习篇. 注册全局组件,和 组件库
初篇 ============================================================== 1. 编写loading组件(components/Loading/ ...
- python爬虫 mac下安装使用Fiddler
HTTP代理工具Fiddler Fiddler是一款强大Web调试工具,它能记录所有客户端和服务器的HTTP请求. Getting started 在安装之前需要准备Mono环境 If you don ...
- jmter 5.1 中文
一.jmeter5.0下载解压后,默认的界面是英文版的,许多人觉得不方便,想要汉化,jmeter是不需要安装汉化包的,通过修改配置文件即可:1.找到jmeter解压后的文件夹,例如我是安装在D:\De ...
- 4.1 react 代码规范
关于 基础规范 组件结构 命名规范 jsx 书写规范 eslint-plugin-react 关于 在代码的设计上,每个团队可能都有一定的代码规范和模式,好的代码规范能够提高代码的可读性便于协作沟通, ...
- build protobuf to a static library
use ar cd src ar -cvq libprotobuf.a *.o
- Cuckoo架构
cuckoo在部署阶段,只在Guest系统里塞了一个agent,这个agent在运行阶段负责与Host端程序进行通信,从Host端接收sample, 整个客户端程序,以及配置文件. 在Host端主要的 ...
- PAT_A1024#Palindromic Number
Source: PAT A1024 Palindromic Number (25 分) Description: A number that will be the same when it is w ...
- 1044 Shopping in Mars (25 分)
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...