Objective-c: Making NSArray all lowercased - ios

i am having a strange problem when tryin to catch some data on my plist..
so, here is my plist
so okay, getting the data is fine, so then i used this code to just you know get the data
NSString *path = [[NSBundle mainBundle] pathForResource:#"kurdiebg" ofType:#"plist"];
NSArray *plistData = [NSArray arrayWithContentsOfFile:path];
NSPredicate *filter = [NSPredicate predicateWithFormat:#"english = %#", self.searchQwery.text];
NSArray *filtered = [plistData filteredArrayUsingPredicate:filter];
NSLog(#"found matches: %# : %#", filtered,[filtered valueForKey:#"kurdi"]);
if (filtered.count>0) {
NSDictionary *dic = filtered[0];
self.ss.text = dic[#"kurdi"];
}
but here to the strange part-- when i try to search for abbey (lowercased)it returns the right result, the problem is it has twenty two thousand records they're not all lowercased,
okay, then when i make the first A capital, it returns nothing
Thanks for even visiting

You can do case insensitive search by adding [c].
Try this.
NSPredicate *filter = [NSPredicate predicateWithFormat:#"english ==[c] %#", self.searchQwery.text];

Related

NSDictionary values changes without any modification

I added an NSDictionary to NSMutableArray.
for (TblFiles *objTblFile in visitorFilesArray) {
NSData *fileDataTemp = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: objTblFile.internalFileName]];
NSDictionary *tempDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:(NSInteger)objTblFile.fileID],#"fileID",
objTblFile.fileName,#"fileName",
fileDataTemp,#"fileData",nil];
[filesArrayInAddVisitor addObject:tempDict];
}
After that I tried to delete one dictionary from this array, I am getting crash. It happens because of fileID values are changed. Some time its working perfect. Some times getting crash.
NSPredicate *objPredicate = [NSPredicate predicateWithFormat:#"fileID == %#",151];
NSArray *filterArray = [filesArrayInAddVisitor filteredArrayUsingPredicate:objPredicate];
[self->filesArrayInAddVisitor removeObject:[filterArray objectAtIndex:0]];
[self->fileTableView reloadData];
I don't know why values are changed in NSDictionary.
Example : I added fileID as 151, but in NSMutableArray it changed to zero in some cases.
1.First problem i got for crash is the predicate string.
replace it with this
NSPredicate *objPredicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"fileID == %d",151]];
2.You are saying that fileID value got change then first confirm that what is the type of objTblFile.fileID if it is NSInteger then its ok.
For my example i have passed NSInteger there and code working fine.

NSPredicate with IN NSNumbers

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];

Using NSPredicate with NSMutableArray

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"]];

Replacing content in string within NSPredicate

I am trying to replace text within an array and for some reason the code doesn't work, despite no line alert by Xcode, it causes it to crash.
i.e. "name, 2013"
stripping ", 2013"
to result in
"name"
What am I doing wrong, for the life of me I can't work it out and desperately need help on this?
The specific line in question is "case ITSectionTypeAuthor:"
- (void)setUpDataByType:(ITSectionType) type andFilter:(NSString *)filter {
self.type = type;
self.filter = (filter)? filter : #"";
[self.sections removeAllObjects];
NSPredicate *predicate = nil;
NSString *descriptorKey = #"genus";
NSArray *rowData = [[ITData sharedObject] animals]; //coming form singleton
//generate predicate
switch (self.type) {
case ITSectionTypeAuthor:
predicate = [NSPredicate predicateWithFormat:#"describer componentsSeparatedByString:#", "] objectAtIndex:0] LIKE %#", filter];
break;
default:
predicate = nil;
break;
}
I think the problem is in this line:
predicate = [NSPredicate predicateWithFormat:#"describer componentsSeparatedByString:#", "] objectAtIndex:0] LIKE %#", filter];
You're creating an objective-c string:
#"describer componentsSeparatedByString:#"
and then a C-string:
"] objectAtIndex:0] LIKE %#"
That can't be right, can it? I think you want:
predicate = [NSPredicate predicateWithFormat:#"describer LIKE %#", [[filter componentsSeparatedByString:#", "] objectAtIndex:0]];

dynamically generate NSPredicate from an NSArray

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];

Resources