1

I have an NSNotification observer in class A named Test. The observer calls a method, checker:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checker:) name:@"Test" object:nil];

Then I have two posts in classes B and C, both to the observer named Test

[[NSNotificationCenter defaultCenter] postNotificationName:@"Test" object:self];

I'd like to be able to know which post is being sent to the observer and increment a counter to be usable in the check method, something like:

-(void)check {
    if ([classB count] <= [classC count]) {
        NSLog(@"boom");
    }
}

I've seen suggestions to use the userinfo to do so but im not quite sure how; is the counter object instantiated in class B/C and passed as an int or dictionary etc

Any help greatly appreciated Thanks

benzado
  • 82,288
  • 22
  • 110
  • 138
Elmo
  • 407
  • 4
  • 15

3 Answers3

2

Well you need to have your counter in class A. Than you can do this in checker: function

- (void)checker:(NSNotification *)notification
{
    if ([notification.object isKindOfClass:[BClass class]]) {
        bCounter++;
    }
    else if ([notification.object isKindOfClass:[CClass class]]) {
        cCounter++;
    }

    if (bCounter < cCounter) {
        NSLog(@"boom");
    }
}

Let me know if it works for you.

Mihai Fratu
  • 7,579
  • 2
  • 37
  • 63
  • I think this is what I was looking for, i'll let you know how it turns out. Thanks! – Elmo Aug 02 '11 at 17:45
  • Im gonna accept this cos its pretty much what im looking for :) Theres just one thing thats annoying me and im not sure if its just me having a stupid day, but when I declare bCounter and cCounter globally in ClassA, they seem to give me outrageous numbers, rather than starting at 0. Also do you have any suggestions on how to retain the counter counts? They return to 0 or whatever when the app is restarted. As I said im having a stupid day :) – Elmo Aug 02 '11 at 19:24
  • @Elmo: In C, if you declare a variable and don't assign it a value, it will just have whatever value happened to be in memory at the time. Unlike, say, Java, C doesn't clean up for you. (Except for object instance variables, they are cleared to zero when the instance is allocated.) Try `static int bCounter = 0;` for your global variable. – benzado Aug 05 '11 at 04:39
1

For how to pass userInfo dictionary, you can visit How to pass userInfo in NSNotification?

What you can do is, you can have two class level variables in the class where the check method is defined and then depending upon the identifier you send with notification object wrapped in userInfo dictionary, you can increment the values of countClassB and countClassC.

Community
  • 1
  • 1
indiantroy
  • 1,503
  • 1
  • 15
  • 25
0

You could also implement an increment method in class b and c respectively. Then in your test method just call the increment method i.e. [notification.object increment]. That way you don't have to care about the actual classes of the objects. To get the actual counter of b or c you can use properties (i.e. in class a you could ask for b.counter or c.counter).

maitee
  • 1