第一种方法是:将多层级的递归转换

方法;

+(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的更多相关文章

  1. iOS 通过(lame)将录制音频转换成Mp3

    版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处,保留原帖地址及作者署名. Url:http://blog.csdn.net/ysy441088327/article/detail ...

  2. IOS假设将一个十六进制的color转换成UIColor,非常有用

    UI给开发的效果图非常多时候标注着十六进制的Color,而程序中用到的往往是UIColor能够用例如以下方法去转换: (UIColor *)RGBColorFromHexString:(NSStrin ...

  3. ios 把毫秒值转换成日期 NSDate

    ios 把毫秒值转换成日期 (比较好用) 1343359790000 这是毫秒值------最佳解决方案-------------------- long long time=134335979000 ...

  4. Bodymovin:Bodymovin和Lottie:把AE动画转换成HTML5/Android/iOS原生动画

    转自:https://www.cnblogs.com/zamhown/p/6688369.html 大杀器Bodymovin和Lottie:把AE动画转换成HTML5/Android/iOS原生动画 ...

  5. 安卓、ios时间转换成时间戳的形式

    将日期转换成时间戳的形式,在安卓和ios不同的系统下转正会有兼容性的问题 安卓系统下Date.parse(new Date('2018-03-30 12:00:00'))会直接转换成时间戳的形式(简单 ...

  6. iOS 16进制字符串转换成int十进制

    NSRange rangeErr; rangeErr.location = 6; rangeErr.length = 2; NSString *strings = [value substringWi ...

  7. iOS 秒数转换成时间,时,分,秒

    //转换成时分秒 - (NSString *)timeFormatted:(int)totalSeconds{ int seconds = totalSeconds % 60;     int min ...

  8. iOS swift HandyJSON组合Alamofire发起网络请求并转换成模型

    在swift开发中,发起网络请求大部分开发者应该都是使用Alamofire发起的网络请求,至于请求完成后JSON解析这一块有很多解决方案,我们今天这里使用HandyJSON来解析请求返回的数据并转化成 ...

  9. iOS swift NSClassFromString将字符串转换成类名

    在oc中将字符串转换成类名直接调用NSClassFromString("classname")即可,但是到了swift中变的麻烦多了 swift中如果要将字符串转换为类型需要以下几 ...

随机推荐

  1. leetcode-回溯②-难题

    题10: 回溯:另:动态规划复杂度更低 class Solution: def isMatch(self, s: str, p: str) -> bool: def helper(s,p): i ...

  2. Linux下安装PHP的curl扩展

    先安装依赖包: yum install curl curl-devel 找到PHP的安装包,cd 进入安装包 cd php-5.6.25/ext/curl phpize 如果报找不到phpize就补全 ...

  3. Spring 容器初始化方法

    Resource resource = new ClassPathResource("/bean.xml");         resource = new FileSystemR ...

  4. 一个小BUG引发的思考。(论开发与测试之间的那点事)

    标题不是“一个馒头引发的血案”. 言归正传:今天上午测试的时候,发现了一个BUG,如图: 一个用肉眼就能发现的BUG.原因当然是因为开发同事没有自测试,流入到了测试人员这里了. 无非是开发同事不严谨造 ...

  5. JAVA学习之跨平台性

    Java语音的特点:跨平台性什么是跨平台性通过Java语音编写的应用程序再不同的系统平台上都可以运行. 原理是什么只要在需要运行Java应用程序的操作系统上.先安装一个Java虚拟机(JVM Java ...

  6. MVC3中_ViewStart.cshtml的一些事

    MVC3中,使用Razor模板引擎的话,会自动生成一个_ViewStart.cshtml文件. 很多人把这个文件当成类似Webform中的Master模板文件,其实这个_ViewStart.cshtm ...

  7. 1089 Insert or Merge (25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  8. 【react】---Hooks的基本使用---【巷子】

    一.react-hooks概念 React中一切皆为组件,React中组件分为类组件和函数组件,在React中如果需要记录一个组件的状态的时候,那么这个组件必须是类组件.那么能否让函数组件拥有类组件的 ...

  9. k8s 资源管理

    对应到Kubernetes的Pod容器上,就是下面这4个参数:◎ spec.container[].resources.requests.cpu:◎ spec.container[].resource ...

  10. Module not found: Error: Can't resolve "xxx" in "xxx"

    报错信息 ERROR in multi ./src/index.js ./dist/bundle.js Module not found: Error: Can't resolve './dist/b ...