BigEARS BigEARS - 1 year ago 34
Objective-C Question

Remove an object with conditions in NSArray

For example, i have an array like that, in each object there are types of objects:

{
{//ob1
name = "zmg";
link = "abccom";
adress = "123 street";
son = "2";
},

{//ob2
name = "asf";
link = "http://fsdfabccom";
adress = "123 street";
son = "1";
},

{//ob3
name = "asf";
adress = "123 street";
son = "5";
},

{//ob4
name = "sadfsdfasdf";
link = "http://fsdfasdfasdfabc.com";
adress = "123 street";
son = "2";
},

{//ob5
name = "asf";
adress = "123 street";
son = "4";
},
}


What can I do if i want to delete all object, in which there is no object "link" (in this example: ob3, ob5 must be removed);
The result must be:

{
{//ob1
name = "zmg";
link = "abccom";
adress = "123 street";
son = "2";
},

{//ob2
name = "asf";
link = "http://fsdfabccom";
adress = "123 street";
son = "1";
},
{//ob3
name = "sadfsdfasdf";
link = "http://fsdfasdfasdfabc.com";
adress = "123 street";
son = "2";
},

}


Thank u so much!

Answer Source

try this way. you should read apple's documentation on this https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(link != null)"];
NSArray *array2 = [array filteredArrayUsingPredicate:predicate];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download