tags:

views:

29

answers:

1

hello i have this array that compares two arrays to see if it contains an object however after testing it with nslog

NSLog (@"1");
favoritesArray = [[NSMutableArray alloc] init];
NSLog (@"2");
//Add items
// favoritesArray = [[NSMutableArray alloc]init];

didContain = [[NSMutableArray alloc]init];
NSLog (@"3");

if ([favoritesArray containsObject:@"one"])
{
    [didContain addObject:@"one"];
    NSLog (@"4");
}

the 4th log doesn appear. does this mean my implementation is wrong of the if it contains an object or it means there is no file present like that?

EDIT here is where i add an array

-(IBAction) addToFavorites {

favoritesArray = [[NSMutableArray alloc] init];
[favoritesArray addObject:@"one"];
NSLog (@"hello");

how come this doesnt show up as the object @"one"

+1  A: 

The fourth log doesn't appear because the array doesn't contain the object @"one" -- the NSLog is inside the if statement testing for that case.

With the code as shown, that's correct -- the array will be empty and so it's not surprising there's no @"one".

Presumably the real code does something to put stuff in the array, but since we can't see that it's hard to say anything about it.

EDIT: Although the code you've added to the question for addToFavorites seems straightforward enough, it's still not clear how it relates to the original code. Where is it called from? What is the sequence of events?

One issue in particular is that you seem to be allocating a new array in both locations. If this is the case, then the reason you don't see @"one" appearing is that you are adding it in one array and looking for it in a different one. The fact that they both happen to be called favoritesArray is incidental and just muddies the water.

In fact, it is extremely likely that you shouldn't be allocating the array in either location, but should just be referencing an array that gets allocated only once somewhere else (eg, in your class's designated init method) and then kept as an ivar for the lifetime of your application.

walkytalky
i edited my question
Alx
@Alx see updated answer above.
walkytalky
Or at least for the lifetime of the instance of the class, and released when the instance is deallocated (i.e., in the class's `- (void) dealloc` method).
Peter Hosey
How would i do that i dobt know ..
Alx
@Alx I don't think I can teach you that here. I suggest going step by step through as many Objective-C and Cocoa tutorials as you can [find online](http://www.google.com/search?q=objective+c+tutorial). Try to understand what they're getting at rather than just cobbling together scraps of code.
walkytalky
Could u show me some tutorials on this? More specific ones to what im trying to do? Thanks
Alx
Alx: Getting tutorials and/or code snippets for specific applications or tasks will not help you as long as you don't have an understanding of the concepts at work. Read Apple's Objective-C Programming Language and Cocoa Fundamentals Guide documents, or buy a book. If you go the latter route, I recommend Scott Stevenson's “Cocoa and Objective-C: Up and Running”.
Peter Hosey