NSIndexPath的隐式转换错误

时间:2015-01-04 14:10:15

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

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

- (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

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

1 个答案:

答案 0 :(得分:1)

您有两种方法:

  1. - (float)imageHeightForRowAtIndexPath:(NSIndexPath *)indexPath
  2. - (UIImage *)imageWithImage:(UIImage *)image forRowAtIndexPath:(NSIndexPath *)indexPath
  3. 每个参数都有一个NSIndexPath *

    现在,在imageWithImage:forRowAtIndexPath:您正在呼叫imageHeightForRowAtIndexPath:

    [self imageHeightForRowAtIndexPath:indexPath.row]
    

    并且在该行上,您正在从rowindexPath)获取NSInteger并尝试将其传递给imageHeightForRowAtIndexPath::,这需要NSIndexPath * NSIndexPath * 1}}。

    这是错误的原因,因为编译器知道期望NSInteger并且它知道您提供了CGFloat newHeight = [self imageHeightForRowAtIndexPath:indexPath];

    要解决此问题,请将代码更改为:

    {{1}}