开发者

copying object from NSMutableArray to another using IF statement

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 t开发者_如何学Che strings, so I couldn't get an equal string!


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];
    }
}


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜