1

我正在获取如下数据,我想在names= app,app1,app2不循环的情况下获取数据。我怎样才能在不循环的情况下实现这一目标?

(
        {
        date = "12/12/12";
        name = app;
    },
        {
        date = "11/02/12";
        name = app1;
    },
        {
        date = "14/05/12";
        name = app2;
    }
)
4

2 回答 2

6

是的。可以尝试这样。不需要在这里采用任何循环概念,这在有大数据时非常有用。

NSString *str=[[array1 valueForKeyPath:@"name"] componentsJoinedByString:@","];
NSLog(@"desired string:%@",str);


NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setObject:@"app" forKey:@"name"];

    NSMutableDictionary *dict1 = [[NSMutableDictionary alloc]init];
    [dict1 setObject:@"app1" forKey:@"name"];


    NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
    [dict2 setObject:@"app2" forKey:@"name"];
    NSArray *array1 = [[NSArray alloc]initWithObjects:dict,dict1,dict2, nil];

    NSString *str=[[array1 valueForKeyPath:@"name"] componentsJoinedByString:@","];
    NSLog(@"desired string:%@",str);

    O/P:- app,app1,app2
于 2013-07-18T13:55:39.240 回答
5
NSArray * yourArray = @[
                        @{
                            @"date" : @"12/12/12",
                            @"name" : @"app"
                        },
                        @{
                            @"date" : @"11/02/12",
                            @"name" : @"app1"
                        },
                        @{
                            @"date" : @"14/05/12",
                            @"name" : @"app2"
                        }
                      ];

NSArray * selectedArray = [yourArray valueForKeyPath:@"@distinctUnionOfObjects.name"];
NSArray * sortedArray = [selectedArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    return [obj1 compare:obj2];
}];
NSString * str = [NSString stringWithFormat:@"name=%@",[sortedArray componentsJoinedByString:@","]];
NSLog(@"%@",str);

使用键值集合运算符:请参阅http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/KeyValueCoding/Articles/CollectionOperators.html

于 2013-07-18T14:00:09.827 回答