如何从NSSet返回NSMutableArray [英] How to return an NSMutableArray from an NSSet

查看:96
本文介绍了如何从NSSet返回NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以把 NSSet 的内容放到 NSMutableArray 中:

I'm able to put the contents of an NSSet into an NSMutableArray like this:

NSMutableArray *array = [set allObjects];

编译器抱怨,因为[set allObjects]返回 NSArray 不是 NSMutableArray

The compiler complains though because [set allObjects] returns an NSArray not an NSMutableArray. How should this be fixed?

推荐答案

由于 -allObjects 返回一个数组,你可以创建一个可变的版本:

Since -allObjects returns an array, you can create a mutable version with:

NSMutableArray *array = [NSMutableArray arrayWithArray:[set allObjects]];

或者,如果您要处理对象所有权:

Or, alternatively, if you want to handle the object ownership:

NSMutableArray *array = [[set allObjects] mutableCopy];

这篇关于如何从NSSet返回NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆