iOS Address Book error on ABMultiValueRef - iphone

I'm facing a problem accessing the address book of my iPad 2. In particular I have problems retrieving the email of my contacts. What I want to do is to access the address book, retrieve my contacts and show them in a table view. Everything seems work fine since the name and the surname of the contacts are shown. The problem is with the email property since when I try to retrieve it I get an "EXC_BAD_ACCESS".
The code i wrote to show the tableview record is the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *tableIdentifier = #"tableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
NSUInteger row = [indexPath row];
NSString *firstName = (NSString *)ABRecordCopyValue([contacts objectAtIndex:row], kABPersonFirstNameProperty);
NSString *lastName = (NSString *)ABRecordCopyValue([contacts objectAtIndex:row], kABPersonLastNameProperty);
NSString *name = [[NSString alloc] initWithFormat:#"%# %#", lastName,firstName];
[firstName release];
[lastName release];
cell.textLabel.text = name;
[name release];
NSArray *emails = [[self getEmailForPerson:row] retain];
/*......*/
return cell;
}
While the function to get the email of my contacts is the following:
- (NSArray *)getEmailForPerson:(NSInteger)index{
//Create the array where emails will be stored
NSMutableArray *m = [[[NSMutableArray alloc] init] autorelease];
//Get the email properties
ABMultiValueRef mails = ABRecordCopyValue([self.contacts objectAtIndex:index], kABPersonEmailProperty);
//Iterate in the multi-value properties
for (int i=0; i<ABMultiValueGetCount(mails); i++) {
//Get the email
NSString *mail = (NSString *) ABMultiValueCopyValueAtIndex(mails, i);
//Add the email to the array previously initializated
[m addObject:mail];
[mail release];
}
CFRelease(mails);
return m;
}
When I run the debugger after this statement
ABMultiValueRef mails = ABRecordCopyValue([self.contacts objectAtIndex:index], kABPersonEmailProperty);
mails seems not initialized since its adress is 0x0 but I cannot understand why.
I hope somebody can help me.
Thanks in advance

ABMultiValueRef mails = ABRecordCopyValue([self.contacts objectAtIndex:index], kABPersonEmailProperty);
It works fine in my app.
Check framework & self.contacts.
My app use two frameworks.
#import <AddressBook/AddressBook.h>
#import <AddressBook/ABAddressBook.h>

Related

Access contact image from address book based on name

Err,I have been pulling my hair thinking about a way from quite a few days.I have retrieved all contacts names and placed in an array using dictionary.
What I have is a model class holding a list of names,now I want to search the location of name in contacts list,depending on which I can retrieve the required contact image.
Initially googled and found out an unanswered question not pretty much similar to my requirement,the same can be glanced here
I tried several ways,the below is one way I have implemented:
EDIT
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[self loadReminders];
ReminderClass *reminderToDisplay = [self.remindersArray objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
// Now create the cell to display the reminder data
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
}
tableView.backgroundColor = [UIColor clearColor];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
[dateFormat setDateFormat:kDateFormat];
NSDate *reminderDate = [dateFormat dateFromString:reminderToDisplay.Date];
[dateFormat setDateFormat:kMinDateFormat];
NSString *dateString = [dateFormat stringFromDate:reminderDate];
NSString *valueString = [NSString stringWithFormat:#"%#'s %#",reminderToDisplay.Name,reminderToDisplay.Event];
NSString *onString = [NSString stringWithFormat:#" on %#",dateString];
NSString *reminderDetailsString = [valueString stringByAppendingString:onString];
//Get the contact image based on name index from contact list
ABAddressBookRef addressBook = ABAddressBookCreate( );
CFStringRef reminderName = (CFStringRef)reminderToDisplay.Name;
CFArrayRef allPeople = ABAddressBookCopyPeopleWithName(addressBook, reminderName);
self.contactsList =[[[NSMutableArray alloc]init]autorelease];
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for ( int i = 0; i < nPeople; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
NSString *contactFirstNamePart = (NSString *)ABRecordCopyValue(ref,kABPersonFirstNameProperty);
NSString *contactFirstName = [[[NSString alloc] initWithString:contactFirstNamePart]autorelease];
NSString *contactLastNamePart = (NSString *)ABRecordCopyValue(ref, kABPersonLastNameProperty);
if (contactLastNamePart == nil)
{
self.contactName = contactFirstName;
}
else
{
NSString *contactLastName = [[[NSString alloc] initWithString:contactLastNamePart]autorelease];
NSString *contactLastNameString = [NSString stringWithFormat:#" %#",contactLastName];
self.contactName = [contactFirstName stringByAppendingString:contactLastNameString];
CFRelease(contactLastNamePart);
}
NSDictionary *contactsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.contactName, kContactName, [NSNumber numberWithInt:i], kContactIndex, nil];
[self.contactsList addObject:contactsDictionary];
CFRelease(contactFirstNamePart);
}
NSDictionary *contactsDictionary = [self.contactsList objectAtIndex:indexPath.row];
self.contactName = [contactsDictionary objectForKey:kContactName];
int addressIndex = [[contactsDictionary objectForKey:kContactIndex]integerValue];
ABRecordRef recordReference = CFArrayGetValueAtIndex(allPeople, addressIndex);
if (ABPersonHasImageData(recordReference))
{
NSData *imageData = (NSData *)ABPersonCopyImageData(recordReference);
self.reminderImage = [UIImage imageWithData:imageData];
CFRelease(imageData);
}
CFRelease(allPeople);
CFRelease(addressBook);
UIImage *notificationImage = reminderImage;
if (notificationImage != nil)
{
UIImageView *imageView=[[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)]autorelease];
imageView.backgroundColor=[UIColor clearColor];
[imageView setImage:notificationImage];
cell.accessoryView = imageView;
}
else
{
UIImageView *imageView=[[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)]autorelease];
imageView.backgroundColor=[UIColor clearColor];
UIImage *defaultImage = [UIImage imageNamed:kDefaultImage];
[imageView setImage:defaultImage];
cell.accessoryView = imageView;
}
cell.textLabel.text = reminderDetailsString;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Bad Access Error Screen shot
But I was unable to accomplish the required task.Can any one please guide me.
Thanks all in advance :)
I am sharing the sample code snippet I used in one of my recent app. I have modified to fit it ur requirements and also please note that I have edited this in notepad and may have some typo errors.(Currently I dnt have mac to test it..:P)
Basic idea is to fill the datasource in viewDidLoad method and use that dataSource to update the tableView. Hope this will be an input to solve your problem.
viewDidLoad
contactsToBeAdded=[[NSMutableArray alloc] init];
ABAddressBookRef addressbook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressbook);
CFIndex numPeople = ABAddressBookGetPersonCount(addressbook);
bool hasPhoneNumber = false;
for (int i=0; i < numPeople; i++) {
hasPhoneNumber = false;
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
ABMutableMultiValueRef phonelist = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex numPhones = ABMultiValueGetCount(phonelist);
if(numPhones > 0){
hasPhoneNumber = true;
}
if(hasPhoneNumber){
NSString *firstName=(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName=(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
CFTypeRef ABphone = ABMultiValueCopyValueAtIndex(phonelist, 0);
NSString *personPhone = (NSString *)ABphone;
NSMutableDictionary *dictToAdd = [[[NSMutableDictionary alloc]init]autorelease];
if(firstName != nil && firstName != NULL){
[dictToAdd setObject:firstName forKey:#"firstName"];
CFRelease(firstName);
}
else{
[dictToAdd setObject:#"" forKey:#"firstName"];
}
if(lastName != nil && lastName != NULL){
[dictToAdd setObject:lastName forKey:#"lastName"];
CFRelease(lastName);
}
else{
[dictToAdd setObject:#"" forKey:#"lastName"];
}
if(personPhone != nil && personPhone != NULL){
[dictToAdd setObject:personPhone forKey:#"mobile"];
CFRelease(ABphone);
}
else{
[dictToAdd setObject:#"" forKey:#"mobile"];
}
//Get the first name and last name added to dict and combine it to full name
NSString *firstName = [dictToAdd objectForKey:#"firstName"];
NSString *lastName = [dictToAdd objectForKey:#"lastName"];
NSString *fullName = [firstName stringByAppendingString:lastName];
//Now check whether the full name is same as your reminderToDisplay.Name
if(reminderToDisplay.Name isEqualToString:fullName )
{
CFDataRef imageData = ABPersonCopyImageData(person);
UIImage *image = [UIImage imageWithData:(NSData *)imageData];
if(image != nil && image != NULL){
[dictToAdd setObject:image forKey:#"image"];
CFRelease(imageData);
}
else{
[dictToAdd setObject:[UIImage imageNamed:TEMP_IMG] forKey:#"image"];
}
}
[contactsToBeAdded addObject:dictToAdd];
}
CFRelease(phonelist);
}
CFRelease(allPeople);
CFRelease(addressbook);
[self.tableView reloadData];
numberOfRowsInSection
return contactsToBeAdded.count;
cellForRowAtIndexPath
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSDictionary *contactToAdd;
//This way you can get the data added in viewDidLoad method
contactToAdd = [contactsToBeAdded objectAtIndex:indexPath.row];
NSString *fName = (NSString *)[contactToAdd objectForKey:#"firstName"];
NSString *lName = (NSString *)[contactToAdd objectForKey:#"lastName"];
UIImage *contactImg = (UIImage*)[contactToAdd objectForKey:#"image"];
when you use 'ABAddressBookCopyArrayOfAllPeople' you get an array of persons in the addressbook. I believe they are type of ABPerson.
You can now loop over them list like you are. For each one record call 'ABPersonCopyImageData' and that will give you the image data as a CFDataRef.
And remember CFDataRef is a tool free bridge to NSData.
Just try changing your code like this. You are adding dictionary items to ur contactsList, so get each dictionary and check whether it contains a key matching your reminderToDisplay.Name, if yes then do ur stuff..
for (NSDictionary* dict in contactsList)
{
if(nil != [dict objectForKey:reminderToDisplay.Name])
{
//take image here
}
}
UPDATE:
//This is the reminder's name you want to get contact image
ReminderClass *reminderToDisplay = [self.remindersArray objectAtIndex:indexPath.row];
//Here you are adding your contacts with full name as object and kName as key, but check ur kName here
NSDictionary *contactsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.contactName, kName, [NSNumber numberWithInt:i], kIndex, nil];
[self.contactsList addObject:contactsDictionary];
//I dont think this part is needed in ur code
NSDictionary *contactsDictionary = [self.contactsList objectAtIndex:indexPath.row];
self.contactName = [contactsDictionary objectForKey:kName];
int addressIndex = [[contactsDictionary objectForKey:kIndex]intValue];
Now you have your contact names as a dictionary in contactsList array, iterate the array and check whether the dictionary contains your reminderToDisplay.Name as key.
for (NSDictionary* dict in contactsList)
{
//Please note that your dict contains key as kName and object as contact name.
if(nil != [dict objectForKey:reminderToDisplay.Name])
{
}
}
Also, I feel like you can do this in one single loop, like when you are iterating the addressbook itself, you can check whether the contact name is in your reminderlist and if available then extract image.
Hope this helps..all the best...

customize the ABPersonPickerViewController

How can we customize the ABPeoplePickerNavigationViewController to display FullName in the first line and email,phone number in the next line. This should not happen on a contact select but should default when the ABPickerController view is loaded.
What I want is the regular functionalities of the ABPeoplePicker.. but the contacts display should have additional information explained above.
I think I would have to extend the ABPeoplePickerNavigationController? Any guidance on this would be greatly appreciated?
If you would want the ABPeoplePicker.... and the addressBook to work together dont quite have a solution.Tried the same thing but finally we spun out a new custom indexed table view. Was quite easy.
if you want an indexed tableView created please have a look at
indexed-ui-tableview
you could then change the following method for having information in multiple lines.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [[[content objectAtIndex:indexPath.section] objectForKey:#"rowValues"]
objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[NSString stringWithFormat:#"%# %# %#", #"someone#gmail.com", #"|",#"123456777777777777777"] ;;
return cell;
}
The data generator can be modified to accomodate your model.
the email etc can be obtained from your model.In the snippet above it is hard coded.You could then easily wire in the search functionality.
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
// assigning control back to the main controller
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
NSString *firstname=(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
Usernametf.text=firstname;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
ABMultiValueRef addressProperty = ABRecordCopyValue(person, kABPersonAddressProperty);
NSArray *address = (NSArray *)ABMultiValueCopyArrayOfAllValues(addressProperty);
NSString* phone;
phone = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
phoneNotf.text=phone;
//NSLog(#"%#",phone);
if([address count] > 0)
{
for (NSDictionary *addressDict in address)
{
NSString* countrytf = [addressDict objectForKey:#"Country"];
NSString* streetaddresstf= [addressDict objectForKey:#"Street"];
NSString* citynametf = [addressDict objectForKey:#"City"];
NSString* statenametf = [addressDict objectForKey:#"State"];
NSString* zipcodetf = [addressDict objectForKey:#"ZIP"];
}
CFRelease(addressProperty);
}
// NSMutableDictionary *dict=[[NSMutableDictionary alloc] initWithObjectsAndKeys:firstname,#"FirstName", nil];
//[selectedemailArray addObject:dict];
// NSLog(#"\n array is %#",selectedemailArray);
//[objDatabase insertArray:selectedemailArray forTable:#"EmailList"];
//[objDatabase insertDictionary:dict forTable:#"EmailList"];
// [dict release];
// dict =nil;
// remove the controller
[self dismissModalViewControllerAnimated:YES];
return NO;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
return NO;
}
here is whole code where you can get anything from contacts.
let me know it is working or not...
Happy Coding!!!!

self performSelector:#selector(loadData) withObject:nil - not working

I use:
self performSelector:#selector(loadData) withObject:nil...
it look like working with some command only in "loadData" but the rest are not.
Here is my viewdidload:
- (void)viewDidLoad
{
[super viewDidLoad];
[mActivity startAnimating];
[self performSelector:#selector(loadData) withObject:nil afterDelay:2];
//[mActivity stopAnimating];
}
and here is loadData:
-(void)loadData
{
[mActivity startAnimating];
NSLog(#"Start LoadData");
AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *selectData=[NSString stringWithFormat:#"select * from k_proverb ORDER BY RANDOM()"];
qlite3_stmt *statement;
if(sqlite3_prepare_v2(delegate.db,[selectData UTF8String], -1,&statement,nil)==SQLITE_OK){
NSMutableArray *Alldes_str = [[NSMutableArray alloc] init];
NSMutableArray *Alldes_strAnswer = [[NSMutableArray alloc] init];
while(sqlite3_step(statement)==SQLITE_ROW)
{
NSString *des_strChk= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,3)];
if ([des_strChk isEqualToString:#"1"]){
NSString *des_str= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,4)];
[Alldes_str addObject:des_str];
}
}
Alldes_array = Alldes_str;
Alldes_arrayAnswer = Alldes_strAnswer;
}else{
NSLog(#"ERROR '%s'",sqlite3_errmsg(delegate.db));
}
listOfItems = [[NSMutableArray alloc] init];
NSDictionary *desc = [NSDictionary dictionaryWithObject:
Alldes_array forKey:#"description"];
[listOfItems addObject:desc];
//[mActivity stopAnimating];
NSLog(#"Finish loaData");}
it give me only printing 2 line, but did not load my Data to the table, but if I copy all the code from inside "loadData" and past in "viewDidLoad", it load the data to the table.
Any advice or help please.
A few things: If you see any NSLog output at all, then the performSelector is succeeding. You should change the title of your question.
If you are trying to load data into a table, the method should end with telling the UITableView to reloadData (or a more elaborate load using begin/end updates).
If the listOfItems is the data supporting the table, get this working first by hard-coding something like this:
-(void)loadData {
listOfItems = [NSArray arrayWithObjects:#"test1", #"test2", nil];
[self.tableView reloadData];
return;
// keep all of the code you wrote here. it won't run until you remove the return
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *string = [listOfItems objectAtIndex:indexPath.row];
cell.textLabel.text = string;
return cell;
// keep all of the code you probably wrote for this method here.
// as above, get this simple thing running first, then move forward
}
Good luck!

adding contacts from iphone contacts to my table view

i want add contacts to a table view and i am able to add all contacts to my table view by using the following code but i want to add selected contacts from phone contacts to my table view .
can any one please help me how to change the following code .....
-(void)loadTableSource{
contactsToBeAdded=[[NSMutableArray alloc] init];
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
//CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook,CFSTR("Rajesh"));
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for(int i=0;i<nPeople;i++){
ABRecordRef person=CFArrayGetValueAtIndex(people, i);
NSString *firstName=(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName=(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *organization=(NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);
if(!firstName) firstName=#"";
if(!lastName) lastName=#"";
if(!organization) organization=#"";
NSDictionary *curContact=[NSDictionary dictionaryWithObjectsAndKeys:(NSString *)firstName,#"firstName",lastName,#"lastName",organization,#"organization",nil];
[contactsToBeAdded addObject:curContact];
}
CFRelease(people);
CFRelease(addressBook);
[self setTableSource:contactsToBeAdded];
[contactsToBeAdded release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdent=[NSString stringWithFormat:#"c%i",indexPath.row];
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdent];
if(cell==nil){
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
NSDictionary *dict=[tableSource objectAtIndex:indexPath.row];
UILabel *lab=[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 310, 40)];
NSLog(#"lable:%#",tableSource);
[lab setNumberOfLines:2];
[lab setText:[[NSString stringWithFormat:#"%# %#\n%#",(NSString *)[dict objectForKey:#"firstName"],[dict objectForKey:#"last_name"],[dict objectForKey:#"organization"]] stringByReplacingOccurrencesOfString:#"(null)" withString:#""]];
[cell.contentView addSubview:lab];
[lab release];
}
return cell;
}
you didn't specify on what condition you are going to iterate contacts..
just check using if () before adding curContact dictionay to contactsToBeAdded array
if([firstName hasPrefix:#"A"])
{
NSDictionary *curContact=[NSDictionary dictionaryWithObjectsAndKeys:(NSString *)firstName,#"firstName",lastName,#"lastName",organization,#"organization",nil];
[contactsToBeAdded addObject:curContact];
}
i think this will help..!!

Iphone xcode simulator crashes when I move up and down on a table row

I can't get my head round this. When the page loads, everything works fine - I can drill up and down, however 'stream' (in the position I have highlighted below) becomes not equal to anything when I pull up and down on the tableview. But the error is only sometimes. Normally it returns key/pairs.
If know one can understand above how to you test for // (int)[$VAR count]} key/value pairs
in a NSMutableDictionary object
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *FirstLevelCell = #"FirstLevelCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease];
}
NSInteger row = [indexPath row];
//NSDictionary *stream = (NSDictionary *) [dataList objectAtIndex:row];
NSString *level = self.atLevel;
if([level isEqualToString:#"level2"])
{
NSMutableDictionary *stream = [[NSMutableArray alloc] init];
stream = (NSMutableDictionary *) [dataList objectAtIndex:row];
// stream value is (int)[$VAR count]} key/value pairs
if ([stream valueForKey:#"title"] )
{
cell.textLabel.text = [stream valueForKey:#"title"];
cell.textLabel.numberOfLines = 2;
cell.textLabel.font =[UIFont systemFontOfSize:10];
NSString *detailText = [stream valueForKey:#"created"];
cell.detailTextLabel.numberOfLines = 2;
cell.detailTextLabel.font= [UIFont systemFontOfSize:9];
cell.detailTextLabel.text = detailText;
NSString *str = #"http://www.mywebsite.co.uk/images/stories/Cimex.jpg";
NSData *imageURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:str]];
UIImage *newsImage = [[UIImage alloc] initWithData:imageURL];
cell.imageView.image = newsImage;
[stream release];
}
}
else
{
cell.textLabel.text = [dataList objectAtIndex:row];
}
return cell;
}
Thanks for your time
You are both leaking and over-releasing the stream dictionary:
NSMutableDictionary *stream = [[NSMutableArray alloc] init]; // <-- Create a new dictionary
stream = (NSMutableDictionary *) [dataList objectAtIndex:row]; // <-- Overwrite the reference with another dictionary. Previous dictionary is lost...
...
[stream release]; // <-- You are releasing an object you don't have the ownership.
You should remove the dictionary creation as it is useless and the release as you don't own the object.
I didn't really understand the question, but...
You can test for number of values in a dictionary by:
if ([[myDictionary allKeys] count] == someNumber) {
// do something...
}