如何从此解析结果中单独获取日期和标题及说明

时间:2017-04-15 00:46:56

标签: objective-c arrays swift nsstring

如何从此解析结果中单独获取日期和标题及说明:

title = "Inter-views 29/03/2017 random description here (its in arabic);
title = " \U0627\U062e\U0628\U0627\U0631 \U0627\U0644\U0635\U0628\U0627\U062d 14/04/2017";

这是从相同的api解析但不同(didSelectRowAtIndexPath)。 我目前正在使用此代码但是因为我通知解析是不同的。所以我不能使用静态逻辑。

代码:

    NSString *myString = [item objectForKey:@"title"];

    NSMutableArray *myArray = [[NSMutableArray alloc] 
    initWithArray:[myString componentsSeparatedByCharactersInSet:
    [NSCharacterSet characterSetWithCharactersInString:@" "]]];

        NSString *Category = @"";
        if(myArray.count>=1){
            Category = [myArray objectAtIndex:0];
            [myArray removeObjectAtIndex:0];
        }
        NSString *Date = @"";
        if(myArray.count>=1) {
            Date = [myArray objectAtIndex:0];
            [myArray removeObjectAtIndex:0];
        }
        cell.dateLabel.text = [[NSString alloc] initWithFormat:@"%@", [Utils dateTransform:[Date stringByReplacingOccurrencesOfString:@" " withString:@""] FromFormat:@"dd-MM-yyyy" ToFormat:@"dd-MMMM-yyyy"]];
        NSString *Title = @"";
        for (NSString *word in myArray) {
            Title = [Title stringByAppendingString:word];
            Title = [Title stringByAppendingString:@" "];
        }
        cell.titleAndDescLabel.text =[NSString stringWithFormat:@"%@ %@", Category,Title];

1 个答案:

答案 0 :(得分:0)

您必须使用for循环才能获得价值

for (NSInteger i=0; i<[[jsonObject objectForKey:@"yourMainKey"]count]; i++)
             {

NSString *strname =[[[jsonObject objectForKey:@"yourMainKey"]objectAtIndex:i]objectForKey:@"date"];

                 NSLog(@"strname==%@, strname);


NSString *str title =[[[jsonObject objectForKey:@"yourMainKey"]objectAtIndex:i]objectForKey:@"title"];

                 NSLog(@"strtitle ==%@, strtitle);

NSString *str description =[[[jsonObject objectForKey:@"yourMainKey"]objectAtIndex:i]objectForKey:@"description"];

                 NSLog(@"description ==%@, description);



             }