1

OK, maybe I'm not seeing clear anymore and hope you can help.

I'm trying to select an Object from a NSMutableArray using:

if([car.seat isEqualToString:@"fancyOne"]){
    fancyThings = [[NSMUtableArray]init];
    [fancyThings addObjects: car];
}

Now I forgot to tell you I'm new at this Objective-C, so maybe I'm thinking the wrong way.

What I'm basically trying to do is to get an Object from one array by selecting a value of it's components.

This is the way to do it, I am however keep having trouble with my if-statement. If I leave out the IF-statement it does fill my other NSMutableArray with the exact same object (thisCar) but if I put in the IF-statement it doesn't pick up that the string is the same in thisCar.seat.

I next example it puts everything in the normalThings but there are some aCar.seats which contain the string FANCYONE. I checked the XML file on spaces and that sort of things but everything is in order as far as I can see.

Shall I build it using NSScanner instead of IsEqualToString?

- (void)viewDidLoad {  
    appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];  
    appDelegate.fancyThings = [[NSMutableArray alloc]init];  
    for (CARS *aCar in appDelegate.someCars) {  
        if ([aCar.seats isEqualToString:@"FANCYONE"]){  
            [appDelegate.fancyThings addObject:aCar];  
        }  
        else {  
            [appDelegate.normalThings addObject:aCar];  
        }  
    }  
    self.title = @"Cars";  
    super viewDidLoad];  
}   

EDIT:

My BAD!! The code supplied was in fact in order! There was a mistake in my XMLParser, which added blank lines to the strings, so I couldn't get an equal string!

sth
  • 222,467
  • 53
  • 283
  • 367
Eddy
  • 11
  • 2
  • 5
    It's not at all clear what your question is; if you want an answer, you should clearly state what you are having trouble with. – smorgan Jan 17 '11 at 22:23
  • I'll try. I'm parsing an XML where all items are loaded in an NSMutableArray like an object car which contains seperate values car.seats car.date and car.color for example. I would like to make a selection on date (car.date) if a certain date is met it should copy the entire car object to another NSMutableArray. I hope this explains it better. – Eddy Jan 17 '11 at 23:03

3 Answers3

1

Hopefully this will give you some guidance:

//init new array
NSMutableArray *fancyThings = [[NSMutableArray alloc] init];

//walk your array
for (SomeCarObject *thisCar in arrayOfCars) {
    //is thisCar a qualifying object
    if ([thisCar.seat isEqualToString:@"fancyOne"]) {
        //yes, add thisCar object
        [fancyThings addObject:thisCar];
    }
}
dredful
  • 4,378
  • 2
  • 35
  • 54
0

You'll want to create that NSMutableArray outside of the for loop (assuming you're iterating through a collection). Then you can add to that NSMutableArray like you did.

Hope this helps!

BTW, you should edit your question with the comment you made to elaborate on it..

donkim
  • 13,119
  • 3
  • 42
  • 47
0

It's depends from volume of objects, which u deal with. If there is 1000 objects or less, this method looks good. But if there is more objects, u have risk to freeze u application and have a big memory leaks. Also if u will need concurrency code later, u have to keep in u mind some other solutions. U can using not just a string objects in u array, u can try to fill u array after application startup in objects, which response if string is same or not. Or using nsdictionary with appropriate keys. Please read my post multithread search design

Community
  • 1
  • 1
Alex
  • 393
  • 6
  • 21