【问题标题】:Implicit conversion error to NSIndexPath到 NSIndexPath 的隐式转换错误
【发布时间】:2015-01-04 14:10:14
【问题描述】:

我有这个比较简单的辅助方法:

- (float)imageHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    float imageWidth = [[self.widthArray objectAtIndex:indexPath.row] floatValue];
    float ratio = screenWidth/imageWidth;
    float imageHeight = ratio * [[self.heightArray objectAtIndex:indexPath.row] floatValue];
    return imageHeight;
}

在另一种方法中调用时完全正常,但在此方法中:

- (UIImage *)imageWithImage:(UIImage *)image forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat newWidth = screenRect.size.width;
    CGFloat newHeight = [self imageHeightForRowAtIndexPath:indexPath.row];
    UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight ));
    [image drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

编译器说: Implicit conversion from 'NSInteger' to 'NSIndexPath' is disallowed

我不知道为什么以及如何解决这个问题。有什么想法吗?

【问题讨论】:

  • 令人惊讶的是它没有识别错误的行!
  • 但很可能是这一行:CGFloat newHeight = [self imageHeightForRowAtIndexPath:indexPath.row]; (Chuck .row)

标签: objective-c compiler-errors implicit-conversion


【解决方案1】:

你有两种方法:

  1. - (float)imageHeightForRowAtIndexPath:(NSIndexPath *)indexPath
  2. - (UIImage *)imageWithImage:(UIImage *)image forRowAtIndexPath:(NSIndexPath *)indexPath

每个都有一个NSIndexPath * 参数。

现在,在imageWithImage:forRowAtIndexPath: 中,您正在调用imageHeightForRowAtIndexPath:

[self imageHeightForRowAtIndexPath:indexPath.row]

在那条线上,您从indexPathNSInteger)中获取row,并尝试将其传递给imageHeightForRowAtIndexPath::,它期待NSIndexPath *

这是错误的原因,因为编译器知道需要 NSIndexPath * 并且它知道您提供的是 NSInteger

要解决,把代码改成:

CGFloat newHeight = [self imageHeightForRowAtIndexPath:indexPath];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多