Luca
Luca

Reputation: 20959

problem when trying to put the content of NSArray in NSMutableArray

please i try to put the content of two NSArray in two NSMutableArray but it seems not working :

NSMutableArray *newArrayTypeCarburant = [[NSMutableArray alloc]init];
    NSMutableArray *newArrayNomStation = [[NSMutableArray alloc]init];
    for(int i=0; i <[topStation.listeTypesDesCarburants count]; i++)
    {
        [newArrayTypeCarburant addObjectsFromArray: [topStation.listeTypesDesCarburants objectAtIndex:i]]; 
    }
    for(int i=0; i <[topStation.listeDesEnseignes count]; i++)
    {
        [newArrayNomStation addObjectsFromArray: [topStation.listeDesEnseignes objectAtIndex:i]]; 
    }

topStation.listeDesEnseignes and topStation.listeTypesDesCarburants are two NSArray and global variables declared in the appdelegate class . Help please, thx in advance ))

EDIT i try that :

NSMutableArray *newArrayTypeCarburant = [[NSMutableArray alloc]init];
    NSMutableArray *newArrayNomStation = [[NSMutableArray alloc]init];



    for(int i=0; i <[topStation.listeTypesDesCarburants count]; i++)
    {
        [newArrayTypeCarburant addObject: [topStation.listeTypesDesCarburants objectAtIndex:i]]; 
    }
    for(int i=0; i <[topStation.listeDesEnseignes count]; i++)
    {
        [newArrayNomStation addObject: [topStation.listeDesEnseignes objectAtIndex:i]]; 
    }
    self.pickerArrayTypeCarburant = newArrayTypeCarburant;
    self.pickerArrayNomStation = newArrayNomStation;

the first picker contains value from the array but when i try to click on the second picker, an exception was thrown :

2011-05-04 15:10:06.631[1065:207] -[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x6e389a0
2011-05-04 15:10:06.633[1065:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x6e389a0'

Upvotes: 0

Views: 1336

Answers (2)

Ripred
Ripred

Reputation: 157

You are using NSArray addObjectsFromArray: which will copy an entire array but you are doing it once for each container member (as well as not passing the correct type since it expects an NSArray ref and you're passing the individual elements). Instead try copying the arrays like this and remove the for-loops:

[newArrayTypeCarburant addObjectsFromArray: topStation.listeTypesDesCarburants];
[newArrayNomStation addObjectsFromArray: topStation.listeDesEnseignes];

If you still need to walk through them one at a time use the code you have but change addObjectsFromArray: to addObject::

for(int i=0; i <[topStation.listeTypesDesCarburants count]; i++)
{
    // perhaps some predicate here...
    [newArrayTypeCarburant addObject: [topStation.listeTypesDesCarburants objectAtIndex:i]]; 
}

Upvotes: 2

Dancreek
Dancreek

Reputation: 9544

you could just use arrayWithArray.

NSMutableArray *newArrayNomStation = [NSMutableArray arrayWithArray:topStation.listeDesEnseignes];

Upvotes: 1

Related Questions