我从核心数据中获取了一个数组,并尝试进行一些简单的计算,但是将无效操作的错误转换为二进制表达式
pop_cum[i]= (pop_ln_array[i-1] + pop_ln_array[i]);
//getting error at this point of "Invalid operations to binary expression('id' and 'id')
我知道我们必须将数组值的类型更改为int但是如何?
NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Input_Details" inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
// Set example predicate and sort orderings...
request.propertiesToFetch = @[ @"pop_Ln" ];
request.resultType = NSDictionaryResultType;
//if you change the sort order here, please change it to SDInputDetailsList also
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"sewer_No" ascending:YES];
[request setSortDescriptors:@[sortDescriptor]];
NSError *error;
NSArray *pop_ln_array = [moc executeFetchRequest:request error:&error];
int arrayCount=[pop_ln_array count];
//Calculating cumulative population, flow & peak factor
NSLog(@"arrayCount is %d",arrayCount);
if (pop_ln_array == nil)
{
NSLog(@"Cumilative population line array is nil");
}
else
{
NSArray *pop_cum[arrayCount];
for(int i=0;i<[pop_ln_array count];i++)
{
NSLog(@"Cumilative array at %d is %@",i,pop_ln_array[i]);
if (i==0)
{
pop_cum[i]=pop_ln_array[i] ;
}
else
{
pop_cum[i]= (pop_cum[i-1] + pop_ln_array[i]); //getting error at this point of "Invalid operations to binary expression('id' and 'id')
}
}
}
答案 0 :(得分:0)
使用如下
for(int i=0;i<[pop_ln_array count];i++){
NSLog(@"Cumilative array at %d is %@",i,pop_ln_array[i]);
if (i==0){
pop_cum[i]=pop_ln_array[i];
NSLog(@"pop_cum[%d] %@",i,pop_cum[i]);
}
else {
NSLog(@"Pop_cum %@",pop_cum[i-1]);
[pop_ln_array[i]intValue]);
NSInteger first=[pop_ln_array[i-1]intValue], second=[pop_ln_array[i]intValue];
NSInteger sum=first+second;
pop_cum[i]=[NSString stringWithFormat:@"%d",sum];
}
}
您正在添加两个id
。哪个不可以。
将其转换为integerValue或intValue或任何数字,然后添加。