【问题标题】:Implicit conversion loses integer precision: TheElements - Apple sample code隐式转换失去整数精度:TheElements - Apple 示例代码
【发布时间】:2014-06-20 11:26:55
【问题描述】:

我最近更新了我的 Xcode,并收到此警告。有人可以帮我解决它。隐式转换丢失整数精度:'NSUInteger'(又名'unsigned long')到'int'

- (UIImage *)reflectedImageRepresentationWithHeight:(NSUInteger)height {

CGContextRef mainViewContentContext;
CGColorSpaceRef colorSpace;

colorSpace = CGColorSpaceCreateDeviceRGB();

// create a bitmap graphics context the size of the image
mainViewContentContext = CGBitmapContextCreate (NULL, self.bounds.size.width,height, 8,0, colorSpace, kCGImageAlphaPremultipliedLast);

// create a 2 bit CGImage containing a gradient that will be used for masking the 
// main view content to create the 'fade' of the reflection.  The CGImageCreateWithMask
// function will stretch the bitmap image as required, so we can create a 1 pixel wide gradient
    // WARING IS CAUSED BY LINE BELOW - height
CGImageRef gradientMaskImage = AEViewCreateGradientImage(1, height); // Implicit conversion loses integer precision: 'NSUInteger' (aka 'unsigned long') to 'int'

【问题讨论】:

  • 之前添加 (int) 并没有解决问题。仍然收到相同的警告...
  • 消息的含义是什么——将 NSUInteger 传递给期望 int 的方法(理论上)会失去精度。 AEViewCreateGradientImage 需要两个 int 值。

标签: ios objective-c xcode5


【解决方案1】:

我不知道函数 AEViewCreateGradientImage 但我敢打赌第二个参数采用 int 而不是 NSUInteger。 NSUInteger 的类型定义为无符号长整数,这就是您收到警告的原因。传递一个 int 而不是 NSUInteger,你的警告就会消失。您可能可以将 into 传递给您的方法,而不是 NSUInteger。

编辑

我查看了 Apple 的示例代码,我将扩展您所看到的内容。基本上这是因为在编写示例代码时,NSUInteger 很可能被定义为无符号整数,因此他们没有收到警告。 XCode 5.1 已经向前发展,现在它的类型定义为无符号长整数。我不想在这里重新发明轮子,并且有很多关于这个的堆栈溢出帖子,比如这个When to use NSInteger vs. int

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多