Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7508717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:43:28+00:00 2026-05-29T22:43:28+00:00

I’m Trying here to add many arrays to one NSMutableArray Actually am adding the

  • 0

I’m Trying here to add many arrays to one NSMutableArray
Actually am adding the same array with different values Many Times to one NSMutable Array
this the code:

NSMutableArray* wordsArray =[[NSMutableArray alloc] init ];
NSMutableArray* meaningsArray=[[NSMutableArray alloc]init];
NSMutableArray* wordsArrayTemp=[[NSMutableArray alloc]init];
NSMutableArray* meaningsArrayTemp=[[NSMutableArray alloc]init ];
NSMutableArray* allWords =[[NSMutableArray alloc]initWithCapacity:2];
NSMutableArray* allMeanings=[[NSMutableArray alloc]initWithCapacity:2];




 for(int i=0;i<2;i++)
{
    int wordsCounter=0;
     [wordsArrayTemp removeAllObjects];
[meaningsArrayTemp removeAllObjects];


    for(NSString *tmp in wordsArray)
        {  
            NSString *meaning =[meaningsArray objectAtIndex:wordsCounter++];

            subtmp= [ tmp substringWithRange:NSMakeRange(0,1)];

            if([currentTable isEqualToString:@"arabicToEnglish"])
            { 
                if([subtmp isEqualToString:[arabicLetters objectAtIndex:i]])
                {
                    [wordsArrayTemp addObject:tmp];
                    [meaningsArrayTemp addObject:meaning];
                }
            }
            else
                if([subtmp isEqualToString:[englishLetters objectAtIndex:i]])
                { 
                    [wordsArrayTemp addObject:tmp];
                    [meaningsArrayTemp addObject:meaning];
                }    
        }
  [allWords insertObject:wordsArrayTemp atIndex:i];
   // [allWords addObject: wordsArrayTemp];
    [allMeanings addObject:meaningsArrayTemp]; 
  NSLog(@"all words count%i",[[allWords objectAtIndex:i] count]);  
} 

The Problem :
The supposed behavior here is to have 2 different values in the allWords array .
But what Actually happens is that the 2 values filled with the same value with the last index Value.
I mean [allWords objectAtIndex:0] should have 2000 object and [allWords objectAtIndex:1] should have 3000 ,but what happens that they both have 3000 object !!

what am I missing here?!!
thnx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:43:29+00:00Added an answer on May 29, 2026 at 10:43 pm

    when you add an object to an array the object is not copied. You just save its memory address.

    Basically you added the same temporary array to the parent array. And you did all your array manipulations to the same array.

    Maybe this piece of unrolled loop code will make it a little bit clearer.

    // create new array on a specific memory address. let's say this address is 0x01
    NSMutableArray* wordsArrayTemp=[[NSMutableArray alloc]init];
    
    // first iteration of your loop
    
    // remove all objects from array at memory address 0x01
    [wordsArrayTemp removeAllObjects];
    
    // add objects to the array at address 0x01
    [wordsArrayTemp addObject:tmp];
    
    // insert array (still at address 0x01) to the parent array 
    [allWords insertObject:wordsArrayTemp atIndex:i];
    // your allWords array now looks like this: {array@0x01}
    
    // second iteration of your loop
    
    // remove all objects from array at memory address 0x01!!! (still the same array as in the first iteration)
    // since it's the same array all objects from the array at [allWords objectAtIndex:0] are removed too
    [wordsArrayTemp removeAllObjects];
    
    // add objects to the array at address 0x01
    [wordsArrayTemp addObject:tmp];
    
    // insert array (still at address 0x01) to the parent array 
    [allWords insertObject:wordsArrayTemp atIndex:i];
    // your allWords array now looks like this {array@0x01, array@0x01}
    

    the solution is pretty easy.

    At the beginning of the for-loop instead of removing allObjects from the array create new arrays.
    Just replace

    [wordsArrayTemp removeAllObjects];
    [meaningsArrayTemp removeAllObjects];
    

    with

    wordsArrayTemp = [NSMutableArray array];
    meaningsArrayTemp = [NSMutableArray array];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.