numberofSectionInTablesView/NumberofRowsInsection - iphone

I am having trouble returning the numberofSections/numberofRows. I have a dictionary full of data
-(void)setUpContacts{
//set up the contacts
NSString *string = #"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
letterArray = [[NSMutableDictionary alloc]init];
Contacts *contact = [[Contacts alloc]init];
contactNumbers = [contact phoneNumbers];
for (NSDictionary* info in contactNumbers) {
firstLetter = [info objectForKey:#"lastName"];
index = #"_";
if([firstLetter length] > 0){
firstLetter =[firstLetter substringToIndex:1];
firstLetter= [firstLetter capitalizedString];
NSRange range = [string rangeOfString:firstLetter];
if(range.length >= 0){
index= firstLetter;
}
}
if(![letterArray objectForKey:index]){
[letterArray setValue:[NSMutableArray array] forKey:index];
}
[[letterArray objectForKey:index] addObject:info];
}
NSLog(#"%#",letterArray);}
and it NSLogs with this
C = (
{
firstName = Alex;
kind = Mobile;
lastName = Chang;
name = "Alex Chang (Mobile)";
number = "(555) 555-5555";
},
{
firstName = YuYu;
kind = Mobile;
lastName = Chen;
name = "YuYu Chen (Mobile)";
number = "(408) 112-2334";
},
{
firstName = Chris;
kind = Mobile;
lastName = Choi;
name = "Chris Choi (Mobile)";
number = "(999) 999-9999";
},
{
firstName = Kevin;
kind = Mobile;
lastName = Chung;
name = "Kevin Chung (Mobile)";
number = "1 (231) 241-2312";
}
);
H = (
{
firstName = Danny;
kind = Mobile;
lastName = Huang;
name = "Danny Huang (Mobile)";
number = "(408) 599-9770";
},
{
firstName = Ice;
kind = Mobile;
lastName = Huang;
name = "Ice Huang (Mobile)";
number = "(408) 444-4444";
}
);
K = (
{
firstName = Will;
kind = Mobile;
lastName = King;
name = "Will King (Mobile)";
number = "(415) 123-4567";
}
);
L = (
{
firstName = "";
kind = iPhone;
lastName = LastName;
name = " LastName (iPhone)";
number = "(408) 123-2555";
},
{
firstName = david;
kind = Mobile;
lastName = lub;
name = "david lub (Mobile)";
number = "(666) 666-1111";
}
);
for the -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; method
how do i retrieve the number of sections for index;
and for the -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; method, how do i retreive the number of rows in the section?
EDIT
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *array = [letterArray mutableCopy];
return [[array objectAtIndex:section]count];}
and it gives me this error: -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8434cc0
Thank you in advance

for -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView you should do something like this:
return [letterArray count];
for -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section you should do something like this:
NSArray *array = letterArray;
return [[array objectAtIndex:section] count];
EDIT
Try this:
-(NSArray *)getLetterArrayCount {
NSMutableArray *countArray = [[NSMutableArray alloc] init];
NSNumber *subCount;
for (id key_main in letterArray) {
subCount = 0;
for (id key_sub in [letterArray objectForKey:key_main]) {
subCount = [NSNumber numberWithInt:[subCount intValue] + 1];
}
[countArray addObject:subCount];
}
return countArray;
}
You should call it once and save it to a class variable and call it like this for sections:
[countArray count];
And for the rows in a section like this:
[[countArray objectAtIndex:section] intValue];

letterArray = [[NSMutableDictionary alloc]init];
indexArray = [[NSMutableArray alloc] init];
[letterArray setValue:[NSMutableArray array] forKey:index];
[indexArray addObject:index];
for -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView :
return [letterArray count];
for -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section :
return [[letterArray objectForKey:[indexArray objectAtIndex:section]] count];
I think it will be helpful to you.

in ur question the letterarray is an NSDictionary. it wont show You the objectAtIndex option. try to change the NSMutableDictionary into NSMutableArray. then you will get the option.
and
return(letterarray.count)
NSMutableArray * arr = [[NSMutableArray alloc]init];
for (NSArray *ar in letterarray){
[arr addObject:ar];
}
Return this arr.count
i am Learning.,.,.

Related

How to filter array of NSDictionary for key separated by comma

I have an array of NSDictionary.NSDictionary has a key named as multiple_image key that contain string separated by ,.
I want set of array that contain 123.png for multiple_images key.
Can some one show me how to do this using NSPredicate or without predicate.
//Array
{
Image = "<UIImage: 0xf72df30>";
active = yes;
"admin_id" = 169;
"category_id" = 32;
"chef_id" = 175;
descr = "Cool tea to cool down the mind.";
id = 110;
"multiple_images" = "Jellyfish.jpg,345.png";
name = "Southern Sweet Ice Tea";
price = 160;
rating = 3;
selected = 0;
"subcat_id" = 23;
"tag_id" = 45;
"tax_id" = 10;
"tax_value" = "12.00";
},
{
Image = "<UIImage: 0xf72ebd0>";
active = yes;
"admin_id" = 169;
"category_id" = 31;
"chef_id" = 175;
descr = "Ingredients are almonds or cashews. No hydrogenated stuff, no extra weirdo ingredients";
id = 107;
"multiple_images" = "Jellyfish.jpg,123.png";
name = "Butter Chicken";
price = 300;
rating = 3;
selected = 0;
"subcat_id" = 24;
"tag_id" = 43;
"tax_id" = 9;
"tax_value" = "0.00";
},
{
Image = "<UIImage: 0xf72f870>";
active = yes;
"admin_id" = 169;
"category_id" = 31;
"chef_id" = 173;
descr = "Raw vegetables including carrots, cucumbers.";
id = 100;
"multiple_images" = "Jellyfish.jpg,shake.png,";
name = Salads;
price = 50;
rating = 3;
selected = 0;
"subcat_id" = 22;
"tag_id" = 44;
"tax_id" = 9;
"tax_value" = "0.00";
}
Using predicates,
[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"multiple_images CONTAINS '123'"]];
Using predicates, but with blocks
NSArray *filtered = [test filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:
^BOOL(NSDictionary *evaluatedObject, NSDictionary * bindings) {
NSString *key = #"123";
return ([evaluatedObject[#"multiple_images"] rangeOfString:key].location != NSNotFound);
}]];
Try
NSDictionary *item = mainArray[0];
NSString *imagesString = item[#"multiple_images"];
NSArray *images = [imagesString componentsSeparatedByString:#","];
Now you can use the images
Try this code
for (int i=0; i<[mainArray count]; i++) {
NSDictionary *item = [mainArray objectAtIndex:i];
NSString *imagesString = item[#"multiple_images"];
NSArray *images = [imagesString componentsSeparatedByString:#","];
for (int j=0;j<[images count]; j++) {
if ([[images objectAtIndex:j] isEqualToString:#"123.png"]) {
///Your Required code;
}
}
}
So you want to save the dictionary that has 123.png.
maybe you can try something like this:
NSMutableArray *arr = [NSMutableArray new];// your array of objects(dictionaries)
__block NSMutableArray *images123 = [NSMutableArray new];
[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSMutableDictionary *dictionary = (NSMutableDictionary *)obj;
if([[dictionary allKeys]containsObject:#"multiple_images"]){
NSString *imageList = (NSString *)[dictionary objectForKey:#"multiple_images"];
NSArray *arrayImages = [imageList componentsSeparatedByString:#","];
if([arrayImages containsObject:#"123.png"]){
[images123 addObject:dictionary];
}
}
}];

iOS Dynamic Button tagging in UITableViewCell does not detect correct row in table

I'm experiencing a major problem now as my renewal button does not detect the row that is being selected at all because of the recycling! Tried tagging the button but it doesn't detect the correct button either. Please help! I've been stuck on this for several days. If not, feel free to recommend other better solutions other than tagging. I parse in sections and rows via NSDictionary which looks like this:
"15/08/2012" = (
{
BIBNo = 290408;
date = "15/08/2012";
itemSequence = 000010;
name = "Sams teach yourself iPhone application development in 24 hours / John Ray.";
noOfRenewal = 3;
number = 000290408;
status = "Normal Loan";
time = "24:00";
type = Loans;
}
);
"16/08/2012" = (
{
BIBNo = 187883;
date = "16/08/2012";
itemSequence = 000010;
name = "Positive thinking / Susan Quilliam.";
noOfRenewal = 3;
number = 000187899;
status = "Normal Loan";
time = "24:00";
type = Loans;
},
{
BIBNo = 161816;
date = "16/08/2012";
itemSequence = 000010;
name = "Malaysian Q & As / compiled by Survey & Interview Department ; illustrations by Exodus.";
noOfRenewal = 1;
number = 000161817;
status = "Normal Loan";
time = "24:00";
type = Loans;
}
);
"17/08/2012" = (
{
BIBNo = 187882;
date = "17/08/2012";
itemSequence = 000010;
name = "Increasing confidence / Philippa Davies.";
noOfRenewal = 1;
number = 000187907;
status = "Normal Loan";
time = "24:00";
type = Loans;
},
{
BIBNo = 244441;
date = "17/08/2012";
itemSequence = 000010;
name = "Coach : lessons on the game of life / Michael Lewis.";
noOfRenewal = 2;
number = 000244441;
status = "Normal Loan";
time = "24:00";
type = Loans;
},
{
BIBNo = 291054;
date = "17/08/2012";
itemSequence = 000010;
name = "iPhone application development for IOS 4 / Duncan Campbell.";
noOfRenewal = 3;
number = 000291054;
status = "Normal Loan";
time = "24:00";
type = Loans;
}
);
}
My code for the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UserCustomCell *cell = (UserCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.bookTitle.frame = CGRectMake(12, 0, 550, 40);
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"UserCustomCell" owner:self options:nil];
cell = userCustomCell;
self.userCustomCell = nil;
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
cell.bookTitle.frame = CGRectMake(12, 0, 550, 40);
cell.renewButton.frame = CGRectMake(600, 14, 68, 24);
}
[cell.renewButton useBlackActionSheetStyle];
dataSource = [[NSMutableDictionary alloc] init]; // This would need to be an ivar
for (NSDictionary *rawItem in myArray) {
NSString *date = [rawItem objectForKey:#"date"]; // Store in the dictionary using the data as the key
NSMutableArray *section = [dataSource objectForKey:date]; // Grab the section that corresponds to the date
if (!section) { // If there is no section then create one and add it to the dataSource
section = [[NSMutableArray alloc] init];
[dataSource setObject:section forKey:date];
}
[section addObject:rawItem]; // add your object
}
self.dataSource = dataSource;
NSLog(#"Data Source Dictionary: %#", dataSource);
NSArray *sections =[[dataSource allKeys] sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
NSString *sectionTitle = [sections objectAtIndex:indexPath.section];
NSArray *items = [dataSource objectForKey:sectionTitle];
NSDictionary *dict = [items objectAtIndex:indexPath.row];
cell.bookTitle.text = [dict objectForKey:#"name"];
cell.detail.text = [NSString stringWithFormat:#"Due Date: %# Due Time: %#",
[dict objectForKey:#"date"], [dict objectForKey:#"time"]];
cell.renewButton.tag = indexPath.row;
return cell;
}
How it looks like now:
Thanks much!
I've resolved the issue by adding index into my array which was recommended by #Phillip which looks like this:
for (int a = 0; a < myArray.count; a++){
NSMutableDictionary *mDict = [myArray objectAtIndex:a];
NSNumber *myIndex = [NSNumber numberWithInt:a];
[mDict setValue:myIndex forKey:#"index"];
NSLog(#"added index: %#", myArray);
}
self.myArray = myArray;
before i set the tag in my button so that it would detect the correct item.
NSString *index = [dict objectForKey:#"index"];
NSInteger index_tag = (#"%d", [index intValue]);
NSLog(#"%d", index_tag);
cell.renewButton.tag = index_tag;
Hope it may help anyone with the same problem as me :)

ordering of NSArray

I am trying to set up my TableView, and I am having trouble. The values of my NSArray is different from each other. I know this is hard to understand, but i will try my best to explain.
For example
-(void)setUpContacts{
//set up the contacts
NSString *string = #"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
letterArray = [[NSMutableDictionary alloc]init];
Contacts *contact = [[Contacts alloc]init];
contactNumbers = [contact phoneNumbers];
for (NSDictionary* info in contactNumbers) {
firstLetter = [info objectForKey:#"lastName"];
index = #"_";
if([firstLetter length] > 0){
firstLetter =[firstLetter substringToIndex:1];
firstLetter= [firstLetter capitalizedString];
NSRange range = [string rangeOfString:firstLetter];
if(range.length >= 0){
index= firstLetter;
}
}
if(![letterArray objectForKey:index]){
[letterArray setValue:[NSMutableArray array] forKey:index];
}
[[letterArray objectForKey:index] addObject:info];
}NSLog(#"%#",letterArray);}
-(NSArray *)getLetterArraycount{
NSMutableArray *countArray = [[NSMutableArray alloc]init];
NSNumber *subCount;
for(id key_main in letterArray){
subCount = 0;
for(id key_sub in [letterArray objectForKey:key_main]){
subCount = [NSNumber numberWithInt:[subCount intValue]+1];
}
[countArray addObject:subCount];
}
return countArray;
}
- (void)viewDidLoad{
[super viewDidLoad];
[self setUpContacts];
countArrays = [self getLetterArraycount];
indexPaths = [[NSMutableDictionary alloc]init];
NSArray *temp = [letterArray allKeys];
NSLog(#"%#",temp);
NSLog(#"%#",countArrays);
//set up the tableView
self.myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
self.title = #"Select Friends";
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:myTableView];}
and it logs like this in the Console
C = (
{
firstName = Alex;
kind = Mobile;
lastName = Chang;
name = "Alex Chang (Mobile)";
number = "(555) 555-5555";
},
{
firstName = YuYu;
kind = Mobile;
lastName = Chen;
name = "YuYu Chen (Mobile)";
number = "(408) 112-2334";
},
{
firstName = Chris;
kind = Mobile;
lastName = Choi;
name = "Chris Choi (Mobile)";
number = "(999) 999-9999";
},
{
firstName = Kevin;
kind = Mobile;
lastName = Chung;
name = "Kevin Chung (Mobile)";
number = "1 (231) 241-2312";
}
);
H = (
{
firstName = Danny;
kind = Mobile;
lastName = Huang;
name = "Danny Huang (Mobile)";
number = "(408) 599-9770";
},
{
firstName = Ice;
kind = Mobile;
lastName = Huang;
name = "Ice Huang (Mobile)";
number = "(408) 444-4444";
}
);
K = (
{
firstName = Will;
kind = Mobile;
lastName = King;
name = "Will King (Mobile)";
number = "(415) 123-4567";
}
);
L = (
{
firstName = "";
kind = iPhone;
lastName = LastName;
name = " LastName (iPhone)";
number = "(408) 123-2555";
},
{
firstName = david;
kind = Mobile;
lastName = lub;
name = "david lub (Mobile)";
number = "(666) 666-1111";
}
);
P = (
{
firstName = Jennifer;
kind = Mobile;
lastName = Pie;
name = "Jennifer Pie (Mobile)";
number = "(666) 666-6666";
}
);
S = (
{
firstName = Martin;
kind = Mobile;
lastName = Shen;
name = "Martin Shen (Mobile)";
number = "(415) 333-1234";
},
{
firstName = Francis;
kind = Mobile;
lastName = Shung;
name = "Francis Shung (Mobile)";
number = "(408) 345-6789";
}
);
V = (
{
firstName = Brian;
kind = Mobile;
lastName = Vu;
name = "Brian Vu (Mobile)";
number = "(408) 234-5678";
}
);
Y = (
{
firstName = Qiu;
kind = Mobile;
lastName = Yang;
name = "Qiu Yang (Mobile)";
number = "(408) 567-8901";
}
);
Z = (
{
firstName = Joy;
kind = Mobile;
lastName = Zhou;
name = "Joy Zhou (Mobile)";
number = "(408) 765-4321";
}
);
"_" = (
{
firstName = FirstName;
kind = Mobile;
lastName = "";
name = "FirstName (Mobile)";
number = "1 (408) 234-5141";
}
);
}
2012-04-26 16:14:05.195 UpOut[2693:15803] (
L,
Y,
V,
"_",
Z,
S,
K,
C,
P,
H
MY Question is why doesnt it log "C" first, but instead it Logged "L"
Sounds like you need to learn how to use "NSSortDescriptor" (I've linked the documentation for you).
Here is a related question that might have some useful sample code you can refer to. The key you would want to sort on, in your case, would be the lastName value of each NSDictionary entry.
Alphabetical answers consistently get more upvotes:
NSArray *sortedArray = [countArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];

Multidimensional array issues

I am creating a multidimensional array for sections / rows based on json data from our API. Looking at the logs the adding rows and sections look good but when I nslog the sections it only shows a bunch of the last object. It seems like the rows are not being added to sections.
What am I doing wrong to make the last API object show in the sections x times? x represents the json count. self.appointments is an NSArray *
According to objective-c multi-dimensional array it should work.
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *sections = [[NSMutableArray alloc] init];
NSMutableArray *rows = [[NSMutableArray alloc] init];
NSString *lastDate = nil;
for (NSDictionary *dict in [json objectForKey:#"data"]) {
NSString *date = [dict objectForKey:#"date"];
NSLog(#"Dates: %# - %#", date, lastDate);
if (date != lastDate) {
if (lastDate == nil) {
NSLog(#"Adding Row 1");
[rows addObject:dict];
lastDate = date;
} else {
NSLog(#"Adding Section 1");
NSLog(#"Adding #rows %i",[rows count]);
[sections addObject:rows];
[rows removeAllObjects];
NSLog(#"Adding Row 2 %#",[dict objectForKey:#"start_time"]);
[rows addObject:dict];
lastDate = date;
}
} else {
NSLog(#"Adding Row 3");
[rows addObject:dict];
}
}
if (rows) {
NSLog(#"Adding Section 2");
NSLog(#"Adding #rows %i",[rows count]);
[sections addObject:rows];
}
NSLog(#"Sections: %#", sections);
self.appointments = [sections mutableCopy]; //I have also tried self.appointments = sections
sections = nil;
rows = nil;
Logs show
Sections: (
(
{
abbrev = "";
account = "";
"addOnService_id" = "";
alert = "";
"appt_id" = 1839384;
"appt_id_unique" = 1839384;
"appt_status_description" = "";
"appt_status_type" = "";
"c_id" = 47;
cost = "0.00";
"coupon_id" = "";
"creation_emp_id" = 2288;
"creation_timestamp" = 201111040717;
"customer_id" = 0;
"customer_notes" = "";
"customer_package_id" = "";
date = 20121228;
"employee_id" = 2288;
"employee_notes" = "";
employer = "";
"end_time" = 570;
"first_name" = "";
"history_id" = 1830959;
key = 134;
"last_emp_id" = 2288;
"last_name" = "";
"last_timestamp" = 201111040717;
"lead_description" = "";
"link_id" = 0;
"location_name" = "Telephonic Setup/Training";
"make_id" = "";
"middle_name" = "";
"model_id" = "";
"model_year" = "";
name = "My Name ";
odometer = "";
"other_vehicle" = "";
"package_name" = "";
"payment_type_description" = "";
"payment_type_id" = "";
"pet_id" = "";
"po_number" = "";
reason = "B.O.B";
"rebook_id" = "";
"recur_id" = 20954;
"rep_id" = "";
"room_id" = 0;
"room_name" = "";
service = "";
"service_id" = 0;
"service_time_description" = "";
spots = 1;
"staff_screen_name" = "John Smith";
"staff_type_id" = 0;
"start_time" = 540;
"status_id" = 0;
tip = "";
"type_id" = 8;
vin = "";
}
),
(
{
abbrev = "";
account = "";
"addOnService_id" = "";
alert = "";
"appt_id" = 1839384;
"appt_id_unique" = 1839384;
"appt_status_description" = "";
"appt_status_type" = "";
"c_id" = 47;
cost = "0.00";
"coupon_id" = "";
"creation_emp_id" = 2288;
"creation_timestamp" = 201111040717;
"customer_id" = 0;
"customer_notes" = "";
"customer_package_id" = "";
date = 20121228;
"employee_id" = 2288;
"employee_notes" = "";
employer = "";
"end_time" = 570;
"first_name" = "";
"history_id" = 1830959;
key = 134;
"last_emp_id" = 2288;
"last_name" = "";
"last_timestamp" = 201111040717;
"lead_description" = "";
"link_id" = 0;
"location_name" = "Telephonic Setup/Training";
"make_id" = "";
"middle_name" = "";
"model_id" = "";
"model_year" = "";
name = "My Name ";
odometer = "";
"other_vehicle" = "";
"package_name" = "";
"payment_type_description" = "";
"payment_type_id" = "";
"pet_id" = "";
"po_number" = "";
reason = "B.O.B";
"rebook_id" = "";
"recur_id" = 20954;
"rep_id" = "";
"room_id" = 0;
"room_name" = "";
service = "";
"service_id" = 0;
"service_time_description" = "";
spots = 1;
"staff_screen_name" = "John Smith";
"staff_type_id" = 0;
"start_time" = 540;
"status_id" = 0;
tip = "";
"type_id" = 8;
vin = "";
}
), ... over and over again.
With:
[sections addObject:rows];
[rows removeAllObjects];
[rows addObject:dict];
you keep adding the same rows object not a new one and keep changing the contents so the last contents display each time.
Try:
[sections addObject:[rows mutableCopy]];
[rows removeAllObjects];
[rows addObject:dict];
That way a new row is added each time.
Perhaps better yet:
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *sections = [NSMutableArray array];
NSMutableArray *rows = [NSMutableArray array];
NSString *lastDate = nil;
for (NSDictionary *dict in [json objectForKey:#"data"]) {
NSString *date = [dict objectForKey:#"date"];
if ([date isEqualToString:lastDate] == NO) {
if (lastDate == nil) {
[rows addObject:dict];
lastDate = date;
} else {
[sections addObject:rows];
rows = [NSMutableArray array];
[rows addObject:dict];
lastDate = date;
}
[rows addObject:dict];
}
}
if (rows) {
[sections addObject:rows];
}
self.appointments = sections;
Note that assigning 0 to an object does not release it, release does. But if you use convenience methods to create objects then they are autoreleased and no further action is necessary to release them.
Also note that when comparing strings one needs to use the isEqualToString: method to compare the contents, just using the = or != only compares the address of the strings.

How to determine the type of an address in AddressBook FrameWork (iOS 4.2)

I´ve one big problem... I plan to write an app that deals with the users addressbook and it´s addresses. Everything´s fine - except the fact that I´m not able to determine whether the addesse´s type is "work", "home" or "other".
Does anybody know how to get the label for home, work and other?
Thanks in advance
Boris
This is the function I´m using at the moment:
+ (void)testing {
//Get the addressbook
ABAddressBookRef _addressBookRef = ABAddressBookCreate ();
//Fetch all contacts
NSArray* allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(_addressBookRef);
//Walk the contacts
for (id record in allPeople) {
//Get the contact´s id
NSInteger recordId = ABRecordGetRecordID((ABRecordRef)record);
//Get the contact´s name and company
NSString* recordName = (NSString *)ABRecordCopyCompositeName((ABRecordRef)record);
NSString* recordCompany = (NSString *)ABRecordCopyValue((ABRecordRef)record, kABPersonOrganizationProperty);
//Get the contact´s addresses
CFTypeRef adressesReference = ABRecordCopyValue((ABRecordRef)record, kABPersonAddressProperty);
NSArray *adressesArray = (NSArray *)ABMultiValueCopyArrayOfAllValues(adressesReference);
CFRelease(adressesReference);
NSLog(#"ID: %d", recordId);
NSLog(#"Name: %#", recordName);
NSLog(#"Firma: %#", recordCompany);
for (NSString *adress in adressesArray) {
NSLog(#"Adresse: %#", adress);
}
[adressesArray release];
}
CFRelease(_addressBookRef);
[allPeople release];
NSLog(#"\n");
}
And here´s the log output:
ID: 1
Name: The first user
Firma: (null)
Adresse: {
City = Reutlingen;
Country = Germany;
CountryCode = de;
Street = "some street";
ZIP = 23456;
}
Adresse: {
City = Reutlingen;
Country = Germany;
CountryCode = de;
State = BW;
Street = "Street number 2";
ZIP = 98765;
}
ID: 2
Name: The second contact
Firma: Firma
Adresse: {
Country = "United States";
CountryCode = us;
Street = Test;
}
here is how you get the address book values extracted:
ABMultiValueRef addresses = ABRecordCopyValue(ref, kABPersonAddressProperty);
for (CFIndex j = 0; j<ABMultiValueGetCount(addresses);j++){
CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(addresses, j);
CFStringRef typeTmp = ABMultiValueCopyLabelAtIndex(addreses, j);
CFStringRef labeltype = ABAddressBookCopyLocalizedLabel(typeTmp);
NSString *street = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStreetKey) copy];
NSString *city = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCityKey) copy];
NSString *state = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStateKey) copy];
NSString *zip = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressZIPKey) copy];
NSString *country = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCountryKey) copy];
[street release];
[city release];
[state release];
[zip release];
[country release];
CFRelease(dict);
CFRelease(type);
CFRelease(typeTmp);
}
CFRelease(addresses);
the label type is what you are looking for.
good luck
shani