1

I essentially want to give each instance of a class a unique id.

So, I created a static integer. I increment it each time a new object is created and then assign the value of the static variable to an ivar. But clearly I don't understand something because, let's say I create three objects, "thisPageNumber" (which is the instance variable) is always 3 no matter which object I reference.

More information:

This class creates a number of "Page" objects. I'd like each page to know it's page number so that it can display the correct page art as well as perform a number of other various actions.

.h partial code:

@interface Page : UIViewController
{
    NSNumber            *thisPageNumber;
    UIImageView         *thisPageView;
    UIImageView         *nextPageView;
    UIImageView         *prevPageView;  
    UIImageView         *pageArt;
}

.m partial code:

@implementation Page

static int pageCount = 0;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        pageCount++;
        thisPageNumber = pageCount;
    }
    return self;
}

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

    CGRect defaultFrame = CGRectMake(0.0, 0.0, 1024.0, 768.0);

    if (thisPageView == nil) {
        thisPageView = [[UIImageView alloc] 
                        initWithImage:[UIImage 
                                       imageNamed:[NSString stringWithFormat:@"Page%i.png", [thisPageNumber intValue]]]];
        thisPageView.frame = defaultFrame;
        [self.view addSubview:thisPageView];
    }

    if (nextPageView == nil && [thisPageNumber intValue] < BOOK_PAGE_COUNT) {
        nextPageView = [[UIImageView alloc] 
                        initWithImage:[UIImage 
                                       imageNamed:[NSString stringWithFormat:@"Page%i.png", [thisPageNumber intValue]+1]]];
        nextPageView.frame = defaultFrame;
        [self.view addSubview:nextPageView];
    }

    if (prevPageView == nil && [thisPageNumber intValue] > 1) {
        prevPageView = [[UIImageView alloc] 
                        initWithImage:[UIImage 
                                       imageNamed:[NSString stringWithFormat:@"Page%i.png", [thisPageNumber intValue]-1]]];
        prevPageView.frame = defaultFrame;
        [self.view addSubview:prevPageView];
    }    
}
3
  • 2
    What you are doing looks ok, can you show the code wehre you are accessing? Commented Jul 12, 2010 at 23:53
  • Also the interface of the class Commented Jul 13, 2010 at 15:39
  • I've added some additional source code. Commented Jul 13, 2010 at 20:21

2 Answers 2

2

I'm not sure why the compiler didn't complain, but part of your problem is here:

thisPageNumber = pageCount;

NSNumber is an object. To set it to the current pageCount value, use

thisPageNumber = [[NSNumber alloc] initWithInt:pageCount];
Sign up to request clarification or add additional context in comments.

1 Comment

OMG, duh! Thanks, that works. "thisPageNumber" was actually an int when I first posted this topic, then by the time I added more of the code I had changed it to an NSNumber to fit in with my work around. Regardless, everything seems to be working great now. Thanks a lot!
0

Why don't you just use self as the unique ID? It's unique.

3 Comments

It's unique at a given instance of time in the program. The value of self can be reused after an object is dealloced which could be a problem for certain uniqueness requirements.
Also knowing the order of creation from the id can e.g. be helpful when evaluating logs.
Ah, that makes sense. We need to know what kind of uniqueness RyJ needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.