gpt4 book ai didi

iphone - 比较两个数组的问题

转载 作者:行者123 更新时间:2023-11-28 23:13:34 24 4
gpt4 key购买 nike

我在 Objective C 中有一个循环,我比较了同一个数组的两个索引,但是比较不起作用,没有编译错误。变量 iMinor 始终为零且 arrayDistancias 不为空:

NSMutableArray *arrayDistancias = [NSMutableArray array];


NSNumber *lat1 = [NSNumber numberWithFloat:39.0025];
NSNumber *lat2 = [NSNumber numberWithFloat:40.2710];
NSNumber *lat3 = [NSNumber numberWithFloat:38.5316];
NSNumber *lat4 = [NSNumber numberWithFloat:27.4529];
NSNumber *lng1 = [NSNumber numberWithFloat:-1.5139];
NSNumber *lng2 = [NSNumber numberWithFloat:-3.4327];
NSNumber *lng3 = [NSNumber numberWithFloat:-7.0029];
NSNumber *lng4 = [NSNumber numberWithFloat:-15.3432];


//for (NSInteger i = 0; i < 40; i++)
[arrayDistancias addObject:[NSNumber numberWithInteger:[self calcularDistancia:latitudaqui :lat1 :longitudaqui :lng1]]];
[arrayDistancias addObject:[NSNumber numberWithInteger:[self calcularDistancia:latitudaqui :lat2 :longitudaqui :lng2]]];
[arrayDistancias addObject:[NSNumber numberWithInteger:[self calcularDistancia:latitudaqui :lat3 :longitudaqui :lng3]]];
[arrayDistancias addObject:[NSNumber numberWithInteger:[self calcularDistancia:latitudaqui :lat4 :longitudaqui :lng4]]];


int iMinor = 0;
for (int i = 0; i < [arrayDistancias count]; i++) {

if([arrayDistancias objectAtIndex: i] < [arrayDistancias objectAtIndex: iMinor]){
iMinor = i;
}


}

提前致谢

最佳答案

[arrayDistances objectAtIndex: i] 重新调整 NSNumber 对象,而不是整数值。为了获得整数值,请对返回的对象使用 intValue 方法。所以,你应该这样比较。

if ([[arrayDistances objectAtIndex: i] intValue] < 
[[arrayDistances objectAtIndex: iMinor] intValue]) {

关于iphone - 比较两个数组的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7358287/

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