隐式转换失去整数精度:' unsigned long'到' int' (导致应用程序崩溃)

时间:2015-05-28 18:19:05

标签: ios objective-c

我正在处理我在网上找到的教程并收到了问题:

  

隐式转换失去整数精度:' unsigned long'到' int'

我查看了其他一些帖子,并找不到帮助我的那个帖子。我是编程新手,我非常感谢任何有用的建议!

这是我遇到问题的部分:

- (void)handleTouchAtLocation:(CGPoint)touchLocation {
    if (!self.editable) return;

    int newRating = 0;
    for(int i = self.imageViews.count - 1; i >= 0; i--) {
        UIImageView *imageView = [self.imageViews objectAtIndex:i];        
        if (touchLocation.x > imageView.frame.origin.x) {
            newRating = i+1;
            break;
        }
    }

    self.rating = newRating;
}

这是我的.h文件的顶部:

#import "RWTRateView.h"

@implementation RWTRateView

@synthesize notSelectedImage = _notSelectedImage;
@synthesize halfSelectedImage = _halfSelectedImage;
@synthesize fullSelectedImage = _fullSelectedImage;
@synthesize rating = _rating;
@synthesize editable = _editable;
@synthesize imageViews = _imageViews;
@synthesize maxRating = _maxRating;
@synthesize midMargin = _midMargin;
@synthesize leftMargin = _leftMargin;
@synthesize minImageSize = _minImageSize;
@synthesize delegate = _delegate;

这些是我的财产陈述:

@property (strong, nonatomic) UIImage *notSelectedImage;
@property (strong, nonatomic) UIImage *halfSelectedImage;
@property (strong, nonatomic) UIImage *fullSelectedImage;
@property (assign, nonatomic) float rating;
@property (assign) BOOL editable;
@property (strong) NSMutableArray * imageViews;
@property (assign, nonatomic) int maxRating;
@property (assign) int midMargin;
@property (assign) int leftMargin;
@property (assign) CGSize minImageSize;
@property (assign) id <RWTRateViewDelegate> delegate;

1 个答案:

答案 0 :(得分:1)

正如你正确地说,这就是问题所在:

for (int i = self.imageViews.count - 1; i >= 0; i--) {

问题是self.imageViews.count 是一个int。它是一种NSUInteger,是一种非常不同的动物。只需改写:

for (NSUInteger i = self.imageViews.count - 1; i >= 0; i--) {

特别是,此转换可能会导致跨越位数障碍的问题。 NSUInteger根据我们是32位还是64位来改变其大小。但是int没有。这两者不兼容。