-2
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.title = category.categoryName;

    if (self.title == @"Haugesund Motiver" && !motivesLoaded) {
        MotivesDataModel *motiv = [[MotivesDataModel alloc] init];
        motiv.motiveName = @"ksaldkaldsk";
        [category.motives addObject:motiv];
        motivesLoaded = YES;
    }    
}

Why is this not working?

I have to viewcontroller, one with a TableView each.

The 1.st view controller shows categories object, which contains a name plus another array called motives.

The 2.nd view controller is supposed to show the categories.motives array.

category object is passed along with the segue, and works fine.

Here i am trying to populate the nsmutablearray if it hasent been populated before.

But the if statement doesnt work. Everytime the view loads it add the object to the motives array, even if the motivesLoaded bool is changed to YES

N_A
  • 19,799
  • 4
  • 52
  • 98
Espen Birk
  • 436
  • 1
  • 5
  • 16
  • possible duplicate of [Why is this code not recognising the NSString as being equal?](http://stackoverflow.com/q/8625936/), [Why is my if statement not working?](http://stackoverflow.com/q/484709/), [Comparing strings in Cocoa](http://stackoverflow.com/q/881335/), [Why does a string not equal what is stored?](http://stackoverflow.com/q/11348809/), [Text comparison won't work](http://stackoverflow.com/q/12483491/), [Comparison of two strings fails](http://stackoverflow.com/q/9322457/), [Understanding NSString comparison in Objective-C](http://stackoverflow.com/q/3703554/) – jscs Sep 20 '12 at 17:38

2 Answers2

1

you shouldnt check equality of strings with ==, this just checks that they are the same object

you should use

[self.title isEqualToString:@"Haugesund Motiver"]
wattson12
  • 11,176
  • 2
  • 32
  • 34
  • Thanks. But the main problem still is there. it creates e new object and adds it to the motives array everytime, even if the If statement only should "work" if motivesLoaded is false. – Espen Birk Sep 20 '12 at 14:58
  • 1
    then the if statement is ok, try logging motivesLoaded just before the if statement – wattson12 Sep 20 '12 at 15:03
  • @EspenBirk Keep in mind `!BOOL` means the BOOL is NO, or nil. – Mick MacCallum Sep 20 '12 at 15:13
  • Yes, i know. And if i remember right BOOL is no as default. So if motiveLoaded = NO then it should make an object and put it in the array. After it has created the object it should set motiveLoaded = YES. Stupid question maybe, but how do i NSLog a boolean? – Espen Birk Sep 20 '12 at 15:18
  • Correct, but just putting this out there, I'm partial to :`NSLog(myBOOL ? @"Yes" : @"No");` – Mick MacCallum Sep 20 '12 at 15:21
  • NSLog show that the bool value is NO before the if statement, and YES after. But when i go back in the navigationController and then load the tableview again the bool is back to 0; How can i set a bool value that retains the value after the viewunloads – Espen Birk Sep 20 '12 at 15:25
  • @EspenBirk This behavior causes the controller your table is in to be deallocated, you will need to find a way to more persistently save your bools value. NSUserDefaults should do the trick. – Mick MacCallum Sep 20 '12 at 15:27
  • @NSPostWhenIdle thanks, will look into NSUserDefaults – Espen Birk Sep 20 '12 at 15:38
  • Settings the NSUserdefaults value instead of a instance variable did the trick :D Thanks alot – Espen Birk Sep 20 '12 at 16:25
0
self.title == @"Haugesund Motiver"

checks for equality of the Pointers, so if self.title has the same name, but another instance of the String @"Haugesund Motiver", the check will fail, so you should use the [NSString isEqualToString:] method

MeXx
  • 3,357
  • 24
  • 39