Search code examples
iosobjective-cnsstringnsarray

How to compair NString when it is converted from NSArray in Objective C?


I am new in iOS. And I am facing a problem in a code

array=[[NSMutableArray alloc] init];
        array =[responsedict valueForKey:@"Type"];
        NSLog(@"Type Array =%@",array);
 for (int i =0; i<[array count]; i++) {
    if (array.count>0)
    {
       typeString = [NSMutableString stringWithFormat:@"%@", [array objectAtIndex:i]];
        NSLog(@"Type String =%@",typeString);
    }
 }

I am getting the value in String as

Type String =1
Type String =2

Now I need to compair the String in cellForRowAtIndexPath

if ([typeString isEqual:@"1"]) {
            cell.IBTiconlbl.textColor = [UIColor blueColor];
        }
if ([typeString isEqual:@"2"]) {
            cell.IBTiconlbl.textColor = [UIColor redColor];
        }

But it convert the textcolour only in readColor not in blue colour. String contain both the value 1 and 2. I need to convert the text colour in blue when string value is 1 and in red when string value is 2. But it only perform the one operation.


Solution

  • No need to convert the whole array into NSString this will save the code optimization time a bit you can do this by checking the object using indexPath.row and take action accordingly see below code:

    if([[NSString stringWithFormat:@"%@",[array objectAtIndex:indexPath.row]] isEqualToString:@"1"]){
            cell.IBTiconlbl.textColor = [UIColor blueColor];
    }else if([[NSString stringWithFormat:@"%@",[array objectAtIndex:indexPath.row]] isEqualToString:@"2"]){
            cell.IBTiconlbl.textColor = [UIColor redColor];
    }else{
            // code for else part here
    }