Understanding a logic in update/add - NSUserDefaults (Objective C) - objective-c

I'm writing a nice class to handle call block in NSUserDefaults.
Haven't tested it yet, but I think I miss something regarding update/add by callBlockID.
My Code:
My Code:
#implementation UserDefaultHandler
+ (UserDefaultHandler *)sharedInstance
{
static UserDefaultHandler *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[UserDefaultHandler alloc] init];
});
return instance;
}
-(void)updateOrAddCallBlock:(NSMutableDictionary*)callBlockObject
{
NSString *callBlockID = [callBlockObject objectForKey:#"callBlockID"];
BOOL needToUpdate = NO;
NSInteger indexDict = 0;
NSMutableArray *arrayOfAllCallBlock = [[NSMutableArray alloc] init];
arrayOfAllCallBlock = (NSMutableArray*)[[NSUserDefaults standardUserDefaults] objectForKey:#"arrayOfAllCallBlock"];
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
if (arrayOfAllCallBlock.count <= 0 || arrayOfAllCallBlock == nil)
{
[arrayOfAllCallBlock addObject:callBlockObject];
}
else
{
for (int i = 0; i < arrayOfAllCallBlock.count; i++)
{
NSMutableDictionary *mutableDict = [arrayOfAllCallBlock objectAtIndex:i];
NSString *idFromDict = [mutableDict objectForKey:#"callBlockID"];
if ([idFromDict isEqualToString:callBlockID])
{
needToUpdate = YES;
indexDict = i;
newDict = mutableDict;
}
}
if (needToUpdate)
{
[arrayOfAllCallBlock removeObjectAtIndex:indexDict];
[arrayOfAllCallBlock insertObject:newDict atIndex:indexDict];
}
else
{
[arrayOfAllCallBlock addObject:callBlockObject];
}
}
[self saveGlobalDict:arrayOfAllCallBlock];
}
- (NSMutableDictionary *)getCallBlockByID:(NSString*)callBlockID
{
NSMutableArray *arrayOfAllCallBlock = (NSMutableArray*)[[NSUserDefaults standardUserDefaults] objectForKey:#"arrayOfAllCallBlock"];
BOOL isFound = NO;
NSInteger indexDict = 0;
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
for (int i = 0; i < arrayOfAllCallBlock.count; i++)
{
NSMutableDictionary *mutableDict = [arrayOfAllCallBlock objectAtIndex:i];
NSString *idFromDict = [mutableDict objectForKey:#"callBlockID"];
if ([idFromDict isEqualToString:callBlockID])
{
isFound = YES;
indexDict = i;
newDict = mutableDict;
}
}
if (isFound)
{
return newDict;
}
else
{
return nil;
}
}
- (NSMutableArray*)getAllCallBlock
{
NSMutableArray *arrayOfAllCallBlock = (NSMutableArray*)[[NSUserDefaults standardUserDefaults] objectForKey:#"arrayOfAllCallBlock"];
return arrayOfAllCallBlock;
}
- (void)clearCallBlockDB
{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"arrayOfAllCallBlock"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)saveGlobalDict:(NSMutableArray*)globalArray
{
self.globalArray = [[NSMutableArray alloc] init];
self.globalArray = globalArray;
[self clearCallBlockDB];
[[NSUserDefaults standardUserDefaults] setObject:self.globalArray forKey:#"arrayOfAllCallBlock"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)clearSingleCallBlock:(NSString*)callBlockID
{
NSMutableArray *array = [[NSMutableArray alloc] init];
array = [self getAllCallBlock];
NSMutableDictionary *specificDict = [[NSMutableDictionary alloc] init];
NSInteger getIndex = [self getIndexForCallBlock:array specificDict:specificDict];
[array removeObjectAtIndex:getIndex];
[self saveGlobalDict:array];
}
- (NSInteger)getIndexForCallBlock:(NSMutableArray*)arrayOfAllCallBlock specificDict:(NSMutableDictionary*)specificDict
{
NSInteger getIndex = 0;
for (int i = 0; i < arrayOfAllCallBlock.count; i++)
{
NSMutableDictionary *mutableDict = [arrayOfAllCallBlock objectAtIndex:i];
NSDictionary *dict_one = [specificDict copy];
NSDictionary *dict_two = [mutableDict copy];
if ([dict_two isEqualToDictionary:dict_one])
{
getIndex = i;
}
}
return getIndex;
}
#end
I'll explain: I've a UITableView and when I add a row to it, I've a new window to put details, save and present back in UITableView.
Also, I've an edit button so I can edit and update a row in the table.
Adding a row to table create a unique string (callBlockID).
I'm not sure how to handle it and whether or not my code is enough.
Any ideas?

Related

How to form an array of dictionaries for multilevel(any number) tableview from the core data object

I need to create array of dictionaries, of data coming from a database object. This array of dictionaries can be of multiple level,and has a parent child relationship depending upon the level.
From the array form I need to create a multilevel table view (level can be any depending upon the data loaded)
Depending upon the catal_id of the coredata object, next set of Catal objects are loaded from the database. Code supporting is shown as below.
I want to create the array as shown in the image
From the above load of loadMainCatalData I am able to load the table but on didSelectRowAtIndexPath I am not able to form the proper array for the expand collapse table. Catal object gets added twice, on viewDidLoad.
There is some issue with function.
**Please help to form the proper array to load the table. Stuck here **.
The coredata object is of the below format:
<__NSArrayM 0x109c3b7a0>(
<Catal: 0x104cccfb0>
(entity: Catal; id: 0xd000000008880006 <x-coredata://F849E220-C905-4359-8CD5-18D5E35FC13A/Catal/p546> ; data: {
breadcrumb = "";
"catal_id" = "SNV2";
"id_ni" = 1;
"id_parent" = 0;
imgId = 1;
title = "Adventure";
"nb_element" = 1010;
order = 38;
}),
<Catal: 0x104ccd3f0> (entity: Catal; id: 0xd000000006e40006 <x-coredata://F849E220-C905-4359-8CD5-18D5E35FC13A/Catal/p441> ; data: {
breadcrumb = "";
"catal_id" = "SNV1";
"id_ni" = 1;
"id_parent" = 0;
imgId = 38;
title = Gros;
"nb_element" = 1366;
order = 82;
}),
<Catal: 0x104ccd6e0> (entity: Catal; id: 0xd00000000a500006 <x-coredata://F849E220-C905-4359-8CD5-18D5E35FC13A/Catal/p660> ; data: <fault>),
<Catal: 0x104ccd790> (entity: Catal; id: 0xd000000005d40006 <x-coredata://F849E220-C905-4359-8CD5-18D5E35FC13A/Catal/p373> ; data: <fault>),
<Catal: 0x104ccd940> (entity: Catal; id: 0xd00000000acc0006 <x-coredata://F849E220-C905-4359-8CD5-18D5E35FC13A/Catal/p691> ; data: <fault>)
)
My code goes as below
- (void)viewDidLoad {
[super viewDidLoad];
if (!self.catalList || self.catalList.count == 0) {
[self loadDataCatal];
isAlreadyInserted = NO;
}
}
- (void)loadDataCatal{
[self loadMainCatalData];
self.arForTable = [NSMutableArray array];
[self.arForTable addObjectsFromArray:self.arrayOriginal];
}
-(void)loadMainCatalData {
NSMutableArray *arrCatalList = [[NSMutableArray alloc] init];
if (catLevel == NULL){
[arrCatalList addObjectsFromArray:[Catal fillDataCatal:#"0" :#"0"]];
}
self.arrayOriginal = [NSMutableArray array];
for (Catal *objCatal in arrCatalList){
ProductCategoryFilter *objProductCatFilter = [[ProductCategoryFilter alloc] init];
[objProductCatFilter setCatalCategory:objCatal];
NSMutableArray *arr = [self loadSubCatalData:objCatal];
[objProductCatFilter setArrCatalSubCategory:arr];
[self.arrayOriginal addObject:objProductCatFilter];
}
}
-(NSMutableArray *)loadSubCatalData:(Catal *)objCatal{
NSMutableArray *arrSubCatal = [NSMutableArray array];
ProductCategoryFilter *objProductCatFilter = [[ProductCategoryFilter alloc] init];
[objProductCatFilter setCatalCategory:objCatal];
NSArray *arrCatal = [Catal fillDataCatal:objCatal.catal_id :#""];
NSMutableArray *arrSubCat = [NSMutableArray array];
for (Catal *subCatal in arrCatal){
ProductCategoryFilter *objSubCatFilter = [[ProductCategoryFilter alloc] init];
[objSubCatFilter setCatalCategory:subCatal];
NSMutableArray *arr = [self loadSubCatalData:subCatal];
[objSubCatFilter setArrCatalSubCategory:arr];
[arrSubCat addObject:objSubCatFilter];
}
[objProductCatFilter setArrCatalSubCategory:arrSubCat];
[arrSubCatal addObject:objProductCatFilter];
return arrSubCatal;
}
// the ProductCategoryFilter class
#import <Foundation/Foundation.h>
#import "Catal+CoreDataClass.h"
#interface ProductCategoryFilter : NSObject
#property (nonatomic,strong) Catal* catalCategory;
#property (nonatomic,strong) NSMutableArray * arrCatalSubCategory;
#end
// Catal CoreDataObject
#import <Foundation/Foundation.h>
#import "Catal+CoreDataClass.h"
+(BOOL)fillSubDataCatal:(NSString *)catal_id
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *context = [[CoreDataHelper getInstance] managedObjectContext];
NSError *error;
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"Catal" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat:#"id_parent == %#", catal_id];
[fetchRequest setPredicate:predicate];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
DebugLog(#"[fetchedObjects count] : %lu",(unsigned long)fetchedObjects.count);
if([fetchedObjects count] > 0)
{
return true;
}
return false;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier =#"AMG_PP_SubCategoryTableCell";
AMG_PP_SubCategoryTableCell *cell = (AMG_PP_SubCategoryTableCell *) [self.tblProductCategory dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell =(AMG_PP_SubCategoryTableCell *)[nib objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
ProductCategoryFilter *objCat = [self.arForTable objectAtIndex:indexPath.row];
[cell.imgRadio setImage:[UIImage imageNamed:#"PlusIcon"]];
cell.textLabel.text = objCat.catalCategory.libelle; //] [ valueForKey:#"name"];
// [cell setIndentationLevel:[[[self.arForTable objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AMG_PP_SubCategoryTableCell *cell = (AMG_PP_SubCategoryTableCell *)[self.tblProductCategory cellForRowAtIndexPath:indexPath];
[self.tblProductCategory deselectRowAtIndexPath:indexPath animated:YES];
ProductCategoryFilter *objCatal = [self.arForTable objectAtIndex:indexPath.row];
if([[objCatal arrCatalSubCategory] count] > 0) {
NSMutableArray *ar=[objCatal arrCatalSubCategory];
if(ar != nil){
isAlreadyInserted=NO;
for(ProductCategoryFilter *dInner in ar ){
NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner];
isAlreadyInserted=(index>0 && index!=NSIntegerMax);
if(isAlreadyInserted) break;
}
if(isAlreadyInserted) {
[self miniMizeThisRows:ar];
} else {
NSUInteger count=indexPath.row+1;
NSMutableArray *arCells=[NSMutableArray array];
for(ProductCategoryFilter *dInner in ar ){
[arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
[self.arForTable insertObject:dInner atIndex:count++];
}
[cell.imgRadio setImage:[UIImage imageNamed:#"MinusIcon"]];
[tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
}
}
}
}
-(void)miniMizeThisRows:(NSArray*)ar{
for(ProductCategoryFilter *dInner in ar ){
NSUInteger indexToRemove=[self.arForTable indexOfObjectIdenticalTo:dInner];
NSMutableArray *arInner=[dInner arrCatalSubCategory];
if(arInner && [arInner count]>0){
if (arInner != nil) {
[self miniMizeThisRows:arInner];
}
}
if([self.arForTable indexOfObjectIdenticalTo:dInner]!=NSNotFound) {
[self.arForTable removeObjectIdenticalTo:dInner];
[self.tblProductCategory deleteRowsAtIndexPaths:[NSArray arrayWithObject:
[NSIndexPath indexPathForRow:indexToRemove inSection:0]
]
withRowAnimation:UITableViewRowAnimationRight];
}
}
}
Here's the simple code which help you in understanding the how to create a array of dictionary :
var arrayOfDict = [[String : String]]()
let dict1 = ["FirstName" : "Abc" , "LastName" : "XYZ"]
let dict2 = ["HouseNo" : "WW49", "Locality" : "GymKhana"]
let dict3 = ["City" : "mnb", "State" : "lkop" , "Country" : "mkl"]
arrayOfDict.append(dict1)
arrayOfDict.append(dict2)
arrayOfDict.append(dict3)
print(arrayOfDict)

multithreading error with ABAddressBookRef

I have problems with the threads at the recorer for, I have a contact list of 600 and 6 shows me the following error.
Thread 1: EXC_BAD_ACCESS (code = 1, address = 0x29)
is the following code
-(void) actualizarContactos
{
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else { // we're on iOS 5 or older
accessGranted = YES;
}
if (accessGranted) {
//ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
// NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];
NSMutableArray *TodosContactos = [[NSMutableArray alloc]init];
//consultar numero de contactos anteriores
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *numeroContactos = [userDefaults objectForKey:#"numeroContactos"];
int numContactos = [numeroContactos intValue];
//validar si hay contactos nuevos
if(numContactos != nPeople)
{
for (int i = 0; i < nPeople; i++)
{
// NSMutableArray *contacto = [[NSMutableArray alloc]init];
ContactsData *contacts = [ContactsData new];
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
//get First Name and Last Name
// NSMutableArray *name = [[NSMutableArray alloc]init];
if((__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty))
{
contacts.firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
}
if((__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty))
{
contacts.lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
}
if (!contacts.firstName) {
contacts.firstName = #"";
}
if (!contacts.lastName) {
contacts.lastName = #"";
}
contacts.compositeName = [NSString stringWithFormat:#"%# %#",contacts.firstName,contacts.lastName];
// get contacts picture, if pic doesn't exists, show standart one
Boolean sw = true;
NSData *imgData = (__bridge_transfer NSData *)ABPersonCopyImageData(person);
contacts.photo = [UIImage imageWithData:imgData];
if (!contacts.photo) {
sw = false;
contacts.photo = [UIImage imageNamed:#"contacts-ico#2x.png"];
}
//get Phone Numbers
NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
Boolean swContactoSiguiente = true;
ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);i++)
{
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
NSString *phoneNumber = (__bridge_transfer NSString *) phoneNumberRef;
//prueba
// NSLog([NSString stringWithFormat:#"name: %# tel: %#",contacts.compositeName,phoneNumbers]);
if(![phoneNumber hasPrefix:#"+"] && ![phoneNumber hasPrefix:#"00"] && ![phoneNumber hasPrefix:#"011"])
{
swContactoSiguiente = true;
}
else
{
swContactoSiguiente = false;
[phoneNumbers addObject:phoneNumber];
}
}
if(swContactoSiguiente)
{
continue;
}
contacts.phones = phoneNumbers;
//get Contact email
NSMutableArray *contactEmails = [NSMutableArray new];
ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex i=0; i<ABMultiValueGetCount(multiEmails); i++) {
CFStringRef contactEmailRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
NSString *contactEmail = (__bridge_transfer NSString *)contactEmailRef;
[contactEmails addObject:contactEmail];
// NSLog(#"All emails are:%#", contactEmails);
}
// [contacts setEmails:contactEmails];
contacts.emails = contactEmails;
//guardar en CoreData
if(contacts.phones.count != 0)
{
contacts.phone = [self deleteSpace:contacts.phones[0]];
}
//se valida existencia del phone en la base de datos
if([self consultarTelefono:contacts.phone])
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
newContact = [NSEntityDescription insertNewObjectForEntityForName:#"Contacts" inManagedObjectContext:context];
[newContact setValue: contacts.compositeName forKey:#"compositeName"];
[newContact setValue: contacts.firstName forKey:#"firstName"];
[newContact setValue: contacts.lastName forKey:#"lastName"];
[newContact setValue: contacts.phone forKey:#"phone"];
NSNumber *fv = [NSNumber numberWithBool:false];
[newContact setValue:fv forKey:#"favorite"];
if(sw)
{
[newContact setValue: imgData forKey:#"photo"];
}
NSError *error;
[context save:&error];
// [items addObject:contacts];
}
//ABRecordRef
// CFRelease(addressBook2);
// CFRelease(person);
// CFRelease(source2);
// CFRelease(allPeople2);
// NSLog([NSString stringWithFormat:#"%d",i]);
}
// se guarda en memoria numero de contactos
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSString stringWithFormat:#"%d",(int)nPeople] forKey:#"numeroContactos"];
[defaults synchronize];
}
}
else
{
UIAlertView *alertFailSave = [[UIAlertView alloc] initWithTitle:#"Error"
message:NSLocalizedString(#"TEXT_ACCESS", nil)
delegate:nil
cancelButtonTitle:NSLocalizedString(#"LO_BUTTON_OK", nil)
otherButtonTitles:nil];
[alertFailSave show];
}
}
la corrección fue cambiar el siguiente codigo:
// ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
// ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
// CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
// CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
// NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];
por este.
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

uisearchbar in grouped section uitable

I've pieced together several tutorials to create a grouped table with sections and I'm now trying to get a uisearchbar to work. the problem I'm having is how to search within the grouped sections.
I've read the similar questions this post suggested but can't
This is the code to create the grouped sections
#import "Job.h" // A model for the data
#import "Address.h" // Another model for the data
- (void)viewDidLoad
{
[super viewDidLoad];
self.theTable.delegate = self;
self.theTable.dataSource =self;
_searchBar.delegate = (id)self;
FMDBDataAccess *db = [[FMDBDataAccess alloc] init];
jobs = [[NSMutableArray alloc] init];
jobs = [db getJobs:1];
_sections = [[NSMutableDictionary alloc] init];
NSMutableArray *jobsTempArray = [db getJobsAsDictionary:1];
BOOL found;
// Loop through the books and create our keys
for (NSDictionary *book in jobsTempArray)
{
NSString *cLong = [book objectForKey:#"addrAddress"];
NSString *c = [cLong substringToIndex:1];
found = NO;
for (NSString *str in [_sections allKeys])
{
if ([str isEqualToString:c])
{
found = YES;
}
}
if (!found)
{
[_sections setValue:[[NSMutableArray alloc] init] forKey:c];
}
}
// Loop again and sort the books into their respective keys
for (NSDictionary *book in jobsTempArray)
{
[[_sections objectForKey:[[book objectForKey:#"addrAddress"] substringToIndex:1]] addObject:book];
}
// Sort each section array
for (NSString *key in [_sections allKeys])
{
[[_sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"addrAddress" ascending:YES]]];
}
}
And this is the code that searches
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
_isFiltered = FALSE;
}
else
{
_isFiltered = true;
_filteredjobs = [[NSMutableArray alloc] init];
//for (Job* book in jobs)
//for (Job* book in [_sections allKeys])
//for (NSString *food in [_sections allKeys])
for (NSDictionary* book in [_sections allKeys])
{
NSString *addrStr = [book objectForKey:#"addrAddress"];
NSString *postStr = [book objectForKey:#"addrPostcode"];
//NSRange nameRange = [book.jobAddress rangeOfString:text options:NSCaseInsensitiveSearch];
NSRange nameRange = [addrStr rangeOfString:text options:NSCaseInsensitiveSearch];
//NSRange descriptionRange = [book.jobPostcode rangeOfString:text options:NSCaseInsensitiveSearch];
NSRange descriptionRange = [postStr rangeOfString:text options:NSCaseInsensitiveSearch];
if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound)
{
[_filteredjobs addObject:book];
}
}
}
[self.theTable reloadData];
}
I've got as far as realising I need to change for (Job* food in jobs) to for (NSDictionary* book in [_sections allKeys]) but I'm stuck how to search within [_sections allKeys]
Specifically this line
NSRange nameRange = [addrStr rangeOfString:text options:NSCaseInsensitiveSearch];
which crashes with
-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x692e200
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]:
unrecognized selector sent to instance 0x692e200':
Any ideas? PS Treat me as a noob, I'll need some code as well as explanation - I'm still learning obj-c
Check the Link.It shows the UISearchBar With Grouped Section Tableview.Its a simple Tutorial..Hope its useful for you
I found the answer in
UISearchBar - search a NSDictionary of Arrays of Objects and reading up on allkeys.
Basically loop through the grouped NSDictionary and extract the NSArrays, then loop through again searching...
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
_isFiltered = FALSE;
}
else
{
_isFiltered = true;
_filteredjobs = [[NSMutableArray alloc] init];
NSString *currentLetter = [[NSString alloc] init];
for (int i=0; i<[_sections count]; i++)
{
currentLetter = [[_sections allKeys] objectAtIndex:i];
NSArray *jobsForKey = [ [NSArray alloc] initWithArray:[_sections objectForKey:[[_sections allKeys] objectAtIndex:i]] ];
for (int j=0; j<[jobsForKey count]; j++)
{
NSDictionary *book = [jobsForKey objectAtIndex:j];
NSRange titleResultsRange = [[book objectForKey:#"addrAddress"] rangeOfString:text options:NSCaseInsensitiveSearch];
if(titleResultsRange.location != NSNotFound)
{
[_filteredjobs addObject:book];
}
}
}
}
[self.theTable reloadData];
}

getting NCFString error while searching the tableview using searchbar

i ve got a table view parsed from json string.everthing works good .if i m to type a character in my search bar i m getting this error '-[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x1d52ac50'below is the code.were the below variable name data is NSMutableArray.could u guys help me out.
- (BOOL)searchDisplayController:(UISearchDisplayController *)controllershouldReloadTableForSearchString:(NSString *)searchString
{
[searchData removeAllObjects];
NSArray *group;
for(group in nameData)
{
NSLog(#"wat am i doing here:%#",group);
NSMutableArray *newGroup = [[NSMutableArray alloc] init];
NSString *element;
for(element in group)
{
NSRange range = [element rangeOfString:searchString options:NSCaseInsensitiveSearch];
if (range.length > 0) {
[newGroup addObject:element];
}
}
if ([newGroup count] > 0) {
[searchData addObject:newGroup];
}
}
return YES;
}
Please check out this code
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
NSMutableArray *array=[[NSMutableArray alloc]init];
NSDictionary *naming= [self.friendsDictionary objectForKey:#"data"];
NSLog(#"ggg %#",[self.friendsDictionary objectForKey:#"data"]);
for(int i =0 ; i< [[self.friendsDictionary objectForKey:#"data"] count]; i++){
[array addObject:[[[self.friendsDictionary objectForKey:#"data"] objectAtIndex:i] objectForKey:#"name"]];
}
NSLog(#"Array %#", array);
if(self.searchDisplayController.searchBar.text.length>0)
{
NSString *strSearchText = self.searchDisplayController.searchBar.text;
NSMutableArray *group = [[NSMutableArray alloc]init];
for (int i=0; i<array.count; i++) {
NSString *strData = [array objectAtIndex:i];
NSLog(#"string Data:%#",strData);
NSRange rng = [strData rangeOfString:strSearchText options:NSCaseInsensitiveSearch];
if(rng.location != NSNotFound)
{
if(rng.location== 0)//that is we are checking only the start of the names.
{
[group addObject:strData];
}
}
}
if ([group count] > 0)
{
searchData = [[NSMutableArray alloc] init];
[searchData addObjectsFromArray:group];
NSLog(#"Sear %#", searchData);
}
}
return YES;
}

Problem with NSMutableArray and custom NSObject

I'm really desperate on this one. I'm trying to make a Framework which you can search and play YouTube videos with. But while testing it, I'm running in to a big problem.
In the search operation I'm adding YTVideos (a subclass of NSObject) to a NSMutableArray. When I loop thru it in the main(), I'm getting nil-objects:
Method
- (NSArray *)videosInRange:(NSRange)range {
if(range.length > 50) {
[NSException raise:#"Range lenth > 50"
format:#"The range of -videosInRange: can't be bigger than 50"];
return nil;
}
if((range.location + range.length) > 999) {
[NSException raise:#"Range to big"
format:#"The given range was to big (%d, %d)", range.location, range.length];
return nil;
}
NSString *searchURLString = [[self feedURL] absoluteString];
searchURLString = [searchURLString stringBySettingURLAttribute:#"start-index" value:[NSString stringWithFormat:#"%d",range.location + 1]];
searchURLString = [searchURLString stringBySettingURLAttribute:#"max-results" value:[NSString stringWithFormat:#"%d",range.length]];
NSLog(#"%#",searchURLString);
NSURL *url = [NSURL URLWithString:searchURLString];
NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:url
options:0
error:NULL];
if(!xmlDoc)
return nil;
NSArray *videoElements = [[xmlDoc rootElement] elementsForName:#"entry"];
NSMutableArray *videos = [[NSMutableArray alloc] initWithCapacity:[videoElements count]];
register int i;
for(i = 0; i < [videoElements count]; i++) {
NSAutoreleasePool *addPool = [[NSAutoreleasePool alloc] init];
YTVideo *vid = [[YTVideo alloc] initWithXMLElement:[videoElements objectAtIndex:i]];
[videos addObject:vid];
[vid release];
[addPool drain];
}
NSArray *retValue = [NSArray arrayWithArray:videos];
[videos release];
return retValue;
}
main()
int main(int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
YTSearchFeed *feed = [[YTSearchFeed alloc] initWithSearch:#"Eminem"];
long long results = [feed videoCount];
NSLog(#"%lld videos for search", results);
long long i = 0;
while(results != 0) {
int length = (results >= 50) ? (50) : (results);
NSArray *videos = [feed videosInRange:NSMakeRange(i, length)];
NSLog(#"L: %d", [videos count]);
int z;
for(z = 0; z < [videos count]; z++, i++) {
YTVideo *vid = [videos objectAtIndex:z];
NSString *title = [vid title];
NSLog(#"%d: %#", i+1, title);
}
results -= length;
}
[pool drain];
return NSApplicationMain(argc, argv);
}
I hope someone can take the time to look at this, and if you need anymore information, just ask.
Thank you in advance,
ief2
EDIT: YTVideo
- (id)initWithXMLElement:(NSXMLElement *)element {
self = [super init];
if(self != nil) {
_XMLElement = [element copy];
}
return self;
}
- (NSString *)title {
if(!_title) {
NSString *str = [[[self XMLElement] firstElementWithName:#"title"] stringValue];
_title = [[str stringByDecodingHTMLEntities] retain];
}
return [[_title copy] autorelease];
}
I get the title (and other video information) only when it's requested. the -stringByDecodingHTMLEntities works fine (Category on NSString).
I've rewritten the code and initialized all instance variables in the -initmethod