0

I have a array inside my init method and then I wish to make a mutable array inside if condition when it's actually needed. Is it possible?

Presently am doing:

- (id)init
{
   self = [super init];

   if (self)
   {
      // [MyClass someMethod] in the below statement returns an array.
      NSMutableArray *someArray = [NSMutableArray arrayWithArray:[MyClass someMethod]];

      if (this condition is true)
      {
           [someArray addObject:@"XYZ"];
      }

     // Print someArray here.
   }
}

What I am trying to do it:

- (id)init
    {
       self = [super init];

       if (self)
       {
          // [MyClass someMethod] in the below statement returns an array.
          NSArray *someArray = @[[MyClass someMethod]];

          if (this condition is true)
          {
               // Make some array mutable here and then add an object to it.
               [someArray mutableCopy];
               [someArray addObject:@"XYZ"];
          }

         // Print someArray here.
       }
    }

Am I doing it right? Or is what I am thinking possible? Can I make the same array mutable whenever needed as in my case I need it to be mutable only if my condition in if is true.

1
  • It's possible to create a mutable array from a non-mutable array, but what are you doing with the mutable array afterwards? Commented Jul 17, 2014 at 14:05

2 Answers 2

1

You should change code in if condition:

if (this condition is true)
{
     // Make some array mutable here and then add an object to it.
     NSMutableArray *mutableArray = [someArray mutableCopy];
     [mutableArray addObject:@"XYZ"];
     someArray = mutableArray.copy;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Add someArray = mutableArray;
Done. But in little bit different way.
I wonder why do you call copy method in last line. NSMutableArray inherits from NSArray so copying object shoudn't be necessary.
I was about to post the same question as Szu.
Using copy to turn it back to immutable array is a good idea since the original array is immutable. The reasons for that (beside consistency) are possible optimisations on array objects access (though I believe there are none for this object at the moment) and if the array was later typecast and then muted it would raise an exception as it should. Anyway if you claim there is no reason to use a NSArray in this case there is no reason to use it anywhere at all, or am I wrong?
|
0

How about this?

{
   NSArray *array;
   if(condition)
   {
     NSMutableArray *mutaArray = [NSMutableArray arrayWithArray:[MyClass someMethod]];
     [mutaArray addObject:@"XYZ"];
     array=mutaArray;
   }
   else
     array=[MyClass someMethod];

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.