gpt4 book ai didi

ios - 隐式转换丢失整数精度 : 'unsigned long' to 'int' (causes app to crash)

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:15:16 25 4
gpt4 key购买 nike

我正在学习网上找到的教程并收到问题:

Implicit conversion loses integer precision: 'unsigned long' to '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)声明:

@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;

最佳答案

正如您所说,这就是问题所在:

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 没有。两者不兼容。

关于ios - 隐式转换丢失整数精度 : 'unsigned long' to 'int' (causes app to crash),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30514219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com