Reputation: 305
i have an NSArray with JsonValue from my webservice.
myArray = [responseString JSONValue];
this Array give me that in a 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";
}
)
I need to create a new array with only sender = 'David'
i try with that:
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]);
}
}
this myNewArray give me that:
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";
}
)
But i need to have a array like that:
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
Upvotes: 0
Views: 99
Reputation: 9850
There's a much easier way. Try using NSArray's filteredArrayUsingPredicate:
where the predicate in your case is something like this:
NSString *attribute = @"sender";
NSString *senderFilter = @"david";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@", attribute, senderFilter];
NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate];
For more details on predicates, try the Predicate Programming Guide.
Upvotes: 1
Reputation: 29767
Declare myNewArray
outside of the cycle, and make it a NSMutableArray (it'll allow to add new objects )
myNewArray = [[NSMutableArray alloc] array];
for(int i=0;i<[myArray count];i++)
{
...
}
then add object to this array if some condition is equal
NSLog(@"YES");
[myNewArray addObject:dict1];
Upvotes: 1
Reputation: 29381
A NSArray is immutable, you will need a NSMutableArray. And you are re-initliazing the new array on every loop, which you cannot do if you want to keep the previous values.
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];
}
...
}
Upvotes: 1