Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 402 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I convert NSMutableArray to NSArray?

#1
How do I convert NSMutableArray to NSArray in [tag:Objective-C]?


[1]:

[To see links please register here]

Reply

#2
you try this code---

NSMutableArray *myMutableArray = [myArray mutableCopy];

and

NSArray *myArray = [myMutableArray copy];
Reply

#3
An `NSMutableArray` is a subclass of `NSArray` so 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 `NSArray` either of these ways depending on whether you want it autoreleased or not:

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

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

**EDIT:** [The solution][1] provided 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.

[1]:

[To see links please register here]

Reply

#4
NSArray *array = [mutableArray copy];

[`Copy`][nscopying] makes immutable copies. This is quite useful because Apple can make various optimizations. For example sending `copy` to a immutable array only retains the object and returns `self`.

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

[nscopying]:

[To see links please register here]

:
Reply

#5
I like both of the 2 main solutions:

NSArray *array = [NSArray arrayWithArray:mutableArray];
Or

NSArray *array = [mutableArray copy];

The **primary difference** I see in them **is how they behave when mutableArray is nil**:

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

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

Reply

#6
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 *)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]`.

[Apple concurs:](

[To see links please register here]

)

> 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:

[To see links please register here]

Reply

#7
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

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

#8
**In objective-c :**

NSArray *myArray = [myMutableArray copy];

**In swift :**

var arr = myMutableArray as NSArray
Reply

#9
Objective-C
-----------
Below is way to convert NSMutableArray to 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 **`let`** keyword then it would become **NSArray** And if declare using **`var`** keyword then it's become **NSMutableArray**.

Sample code:

let newArray = oldArray as Array
Reply

#10
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.

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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through