Similar to the WhatsApp 'Add Participants' to a new group feature, in my app I have a UITextField where the user can begin searching for contacts which also have the app installed to add to a group chat.
I store all of the available contacts in an NSMutableArray searchableContacts with the following keys
"firstName"
"lastName"
"phoneNumber"
Using the following delegate method, I would like to check the current input in the UITextField and see if it matches either part of the firstName OR lastName in the searchableContacts array:
-(void)textFieldDidChange :(UITextField *)theTextField{
// Check if the user input matches the beginning of either firstName or lastName in contactsArray. If so, output that user information to the tableView
}
You can use an NSPredicate to filter the array.
For example like this:
NSArray *users = #[
[[User alloc] initWithFirstName:#"Test" lastName:#"Me"],
[[User alloc] initWithFirstName:#"Fooo" lastName:#"bar"]
];
NSString *searchTerm = #"bar";
NSArray *filteredArray = [users filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(User *user, NSDictionary *bindings) {
return [user.firstName containsString:searchTerm] || [user.lastName containsString:searchTerm];
}]];
NSLog(#"Filtered values: %#", filteredArray);
searchTerm in your case would be the theTextField.text property value. Filtered array contains all matched contacts.
Cheers
Related
Get username values from sqlite db.
-(NSArray*)getUname
{
NSArray *resul = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSArray *fetchedRecords = [resul valueForKey:#"uName"];
}
Insert this array values into NSPopupButton
[_registeredUserPopupButton addItemsWithTitles:[self getUname]];
Get a string value from NSPopupButton dropdown list
NSString *usrNam = [NSString stringWithFormat:#"%#",[_registeredUserPopupButton selectedItem]];
From the above code usrNam value returns like below
"NSMenuItem: 0x6080000a9c00 mickel"
but i want my nsstring output as "mickel"
Just get the title from the menu item:
NSString *usrNam = [[_registeredUserPopupButton selectedItem] title];
or with dot notation
NSString *usrNam = _registeredUserPopupButton.selectedItem.title;
The -selectedItem method returns a NSMenuItem object. This in turn has a title property which is what I think you are looking for. Should be as below I think.
NSString *usrNam = [NSString stringWithFormat:#"%#",[_registeredUserPopupButton selectedItem.title]];
i've have this NSMutableArray called finalArray where i add the name and a image.
[finalArray addObject:#[name, image]];
in the following method its searching through the finalarray, but because its not expecting a multidimensional array it seem to give an error. How can i in this code:
self.filteredArray = [[finalArray filteredArrayUsingPredicate:predicate] mutableCopy];
get all the name objects from the finalArray? since that is what this code expect:
[[finalArray filteredArrayUsingPredicate:predicate]
the method:
-(void) searchThroughdata {
self.filteredArray = nil;
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF contains [c] %#",self.searchBar.text];
self.filteredArray = [[finalArray filteredArrayUsingPredicate:predicate] mutableCopy];
[tableViewData reloadData];
}
How do i add the image that belong to the name in the finalarray and still only search through the names like it do now with this?
[[finalArray filteredArrayUsingPredicate:predicate] mutableCopy];
It looks like you are trying to associate names and images with each other by using the same index of an array or something? Am I right?
If so then you should really be using a custom class to do this...
#interface MyNameImageClass : NSObject
#property NSString *name;
#property UIImage *image;
#end
Now you can create the object...
MyNameImageClass *object = [MyNameImageClass new];
object.name = theName;
object.image = theImage;
Then just add this to an array...
[finalArray addObject:object];
Having said that though, you need to clarify what you are asking. The question is very confusing.
By doing this if the image is updated on the object you don't need to reinsert it into the array. The array stores a reference to the object so the array will have the updated value too.
I have an NSManagedObject called WorkOrder with a boolean property isComplete defined in a category for that class.
I use an NSFetchedResultsController to fetch these from the data store and display them in a table view. I'd like to be able to filter the results based on the isComplete property, but of course the predicate in the NSFetchedResultsController can't do that because the property is not a Core Data attribute. I also can't filter the controller's fetchedObjects array because that property is read-only.
Is there any way to do what I'm trying to do without rolling my own data structure that mimics NSFetchedResultsController but allows me to filter the results post-fetch?
As I said in the comments for Adam Eberbach's answer, I solved this using an NSDictionary (self.workOrdersByDate, below) to store the grouped results, as well as an NSArray (self.dateSections, below) to store the sorted keys of the dictionary.
// Fetch all the WorkOrders
NSArray *results = [WorkOrder findAll];
// Filter based on isComplete property
NSPredicate *filter;
if (self.segmentedStatus.selectedSegmentIndex == SegmentAssigned) {
filter = [NSPredicate predicateWithFormat:#"isComplete == 0"];
} else {
filter = [NSPredicate predicateWithFormat:#"isComplete == 1"];
}
results = [results filteredArrayUsingPredicate:filter];
// Retain an NSArray of sorted NSDate keys
self.dateSections = [[results valueForKeyPath:#"#distinctUnionOfObjects.scheduledStartDate.beginningOfDay"] sortedArrayUsingSelector:#selector(compare:)];
// Create the dictionary
self.workOrdersByDate = [[NSMutableDictionary alloc] initWithCapacity:self.dateSections.count];
for (int sectionNum = 0; sectionNum < self.dateSections.count; sectionNum++) {
NSPredicate *sectionPredicate = [NSPredicate predicateWithFormat:#"scheduledStartDate.beginningOfDay == %#", [self.dateSections objectAtIndex:sectionNum]];
NSArray *workOrdersForSection = [results filteredArrayUsingPredicate:sectionPredicate];
[resultsDict setObject:workOrdersForSection forKey:[self.dateSections objectAtIndex:sectionNum]];
}
You receive an array of objects from the Core Data query, why not use filteredArrayUsingPredicate on that result to get the array you really want?
In this case you can probably abandon using the NSFetchedResultsController to fill your table directly but it will still be useful for its delegate notifications when data changes.
I want to update an mutable array. i have one array "ListArray" with some keys like "desc", "title" on other side (with click of button.) i have one array name newListArray which is coming from web service and has different data but has same keys like "desc" "title". so i want to add that data in "ListArray" . not want to replace data just add data on same keys. so that i can show that in tableview.
..........So my question is how to add data in "ListArray". or any other way to show that data in tableview but replacing old one just want to update the data
NSMutableArray *newListArray = (NSMutableArray*)[[WebServices sharedInstance] getVideoList:[PlacesDetails sharedInstance].userId tokenValue:[PlacesDetails sharedInstance].tokenID mediaIdMin:[PlacesDetails sharedInstance].minId mediaIdMax:[PlacesDetails sharedInstance].maxId viewPubPri:#"Public_click"];
NSDictionary *getNewListDic = [[NSDictionary alloc] initWithObjectsAndKeys:newListArray,#"videoList", nil];
[listVidArray addObject:getNewListDic];
NSArray *keys = [NSArray arrayWithObjects:#"desc",#"url",#"media_id",#"img",#"status",#"media_id_max",#"fb_url",#"title", nil] ;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for(int i = 0 ; i < [listVidArray count] ; i++)
{
for( id theKey in keys)
{
// NSMutableArray *item = [NSMutableArray array];
NSLog(#"%#",theKey);
NSLog(#"%#",keys);
[dict setObject:[[[listVidArray objectAtIndex:i]valueForKey:#"videoList"] valueForKey:theKey] forKey:theKey];
// [dict setObject:[newListArray valueForKey:theKey] forKey:theKey];
}
}
If you the newListArray is totally different from the oldListArray, then clear the old one, and use the new data to fit it.
Otherwise, you need to merge the two arrays. One way is to check if a
data in newListArray is/is not in oldListArray and then decide
whether to add it into oldListArray.
When oldListArray is updated, call -reloadData of the tableView.
If I do not misunderstand your question, you may do something like this:
[listArray addObjectsFromArray:newListArray];
[_tableView reloadData];
I'm making a scoreboard for my game. And when I do a NSLog of the data it comes out as this:
{
name = TTY;
score = "3.366347";
}
So my question is how do I remove this brackets and also just grab the name (TTY) and score (3.36 without the quotations) and place them in variable for me to put into labels.
Currently I can place them in labels but they have the lingering curly braces "{" and "}".
Any hints would be helpful as I'm happy to search further I just don't know the vocab to search for it.
thanks
For NSDictionary if you want to get the values you use objectForKey: method;
[scoreDictionary objectForKey:#"name"];
In your case the method will return TTY
You can store this in a variable like normal:
NSString *entryName = [scoreDictionary objectForKey:#"name"];
For NSArray (and NSMutableArray) you use objectAtIndex: method;
[scoresArray objectAtIndex:0];
I'm guessing that you have many NSDictionary in the NSArray, in which case you can combine the two methods above and get;
[[scoresArray objectAtIndex:0] objectForKey:#"name"];
which will give you the value of name in the first dictionary of the array.
Also if you want to access multiple key values you can use;
NSDictionary *entry = [scoresArray objectAtIndex:0];
NSString *entryName = [entry objectForKey:#"name"];
NSString *entryScore = [entry objectForKey:#"score"];