I have a simple looking piece of code that has me completely flummoxed.
我有一个简单的代码片段,让我完全陷入困境。
NSInteger ymax;
NSInteger ymin;
NSInteger numberIndex1;
NSInteger numberIndex2;
for (NSNumber *theNumber in array2)
{
if ([theNumber integerValue] > ymax) {
ymax = [theNumber integerValue];
numberIndex1 = [array2 indexOfObject:theNumber];
}
}
for (NSNumber *theNumber in array2)
{
if ([theNumber integerValue] < ymin) {
ymin = [theNumber integerValue];
numberIndex2 = [array2 indexOfObject:theNumber];
}
}
NSLog(@"Highest number: %d at index: %d", ymax, numberIndex1);
NSLog(@"Lowest number: %d at index: %d", ymin, numberIndex2);
The NSLog is outputted as:
NSLog输出为:
Highest number: 129171656 at index: -1073752392 (Huh??)
最高人数:129171656,指数:-1073752392(嗯?)
Lowest number: 57 at index: 5 (Correct)
最低数字:57指数:5(正确)
How do you explain this odd behaviour? Both the functions look the same. One is working and one isn't? I've played around a lot with this, but I still can't put my finger on it. Any help would be appreciated/
你怎么解释这个奇怪的行为?两个功能看起来都一样。一个是工作,一个不是?我用这个玩过很多次,但我还是不能把手指放在上面。任何帮助,将不胜感激/
5 个解决方案
#1
1
If I am not wrong you are considering the default value of NSInteger is 0, No, it isn't guaranteed to be zero, since it's a local automatic variable. Without initialization, its value is indeterminate.
如果我没有错,你正在考虑NSInteger的默认值是0,不,它不能保证为零,因为它是一个本地自动变量。没有初始化,它的值是不确定的。
so you need to set default values for your var, start with ymax = -1;
所以你需要为var设置默认值,从ymax = -1开始;
#2
2
You can get maximum and minimum number as below code. It may help you
您可以获得以下代码的最大和最小数量。它可能会帮助你
NSNumber * max = [array2 valueForKeyPath:@"@max.intValue"];
NSNumber * min = [array2 valueForKeyPath:@"@min.intValue"];
NSUInteger numberIndex1 = [array indexOfObject:min];
NSUInteger numberIndex2 = [array indexOfObject:max];
NSLog(@"Max Value = %d and index = %d",[max intValue],numberIndex1);
NSLog(@"Min Value = %d and index = %d",[min intValue],numberIndex2);
#3
1
Please initialize NSInteger ymax = 0; NSInteger ymin = 0 ; NSInteger numberIndex1 = 0; NSInteger numberIndex2 = 0; It will fix your issue. Otherwise it is checking with a garbage value and giving wrong result.
请初始化NSInteger ymax = 0; NSInteger ymin = 0; NSInteger numberIndex1 = 0; NSInteger numberIndex2 = 0;它将解决您的问题。否则,它将检查垃圾值并给出错误的结果。
#4
0
enjoy with my answer.......happy coding
享受我的答案.......快乐的编码
NSArray *arr1=[[NSArray alloc]initWithObjects:@"0.987",@"0.951",@"0.881",@"0.784",@"0.662",@"0.522",@"0.381",@"-0.265",@"-0.197", @"0.189",@"-0.233",@"0.310",@"0.402",@"0.402",@"0.988",@"0.633",@"0.661",@"0.656",@"0.617",@"0.634",@"0.690",@"0.767",@"0.836",nil];
NSArray * arr1 = [[NSArray alloc] initWithObjects:@“0.987”,@“0.951”,@“0.881”,@“0.784”,@“0.662”,@“0.522”,@“0.381”,@“ - 0.265 “,@” - 0.197“,@”0.189“,@” - 0.233“,@”0.310“,@”0.402“,@”0.402“,@”0.988“,@”0.633“,@”0.661“,@ “0.656”,@ “0.617”,@ “0.634”,@ “0.690”,@ “0.767”,@ “0.836”,零]。
NSNumber * max = [arr1 valueForKeyPath:@"@max.floatValue"];
NSString *str=[NSString stringWithFormat:@"%@",max];
NSInteger path=[arr1 indexOfObject:str];
NSIndexPath *indepath=[NSIndexPath indexPathForItem:path inSection:0];
NSNumber * min = [arr1 valueForKeyPath:@"@min.floatValue"];
NSString *str1=[NSString stringWithFormat:@"%@",min];
NSInteger path1=[arr1 indexOfObject:str1];
NSIndexPath *indepath1=[NSIndexPath indexPathForItem:path1 inSection:0];
NSLog(@"Max Value = %f and index = %ld",[max floatValue],(long)indepath.row);
NSLog(@"Min Value = %f and index = %ld",[min floatValue],(long)indepath1.row);
#5
-3
A more legitimate solution would be:
更合理的解决方案是:
NSArray *originalArray = @[[NSNumber numberWithInt:91],
[NSNumber numberWithInt:12],
[NSNumber numberWithInt:99123],
[NSNumber numberWithInt:9],
[NSNumber numberWithInt:43234]];
NSArray *sortedArray = [originalArray sortedArrayUsingSelector:@selector(compare:)];
NSNumber *minNumber = [sortedArray objectAtIndex:0];
NSNumber *maxNumber = [sortedArray lastObject];
NSInteger minIndex = [originalArray indexOfObject:minNumber];
NSInteger maxIndex = [originalArray indexOfObject:maxNumber];
#1
1
If I am not wrong you are considering the default value of NSInteger is 0, No, it isn't guaranteed to be zero, since it's a local automatic variable. Without initialization, its value is indeterminate.
如果我没有错,你正在考虑NSInteger的默认值是0,不,它不能保证为零,因为它是一个本地自动变量。没有初始化,它的值是不确定的。
so you need to set default values for your var, start with ymax = -1;
所以你需要为var设置默认值,从ymax = -1开始;
#2
2
You can get maximum and minimum number as below code. It may help you
您可以获得以下代码的最大和最小数量。它可能会帮助你
NSNumber * max = [array2 valueForKeyPath:@"@max.intValue"];
NSNumber * min = [array2 valueForKeyPath:@"@min.intValue"];
NSUInteger numberIndex1 = [array indexOfObject:min];
NSUInteger numberIndex2 = [array indexOfObject:max];
NSLog(@"Max Value = %d and index = %d",[max intValue],numberIndex1);
NSLog(@"Min Value = %d and index = %d",[min intValue],numberIndex2);
#3
1
Please initialize NSInteger ymax = 0; NSInteger ymin = 0 ; NSInteger numberIndex1 = 0; NSInteger numberIndex2 = 0; It will fix your issue. Otherwise it is checking with a garbage value and giving wrong result.
请初始化NSInteger ymax = 0; NSInteger ymin = 0; NSInteger numberIndex1 = 0; NSInteger numberIndex2 = 0;它将解决您的问题。否则,它将检查垃圾值并给出错误的结果。
#4
0
enjoy with my answer.......happy coding
享受我的答案.......快乐的编码
NSArray *arr1=[[NSArray alloc]initWithObjects:@"0.987",@"0.951",@"0.881",@"0.784",@"0.662",@"0.522",@"0.381",@"-0.265",@"-0.197", @"0.189",@"-0.233",@"0.310",@"0.402",@"0.402",@"0.988",@"0.633",@"0.661",@"0.656",@"0.617",@"0.634",@"0.690",@"0.767",@"0.836",nil];
NSArray * arr1 = [[NSArray alloc] initWithObjects:@“0.987”,@“0.951”,@“0.881”,@“0.784”,@“0.662”,@“0.522”,@“0.381”,@“ - 0.265 “,@” - 0.197“,@”0.189“,@” - 0.233“,@”0.310“,@”0.402“,@”0.402“,@”0.988“,@”0.633“,@”0.661“,@ “0.656”,@ “0.617”,@ “0.634”,@ “0.690”,@ “0.767”,@ “0.836”,零]。
NSNumber * max = [arr1 valueForKeyPath:@"@max.floatValue"];
NSString *str=[NSString stringWithFormat:@"%@",max];
NSInteger path=[arr1 indexOfObject:str];
NSIndexPath *indepath=[NSIndexPath indexPathForItem:path inSection:0];
NSNumber * min = [arr1 valueForKeyPath:@"@min.floatValue"];
NSString *str1=[NSString stringWithFormat:@"%@",min];
NSInteger path1=[arr1 indexOfObject:str1];
NSIndexPath *indepath1=[NSIndexPath indexPathForItem:path1 inSection:0];
NSLog(@"Max Value = %f and index = %ld",[max floatValue],(long)indepath.row);
NSLog(@"Min Value = %f and index = %ld",[min floatValue],(long)indepath1.row);
#5
-3
A more legitimate solution would be:
更合理的解决方案是:
NSArray *originalArray = @[[NSNumber numberWithInt:91],
[NSNumber numberWithInt:12],
[NSNumber numberWithInt:99123],
[NSNumber numberWithInt:9],
[NSNumber numberWithInt:43234]];
NSArray *sortedArray = [originalArray sortedArrayUsingSelector:@selector(compare:)];
NSNumber *minNumber = [sortedArray objectAtIndex:0];
NSNumber *maxNumber = [sortedArray lastObject];
NSInteger minIndex = [originalArray indexOfObject:minNumber];
NSInteger maxIndex = [originalArray indexOfObject:maxNumber];