0

我有一个从核心数据中获取的数组并尝试进行一些简单的计算,但是将无效操作错误转换为二进制表达式

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')
        }

    }
}
4

1 回答 1

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

        }
}

您正在添加两个ids。这是不可能的。

将其转换为 integerValue 或 intValue 或任何数字,然后添加。

于 2013-01-12T19:12:32.360 回答