objective-c 如何将 NSMutableArray 转换为 NSArray?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1768937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:31:32  来源:igfitidea点击:

How do I convert NSMutableArray to NSArray?

objective-ccocoa-touchcocoansmutablearraynsarray

提问by marcy

How do I convert NSMutableArray to NSArray in objective-c?

如何在objective-c中将NSMutableArray 转换为NSArray ?

回答by Georg Sch?lly

NSArray *array = [mutableArray copy];

Copymakes immutable copies. This is quite useful because Apple can make various optimizations. For example sending copyto a immutable array only retains the object and returns self.

Copy制作不可变的副本。这非常有用,因为 Apple 可以进行各种优化。例如发送copy到一个不可变数组只保留对象并返回self.

If you don't use garbage collection or ARC remember that -copyretains the object.

如果您不使用垃圾收集或 ARC,请记住-copy保留对象。

回答by hallski

An NSMutableArrayis a subclass of NSArrayso you won't always need to convert but if you want to make sure that the array can't be modified you can create a NSArrayeither of these ways depending on whether you want it autoreleased or not:

AnNSMutableArray是 of 的子类,NSArray因此您并不总是需要转换,但是如果您想确保无法修改数组,则可以NSArray根据您是否希望它自动发布来创建以下任一方式:

/* Not autoreleased */
NSArray *array = [[NSArray alloc]?initWithArray:mutableArray];

/* Autoreleased array */
NSArray *array = [NSArray arrayWithArray:mutableArray];

EDIT:The solutionprovided by Georg Sch?lly is a better way of doing it and a lot cleaner, especially now that we have ARC and don't even have to call autorelease.

编辑:Georg Sch?lly 提供的解决方案是一种更好的方法,而且更简洁,尤其是现在我们有了 ARC,甚至不必调用 autorelease。

回答by Richard Venable

I like both of the 2 main solutions:

我喜欢这两个主要解决方案:

NSArray *array = [NSArray arrayWithArray:mutableArray];

Or

或者

NSArray *array = [mutableArray copy];

The primary differenceI see in them is how they behave when mutableArray is nil:

我在它们中看到的主要区别是当 mutableArray 为 nil 时它们的行为方式

NSMutableArray *mutableArray = nil;
NSArray *array = [NSArray arrayWithArray:mutableArray];
// array == @[] (empty array)

NSMutableArray *mutableArray = nil;
NSArray *array = [mutableArray copy];
// array == nil

回答by Jerry Thomsan

you try this code---

你试试这个代码---

NSMutableArray *myMutableArray = [myArray mutableCopy];

and

NSArray *myArray = [myMutableArray copy];

回答by Sakir Sherasiya

Objective-C

目标-C

Below is way to convert NSMutableArray to NSArray:

下面是将 NSMutableArray 转换为 NSArray 的方法:

//oldArray is having NSMutableArray data-type.
//Using Init with Array method.
NSArray *newArray1 = [[NSArray alloc]initWithArray:oldArray];

//Make copy of array
NSArray *newArray2 = [oldArray copy];

//Make mutablecopy of array
NSArray *newArray3 = [oldArray mutableCopy];

//Directly stored NSMutableArray to NSArray.
NSArray *newArray4 = oldArray;

Swift

迅速

In Swift 3.0 there is new data type Array. Declare Array using letkeyword then it would become NSArrayAnd if declare using varkeyword then it's become NSMutableArray.

在 Swift 3.0 中有新的数据类型Array。使用let关键字声明数组然后它会变成NSArray如果声明使用var关键字然后它变成NSMutableArray

Sample code:

示例代码:

let newArray = oldArray as Array

回答by Vikram Biwal

In objective-c :

在目标-c 中:

NSArray *myArray = [myMutableArray copy];

In swift :

迅速:

 var arr = myMutableArray as NSArray

回答by Anton Tropashko

NSArray *array = mutableArray;

This [mutableArray copy]antipattern is all over sample code. Stop doing so for throwaway mutable arrays that are transient and get deallocated at the end of the current scope.

这种[mutableArray copy]反模式遍布示例代码。对于瞬态的一次性可变数组,请停止这样做,并在当前范围的末尾被释放。

There is no way the runtime could optimize out the wasteful copying of a mutable array that is just about to go out of scope, decrefed to 0 and deallocated for good.

运行时无法优化对即将超出范围的可变数组的浪费性复制,减少为 0 并永久释放。

回答by pkamb

If you're constructing an array via mutability and then want to return an immutable version, you can simply return the mutable array as an "NSArray" via inheritance.

如果您通过可变性构造一个数组,然后想要返回一个不可变的版本,您可以简单地通过继承将可变数组作为“NSArray”返回。

- (NSArray *)arrayOfStrings {
    NSMutableArray *mutableArray = [NSMutableArray array];
    mutableArray[0] = @"foo";
    mutableArray[1] = @"bar";

    return mutableArray;
}

If you "trust" the caller to treat the (technically still mutable) return object as an immutable NSArray, this is a cheaper option than [mutableArray copy].

如果您“信任”调用者将(技术上仍然可变的)返回对象视为不可变的 NSArray,则这是比[mutableArray copy].

Apple concurs:

苹果同意:

To determine whether it can change a received object, the receiver of a message must rely on the formal type of the return value. If it receives, for example, an array object typed as immutable, it should not attempt to mutate it. It is not an acceptable programming practice to determine if an object is mutable based on its class membership.

要确定它是否可以更改接收到的对象,消息的接收者必须依赖返回值的形式类型。例如,如果它接收到类型为不可变的数组对象,则不应尝试对其进行变异。根据对象的类成员资格来确定对象是否可变是不可接受的编程实践。

The above practice is discussed in more detail here:

此处更详细地讨论了上述做法:

Best Practice: Return mutableArray.copy or mutableArray if return type is NSArray

最佳实践:如果返回类型是 NSArray,则返回 mutableArray.copy 或 mutableArray

回答by Amr Angry

i was search for the answer in swift 3 and this question was showed as first result in search and i get inspired the answer from it so here is the swift 3 code

我在 swift 3 中搜索答案,这个问题在搜索中显示为第一个结果,我从中得到了启发,所以这里是 swift 3 代码

let array: [String] = nsMutableArrayObject.copy() as! [String]