从nsarray构建一个NSArray已经存在

时间:2012-02-18 17:42:04

标签: iphone xcode nsarray nsdictionary

我的网络服务中有一个带有JsonValue的NSArray。

myArray = [responseString JSONValue];

这个数组在NSLog中给我这个:

 2012-02-18 18:22:46.338 Test[2758:fb03] myArray: (
            {
            receiver = david;
            sender = sophie;
            message = "test1";
            photo = "****************.jpg";
        },
            {
            receiver = david;
            sender = matt;
            message = "test2";
            photo = "****************.jpg";
        },
            {
            receiver = sophie;
            sender = mike;
            message = "test1";
            photo = "****************.jpg";
        },
            {
            receiver = david;
            sender = ali;
            message = "test1";
            photo = "****************.jpg";
        },
            {
            receiver = mike;
            sender = david;
            message = "test1";
            photo = "****************.jpg";
        },
            {
            receiver = admin;
            sender = david;
            message = "test1";
            photo = "****************.jpg";
        }
    )

我需要创建一个只有sender ='David'

的新数组

我尝试着:

for(int i=0;i<[myArray count];i++)
   {
    NSDictionary *dict1=[myArray objectAtIndex:i];


       if ( [[dict1 objectForKey:@"sender"] isEqualToString:@"david"]) 


       {

           NSLog(@"YES");


           myNewArray = [[NSArray alloc] initWithObjects:dict1, nil];

            NSLog(@"array %@",myNewArray);
            NSLog(@"count %d",[myNewArray count]);



       }
       else
       {
           NSLog(@"NO");


           myNewArray2 = [[NSArray alloc] initWithObjects:dict1, nil];

            NSLog(@"array2 %@",myNewArray2);
            NSLog(@"count2 %d",[myNewArray2 count]);
       }
   }

这个myNewArray给了我:

           2012-02-18 18:22:46.341 Test[2758:fb03] myNewArray (
        {
            receiver = mike;
            sender = david;
            message = "test1";
            photo = "****************.jpg";
    }
)
          2012-02-18 18:22:46.341 Test[2758:fb03] myNewArray (
        {
            receiver = admin;
            sender = david;
            message = "test1";
            photo = "****************.jpg";
    }
)

但我需要有这样的数组:

2012-02-18 18:22:46.338 Test[2758:fb03] myNewArray: (

        {
            receiver = mike;
            sender = david;
            message = "test1";
            photo = "****************.jpg";
    },
        {
            receiver = admin;
            sender = david;
            message = "test1";
            photo = "****************.jpg";
    }
)

thx for reading

3 个答案:

答案 0 :(得分:1)

有一种更简单的方法。尝试使用NSArray的filteredArrayUsingPredicate:,其中您的案例中的谓词是这样的:

NSString *attribute = @"sender";
NSString *senderFilter = @"david";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@", attribute, senderFilter];

NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate];

有关谓词的更多详细信息,请尝试Predicate Programming Guide

答案 1 :(得分:1)

NSArray是不可变的,你需要一个NSMutableArray。并且您在每个循环上重新初始化新数组,如果您想保留以前的值,则无法执行此操作。

NSMutableArray *a = [[NSMutableArray alloc] init];

for (int i = 0; i < [myArray count]; i++) {
    NSDictionary *m = [myArray objectAtIndex:i];

    if ([[m objectForKey:@"sender"] isEqualToString:@"david"]) {
        [a addObject:m];
    }
    ...
}

答案 2 :(得分:1)

在循环之外声明myNewArray,并使其成为NSMutableArray(它将允许添加新对象)

myNewArray = [[NSMutableArray alloc] array];
for(int i=0;i<[myArray count];i++)
{
    ...
}

然后在某些条件相等的情况下将对象添加到此数组

NSLog(@"YES");
[myNewArray addObject:dict1];