objective-c - 隐式转换为NSIndexPath的错误

我有这个相对简单的辅助方法:

- (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
我不知道为什么以及如何解决这个问题。任何想法?

最佳答案

您有2种方法:

  • - (float)imageHeightForRowAtIndexPath:(NSIndexPath *)indexPath
  • - (UIImage *)imageWithImage:(UIImage *)image forRowAtIndexPath:(NSIndexPath *)indexPath

  • 每个都有一个NSIndexPath *参数。

    现在,在imageWithImage:forRowAtIndexPath:中,您正在调用imageHeightForRowAtIndexPath::
    [self imageHeightForRowAtIndexPath:indexPath.row]
    

    然后在那一行上,您从row(一个indexPath)中获取NSInteger,并尝试将其传递给imageHeightForRowAtIndexPath::,后者需要一个NSIndexPath *

    这是导致错误的原因,因为编译器知道期望NSIndexPath *并且知道您正在提供NSInteger

    要解决,请将代码更改为:
    CGFloat newHeight = [self imageHeightForRowAtIndexPath:indexPath];
    

    https://stackoverflow.com/questions/27766339/

    相关文章:

    caching - 如何处理并发Web用户的重置CS-Script

    macos - AudioFileReadPackets错误-50

    compiler-errors - 如何解决MiniZinc错误: “model inconsist

    compilation - 如何在Windows上的SCons中导出具有备用扩展名的程序

    actionscript-3 - for(const i)而不是(var i)

    android - Android Delphi xe7中的错误

    c# - 如何从 C# 中的封闭构造类中获取基类对象

    compiler-errors - Latex中缺少\begin {document}

    actionscript-3 - 如果使用 Flash Builder 4.7 而不是 4.6,则基

    opencv - 使用 CUDA 支持构建 OpenCV transpose.cu 上的错误