Baub
Baub

Reputation: 5054

indexPath.row-1 is 4294967295

I have an indexPath.row that is 1 and logs 1(when using NSLog). If I call indexPath.row-1 (should return 0) it returns 4294967295.

I'm trying to return an objectAtIndex:indexPath.row-1 but that's when I get 4294967295.

Any ideas?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    Singleton *singleton = [Singleton sharedSingleton];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    if ([[prefs objectForKey:@"isYes"]boolValue] == 1 && randomMarker != 100)
    {
        //sets cell image
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,98,100)];
        imgView.image = [UIImage imageNamed:@"stackoverflow.png"];
        cell.imageView.image = imgView.image;

        //sets cell text
        cell.textLabel.text = @"Text";
        self.checkedInCount == 100;
    }
    else if ([[prefs objectForKey:@"isYes"]boolValue] == 1 && randomMarker == 100)
    {
        //gets cell and cleans up cell text
        NSLog(@"%@", indexPath.row);
        NSString *title = [[[singleton linkedList]objectAtIndex:(indexPath.row-1)]objectForKey:@"desc"];

Upvotes: 0

Views: 2589

Answers (2)

Umangshu Chouhan
Umangshu Chouhan

Reputation: 131

NSLog(@"%@", indexPath.row);

You shold use %d for integer as indexPath.row will return an integer

Use NSLog(@"%d", indexPath.row);

Upvotes: 0

PengOne
PengOne

Reputation: 48398

When you attempt to give an unsigned int (NSUInteger) a negative value, it often returns a very large positive value instead.

You are calling

NSString *tempDesc = [[[singleton linkedList]objectAtIndex:indexPath.row-1]objectForKey:@"desc"]; 

when indexPath.row has value 0, so the translation is:

NSString *tempDesc = [[[singleton linkedList]objectAtIndex:-1]objectForKey:@"desc"]; 

Since objectAtIndex: takes an unsigned integer as its parameter, -1 is converted to a garbage value of 4294967295.

To avoid this problem, don't subtract 1 from 0 by checking first that indexPath.row is positive.


Here's another problem:

NSLog(@"%@", indexPath.row);

This should instead read:

NSLog(@"%u", indexPath.row);

Upvotes: 10

Related Questions