0

I get this error, when compiling project for real phone.

format specifies type 'long' but the argument has type 'int'

On simulator all work fine.

If replace "%d" with "%ld" I get this:

format specifies type 'int' but the argument has type 'long'

In this code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    if (cell) {
        cell.textLabel.text = [NSString stringWithFormat:@"page %d", indexPath.row+1]; // <-
    }
    return cell;
}

How can I fix it?

rmaddy
  • 298,130
  • 40
  • 468
  • 517
user3819738
  • 209
  • 3
  • 9

2 Answers2

2

indexPath.row is an NSInteger. This will be 32-bits on a 32-bit compile or 64-bits on 64-bit compile. When I got this error in Xcode the message said to use %ld in the format string and cast the value to a long.

cell.textLabel.text = [NSString stringWithFormat:@"page %ld", (long)(indexPath.row+1]);

This fixed the problem for both 32-bit and 64-bit builds. Don't cast to int because on a 64-bit build this will narrow the 64-bit value to 32-bits.

If using NSUInteger use %lu as the format and cast to unsigned long.

This question has some other alternatives.

Community
  • 1
  • 1
Brian Walker
  • 8,108
  • 2
  • 30
  • 34
0

replace this line with this

        cell.textLabel.text = [NSString stringWithFormat:@"page %d",(int) indexPath.row+1]; // <-
BHASKAR
  • 1,191
  • 1
  • 8
  • 20