I have set up an NSPredicate to check if the object category_id is IN in an NSArray of numbers.
The code:
///This is working
NSArray *arr2 = [[NSArray alloc]initWithObjects:#[#3,#2,#3],nil];
[NSPredicate predicateWithFormat:#"category_id IN %#", arr2]
//This is not working
NSArray *arr = [[NSArray alloc]initWithArray:category_array]; //Array with NSNumbers
[NSPredicate predicateWithFormat:#"category_id IN %#", arr]
This was working fine but after I changed my category_array to contains NSNumber the predicate stopped working. How I use NSPredicate for NSNumber?
Update:
Arrays:
Entity for Category:
You are making minor but blatant mistake here. You are adding an array #[#1,#2,#4] as an object in the array category_array.
Try:
NSArray *category_array = [[NSArray alloc] initWithArray:#[#1,#2,#4]];
or simply
NSArray *category_array = #[#1,#2,#4];
You are making a little mistake
try out this code
NSArray *category_array = #[#1,#2,#4];
Related
I have a NSMutableArray containing NSString.
I need to filter only the objects that begins with (^WS|WV)[A-Z]{2}[0-9]{2}
How can I do it with NSPredicate?
Assuming that the array is called myArray, how can I write it?
Just google it to see a lot of answers like this:
NSArray *array = #[#"WSPS01", #"WLP05", #"1112"];
NSString *regex = #"(^WS|WV)[A-Z]{2}[0-9]{2}";
NSPredicate *pred = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", regex];
NSArray *filteredArray = [array filteredArrayUsingPredicate:pred];
// filteredArray contains only WSPS01
array - its your NSMutableArray
Solved.
this is the correct regex:
NSString *regex = #"(^WS|WV|WC)[A-Z]{2}[0-9]{2}.*"
The only question that I still have is why the MATCHES operator works and the BEGINSWITH not...
I'm trying to use the input on an NSTextField along with NSPredicate to populate a NSMutableArray of like objects.
In my app I have the following simple NSMutableArray
{
firstName = Danton;
phoneNumbers = 5555555555;
}
Now, I am using the following code to try and filter the results as the user types (if "Dan" is in the textField, the filtered array should include the above object)
NSMutableArray *filteredArray = [[NSMutableArray alloc] init];
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:#"firstName CONTAINS[cd] %#", self.searchField.text];
filteredArray = [[filteredArray filteredArrayUsingPredicate:sPredicate] mutableCopy];
NSLog(#"Filtered values: %#", filteredArray);
However, I am getting an empty return on filteredArray. Can someone tell me what is wrong with my code?
NSArray *array = #[
#{
#"firstName" : #"Danton", #"phoneNumbers" : #"5555555555"
}
];
NSArray *result = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"%K contains[c] %#", #"firstName", #"Dan"]];
I have an mutable array which consists dictionaries of contacts
[
{
"name":""
"Email":""
type:"A"
},
"name":""
"Email":""
type:"B"
}
"name":""
"Email":""
type:"C"
}........100 contacts
]
i want to filter them by type A,B or C that is taken care by a segmented switch.My question how to use predicate in order to filter this type of case.
Here is my implementation of filtering
-(NSArray *)filtercontcts:(NSString *)filterParameter
{
NSArray *filterContacts = [[NSArray alloc]init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"type == %#)",filterParameter];
filterContacts= [contacts filteredArrayUsingPredicate:predicate];
return filterContacts;
}
Help is Much Appreciated thanks.
The correct way to create the predicate is without the closing bracket like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"type == %#", filterParameter];
You are also leaking memory because you allocate filterContacts but then lose the reference to it when you overwrite the pointer with filteredArrayUsingPredicate:, simply remove the [[NSArray alloc]init]; to get rid of the leak.
Try like this :-
NSArray *filterContacts = [[NSArray alloc]init];
NSPredicate *predicate=[NSPredicate predicateWithFormat:#"category type[cd] %#,filterParameter];
filterContacts=[contacts filteredArrayUsingPredicate:predicate]mutableCopy];
I have a large number of different NSObject types that all have different properties and I am trying to abstract out a single method that will allow me to filter the NSArrays of the objects by simply passing in an NSArray of properties I wish to filter on. The number keys I filter on vary from possibly 1 to whatever.
Here is an example of the filtering NSArray
NSArray *filterBy = [NSArray arrayWithObjects:
#"ManufacturerID",
#"CustomerNumber",nil];
These keys also exist in the objects of my NSArray I am filtering, so basically this would need to generate something like this:
NSPredicate *pred = [NSPredicate predicateWithFormat:#"%K == %# AND %K == %#",
[filterBy objectAtIndex:0],
[items valueForKey: [filterBy objectAtindex:0],
[filterBy objectAtIndex:1],
[items valueForKey: [filterBy objectAtIndex:1]];
Which would generate something like: ManufacturerID==18 AND CustomerNumber=='WE543'
Is it possible to do this?
This is easy. Check it out:
NSMutableArray *subpredicates = [NSMutableArray array];
for (NSString *filterKey in filterBy) {
NSString *filterValue = [items valueForKey:filterKey];
NSPredicate *p = [NSPredicate predicateWithFormat:#"%K = %#", filterKey, filterValue];
[subpredicates addObject:p];
}
NSPredicate *final = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
I want to search a specific string in the array of strings in objective c. Can somebody help me in this regard?
BOOL isTheObjectThere = [myArray containsObject: #"my string"];
or if you need to know where it is
NSUInteger indexOfTheObject = [myArray indexOfObject: #"my string"];
I strongly recommend you read the documentation on NSArray. It's best to do that before posting your question :-)
You can use NSPredicate class for searching strings in array of strings.
See the below sample code.
NSMutableArray *cars = [NSMutableArray arrayWithObjects:#"Maruthi",#"Hyundai", #"Ford", #"Benz", #"BMW",#"Toyota",nil];
NSString *stringToSearch = #"i";
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF contains[c] %#",stringToSearch]; // if you need case sensitive search avoid '[c]' in the predicate
NSArray *results = [cars filteredArrayUsingPredicate:predicate];
This is the most efficient way for searching strings in array of strings
NSMutableArray *cars = [NSMutableArray arrayWithObjects:#"Max",#"Hai", #"Fine", #"Bow", #"Bomb",#"Toy",nil];
NSString *searchText = #"i";
NSArray *results = [cars filteredArrayUsingPredicate:predicate];
// if you need case sensitive search avoid '[c]' in the predicate
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:#"title contains[c] %#",
searchText];
searchResults = [cars filteredArrayUsingPredicate:resultPredicate];