我编写了一个函数来对四个字段执行计算。然后将计算结果输出到接收方字段。数学在概念上相当简单。接收方字段是最小整型数与下一个最小整型数的余数。例如,应用程序从用户输入中读取值2,5,6,8。接收域应等于(3)
我已经实现了NSSet来删除任何重复,而不是将集合重新堆叠到一个数组中,以便于阅读和值和自定义控件。
函数的数学部分似乎有点过了头。除了我所构建的方法之外,是否还有更优雅的方法来执行此操作?它可以工作,它只是丑陋,可能不是正确的路径。
任何帮助都是感激的。
// set of strings populated from a sibling View so i used a singleton to sync.
NSArray *myArray = [[NSArray alloc] initWithObjects:[StringLinker sharedManager].singcol1Row1String,
[StringLinker sharedManager].singcol1Row2String,
[StringLinker sharedManager].singcol1Row3String,
[StringLinker sharedManager].singcol1Row4String,nil];
NSSet *set = [NSSet setWithArray:myArray];
NSLog(@"mySet count: %d", [set count]);
NSMutableArray *newArray = [NSMutableArray arrayWithArray:[set allObjects]];
// define locals
int i;
int tempV;
id *objects;
int v1Col1=0;
int v2Col1=0;
int v3Col1=0;
int v4Col1=0;
// create helpers
NSUInteger count = [newArray count];
objects = malloc(sizeof(id) * count);
[newArray getObjects:objects];
// check the correct id and location
for (i = 0; i < [newArray count]; i++) {
tempV = [[newArray objectAtIndex:i]intValue];
NSLog(@"current objectAtIndex = %i id = %i",i,tempV);
}
NSLog(@"count of new Array = %d",count);
// assign our controlled items. and make string into a Value
if (count >= 1) { v1Col1 = [[newArray objectAtIndex:0]intValue];}
if (count >= 2) { v2Col1 = [[newArray objectAtIndex:1]intValue];}
if (count >= 3) { v3Col1 = [[newArray objectAtIndex:2]intValue];}
if (count >= 4) { v4Col1 = [[newArray objectAtIndex:3]intValue];}
// then do raw calculations
if ( count ==2) {
if (v1Col1 < v2Col1) { _master1PinsCol1 = v2Col1 - v1Col1;}
if (v2Col1 < v1Col1) { _master1PinsCol1 = v1Col1 - v2Col1;}
}
if ( count ==3) {
if ((v1Col1 < v2Col1) && (v2Col1 < v3Col1)) { _master1PinsCol1 = v2Col1 - v1Col1;}
if ((v1Col1 < v2Col1) && (v3Col1 > v2Col1)) { _master1PinsCol1 = v2Col1 - v1Col1;}
if ((v1Col1 < v2Col1) && (v3Col1 < v2Col1)) { _master1PinsCol1 = v3Col1 - v1Col1;}
if ((v2Col1 < v1Col1) && (v1Col1 < v3Col1)) { _master1PinsCol1 = v1Col1 - v2Col1;}
if ((v2Col1 < v3Col1) && (v3Col1 < v1Col1)) { _master1PinsCol1 = v3Col1 - v2Col1;}
}
if (count >=4){
if ((v1Col1 < v2Col1) && (v1Col1 < v3Col1) && (v1Col1 < v4Col1)) {
if ((v2Col1 < v3Col1) && (v2Col1 < v4Col1)) {_master1PinsCol1 = v2Col1-v1Col1;}
if ((v3Col1 < v2Col1) && (v3Col1 < v4Col1)) {_master1PinsCol1 = v3Col1-v1Col1;}
if ((v4Col1 < v2Col1) && (v4Col1 < v3Col1)) {_master1PinsCol1 = v4Col1-v1Col1;}
}
if ((v2Col1 < v1Col1) && (v2Col1 < v3Col1) && (v2Col1 < v4Col1)) {
if ((v1Col1 < v3Col1) && (v1Col1 < v4Col1)) {_master1PinsCol1 = v1Col1-v2Col1;}
if ((v3Col1 < v1Col1) && (v3Col1 < v4Col1)) {_master1PinsCol1 = v3Col1-v2Col1;}
if ((v4Col1 < v1Col1) && (v4Col1 < v3Col1)) {_master1PinsCol1 = v4Col1-v2Col1;}
}
if ((v3Col1 < v1Col1) && (v3Col1 < v2Col1) && (v3Col1 < v4Col1)) {
if ((v1Col1 < v2Col1) && (v1Col1 < v4Col1)) {_master1PinsCol1 = v1Col1-v3Col1;}
if ((v2Col1 < v1Col1) && (v2Col1 < v4Col1)) {_master1PinsCol1 = v2Col1-v3Col1;}
if ((v4Col1 < v1Col1) && (v4Col1 < v2Col1)) {_master1PinsCol1 = v4Col1-v3Col1;}
}
if ((v4Col1 < v1Col1) && (v4Col1 < v2Col1) && (v4Col1 < v3Col1)) {
if ((v1Col1 < v2Col1) && (v1Col1 < v3Col1)) {_master1PinsCol1 = v1Col1-v4Col1;}
if ((v2Col1 < v1Col1) && (v2Col1 < v3Col1)) {_master1PinsCol1 = v2Col1-v4Col1;}
if ((v3Col1 < v1Col1) && (v3Col1 < v2Col1)) {_master1PinsCol1 = v3Col1-v4Col1;}
}
}
NSLog(@"***********************************Col1 Master1 = %.0f",_master1PinsCol1);
您的代码在保存方法调用和对象方面有很大的潜力。此外,似乎您正在泄漏内存:您malloc
'd但从未free
'd,并且您的myArray
不是released
。我是这样做的:
// Get instance once, save three method calls.
StringLinker *linker = [StringLinker sharedManager];
// Initialize set with the objects. Convert them to NSNumbers, we need
// to have the numbers parsed later on anyway. Doing it now makes
// sorting easier.
NSSet *set = [NSSet setWithObjects:
[NSNumber numberWithInt:[linker.singcol1Row1String intValue]],
[NSNumber numberWithInt:[linker.singcol1Row2String intValue]],
[NSNumber numberWithInt:[linker.singcol1Row3String intValue]],
[NSNumber numberWithInt:[linker.singcol1Row4String intValue]],
nil
];
// Sanity check.
if ([set count] < 2) {
// Error handling goes here. For example, if there's only one
// element, can that be used as value for _master1PinsCol1 ?
return;
}
// Get mutable array of distinct objects...
NSMutableArray *array = [[set allObjects] mutableCopy];
// ... and sort it.
[array sortUsingSelector:@selector(compare:)];
// Calculate the value by subtracting the smaller number from the
// bigger number.
_master1PinsCol1 = [[array objectAtIndex:1] intValue]
- [[array objectAtIndex:0] intVale];
// Release the mutable copy.
[array release];