2017-05-25 144 views
0

我有一些API调用,它返回一些值,并将其存储阵列,同时在接收我可能会重复,从我估计不是服务器值追加时,它已经在阵列和插入新值,如何追加字符串在数组中的增量为了

这里我的样本代码:

-(void)getIdValue:(NSString*)getIdVal { 

getTagIdList = [[NSMutableArray alloc]init]; 

BOOL isTheObjectThere = [getTagIdList containsObject: getIdVal]; 

NSLog(isTheObjectThere ? @"Yes" : @"No"); 

if (isTheObjectThere == NO){ 
    [getTagIdList addObject:getIdVal]; 
    NSLog(@"getTagIdList -->%@",getTagIdList); 

} if (isTheObjectThere == YES){ 
    return; 
} 

} 

我希望所有的新数据应在getTagIdList存储,但我只得到了一个记录被存储

+0

你需要检查怎样用这种方法:getIdValue获取调用多少次,它被称为。 – coder

+0

在这段代码中,数组总是空的,所以'isTheObjectThere'将始终为假,数组总是以1的值结束。如果你想让所有的值都在同一个数组中,那么你需要使用一个不是为每个'getIdVal'创建的数组。 – beyerss

回答

2

你初始化的方法中的阵列这是错误的。你不应该开始在您尝试存储数据的阵列。(这将删除存储在以前的调用所有的旧数据)

+0

我想你在这里得到了你的答案。 – coder

0
NSInteger count=[ArrayData count]; 

for (int i=0; i<count; i++) { 
NSString *StringValue= ArrayData[i]; 

BOOL contains = [getTagIdList containsObject: StringValue]; 
    if (contains==YES) { 
     NSLog(@"fail"); 
    } 
    else{ 
    [getTagIdList addObject: StringValue]; 
} 
    } 

这里的“ArrayData”是一个JSON性反应。 getTagIdList正在保存字符串值。试试吧,我认为这是工作。

0

从jegadeesh回答我更新代码

getTagIdList = [[NSMutableArray alloc]init]; 

-(void)getIdValue:(NSString*)getIdVal { 


BOOL isTheObjectThere = [getTagIdList containsObject: getIdVal]; 

NSLog(isTheObjectThere ? @"Yes" : @"No"); 

if (isTheObjectThere == NO){ 
    [getTagIdList addObject:getIdVal]; 
    NSLog(@"getTagIdList -->%@",getTagIdList); 

} if (isTheObjectThere == YES){ 
    return; 
} 

}