简体   繁体   中英

How to check if nsmutablearray contains integer value

Am getting the following dat from array through json,StatusArray is

(
    (
    5
),
    (
    4
),
    (
    3
),
    (
    5
),
    (
    1
),
    (
    1
)

)

Now i need to compare these values with integer values and adding it to the tableview cell label. my code is,

NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:data

                      options:kNilOptions
                      error:&error];
 NSLog(@"%@", json);
totalArray =[json objectForKey:@"Files"];
 NSLog(@"%@", totalArray);
fileIdArray=[[NSMutableArray alloc]init];
loanNumberArray=[[NSMutableArray alloc]init];
borrowerArray=[[NSMutableArray alloc]init];
adressArray=[[NSMutableArray alloc]init];
statusArray=[[NSMutableArray alloc]init];
ordersArray=[[NSMutableArray alloc]init];


for (int i=0; i<[[json objectForKey:@"Files"] count]; i++)
{
    [fileIdArray addObject:[[totalArray objectAtIndex:i]objectForKey:@"FileID"]];
    [loanNumberArray addObject:[[totalArray objectAtIndex:i]objectForKey:@"LoanDetails"]];
    [borrowerArray addObject:[[totalArray objectAtIndex:i]objectForKey:@"Borrower"]];
    [adressArray addObject:[[totalArray objectAtIndex:i]objectForKey:@"PropertyAddress"]];
    [ordersArray addObject:[[totalArray objectAtIndex:i]objectForKey:@"Orders"]];
}
statusArray = [ordersArray valueForKey:@"Status"];

if ([statusArray containsObject:@"1"]) 
{
    UILabel *statusLabel = (UILabel *)[cell viewWithTag:105];
    statusLabel.text=@"New";
    NSString *label=statusLabel.text;
    NSLog(@"%@",label);
}
else if([statusArray containsObject:[NSNumber numberWithInteger:2]])
{
    UILabel *statusLabel = (UILabel *)[cell viewWithTag:105];
    statusLabel.text=@"Pending";
    NSString *label=statusLabel.text;
    NSLog(@"%@",label);
}
else if([statusArray containsObject:@"4"])
{
    UILabel *statusLabel = (UILabel *)[cell viewWithTag:105];
    statusLabel.text=@"Accepted";
    NSString *label=statusLabel.text;
    NSLog(@"%@",label);
}
else if([statusArray containsObject:@"5"])
{
    UILabel *statusLabel = (UILabel *)[cell viewWithTag:105];
    statusLabel.text=@"Completed";
    NSString *label=statusLabel.text;
    NSLog(@"%@",label);
}

Am unable to find the label text and its not going to any condition.whats wrong with my code Please correct me.

Thanks

try this :

   for (int x=0; x<[StatusArray count]; x++) {

    NSMutableArray *array = [StatusArray objectAtIndex:x];

     NSString *string = [NSString stringWithFormat:@"%@",[array objectAtIndex:0]];

    if ([string isEqualToString:@"1"])
    {
        UILabel *statusLabel = (UILabel *)[cell viewWithTag:105];
        statusLabel.text=@"New";
        NSString *label=statusLabel.text;
        NSLog(@"%@",label);
    }
    else if([string isEqualToString:@"2"])
    {
        UILabel *statusLabel = (UILabel *)[cell viewWithTag:105];
        statusLabel.text=@"Pending";
        NSString *label=statusLabel.text;
        NSLog(@"%@",label);
    }
    else if([string isEqualToString:@"4"])
    {
        UILabel *statusLabel = (UILabel *)[cell viewWithTag:105];
        statusLabel.text=@"Accepted";
        NSString *label=statusLabel.text;
        NSLog(@"%@",label);
    }
    else if([string isEqualToString:@"5"])
    {
        UILabel *statusLabel = (UILabel *)[cell viewWithTag:105];
        statusLabel.text=@"Completed";
        NSString *label=statusLabel.text;
        NSLog(@"%@",label);
    }
}

Your statusArray is an array of arrays. And you are trying to compare array with string.

Try this one:

UILabel *statusLabel = (UILabel *)[cell viewWithTag:105];
if ([statusArray containsObject:@[@1]])
{
    statusLabel.text=@"New";
    NSString *label=statusLabel.text;
    NSLog(@"%@",label);
}
else if([statusArray containsObject:@[@2]])
{
    statusLabel.text=@"Pending";
    NSString *label=statusLabel.text;
    NSLog(@"%@",label);
}
else if([statusArray containsObject:@[@3])
{
    statusLabel.text=@"Accepted";
    NSString *label=statusLabel.text;
    NSLog(@"%@",label);
}
else if([statusArray containsObject:@[@4])
{
    statusLabel.text=@"Completed";
    NSString *label=statusLabel.text;
    NSLog(@"%@",label);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM