【问题标题】:Incompatible integer to pointer conversion assigning to 'NSString *' from 'NSInteger' (aka 'int');从'NSInteger'(又名'int')分配给'NSString *'的不兼容整数到指针转换;
【发布时间】:2012-06-01 10:03:56
【问题描述】:

在表格单元格视图中显示 xml 解析数据时出现此语义问题。应用程序运行成功,但是当我选择任何行时,应用程序会在当时应用程序崩溃时进入详细视图。请帮忙!!!

这行第三种情况出现语义问题

cell.textLabel.text = aBook.bookID;

应用代码:

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

  static NSString *CellIdentifier = @"Cell";

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

  switch (indexPath.section) {
    case 0:
      cell.textLabel.text = aBook.name;
      break;
    case 1:
      cell.textLabel.text = aBook.author;
      break;
    case 2:
      cell.textLabel.text = aBook.price;
      break;
    case 3:            
      cell.textLabel.text = aBook.bookID;
      break;
  }

  return cell;
}

【问题讨论】:

    标签: objective-c


    【解决方案1】:

    您的 aBook.bookID 变量可能是 NSInteger,要将其转换为 NSString,请使用:

    case 3:            
        cell.textLabel.text = [NSString stringWithFormat:@"%i", aBook.bookID];
        break;
    

    【讨论】:

      【解决方案2】:

      您的案例 3 将取决于您的 bookID

      对于整数

      cell.textLabel.text = [NSString stringWithFormat:@"%d",aBook.bookID];
      

      对于 NSInteger

       cell.textLabel.text = [NSString stringWithFormat:@"%i",aBook.bookID];
      

      因为您正在为字符串提供 int 值

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-03
        相关资源
        最近更新 更多