views:

48

answers:

3
NSArray* address = [NSArray arrayWithArray:[detailItem addressArray]];
NSLog(@"address = %@", address);
NSString* addressToString = @"";
int arrayCount = [address count];
for (int i = 0; i < arrayCount; i++) {
    addressToString = [addressToString stringByAppendingString:[address objectAtIndex:i]];
    if (i == arrayCount -1) {
        addressToString = [addressToString stringByAppendingString:@""];
    } else {
        addressToString = [addressToString stringByAppendingString:@", "];
    }       
}

address is an NSArray that holds an address

2010-06-23 09:05:19.346 iPhoneExample[1093:207] address = (
        {
        City = "Cupertino";
        Country = "United States";
        CountryCode = us;
        State = CA;
        Street = "1 Infinite Loop";
        ZIP = 95014;
    }
)

I'm trying to go thru the array and create a CSV string so it would look like

Cupertino, "United States", us, CA, "1 Infinite Loop", 95014

However, I keep crashing on

addressToString = [addressToString stringByAppendingString:@", "];

Message I get is

 *** -[NSCFDictionary stringByAppendingString:]: unrecognized selector sent to instance 0x1c2f10

UPDATED: detailItem is an object of type ABContact (custom class).

ABContact has a property called addressArray

@property (nonatomic, readonly) NSArray *addressArray;

the definition of my addressArray is

- (NSArray *) addressArray {return [self arrayForProperty:kABPersonAddressProperty];}
+3  A: 

Your "address" is an NSArray of NSDictionary, not an NSArray of NSArray.

To get the values of the dictionary as an array, you can use

[theDictionary allValues]

but there is no guarantee on the order. And I think what you actually need is:

NSMutableString* addressToString = [NSMutableString string];  // use mutable string!
for (NSDictionary* item in address) {     // use fast enumeration!
  [addressToString appendFormat:@"%@, \"%@\", %@, %@, \"%@\", %@\n",
   [item objectForKey:@"City"], 
   /* etc ... */
  ];
}
KennyTM
+1 this is much more correct than @huntaub's answer
Dave DeLong
Cocoa Dev
@orangecl4now an `NSMutableString` *is* an `NSString`, and anywhere you can use an `NSString` you can use an `NSMutableString`
Dave DeLong
@orange: If the City doesn't exist, `-objectForKey:` will return `nil`.
KennyTM
orangecl4now: Google has no opinion of NSMutableString; indeed, the class of objects you pass doesn't matter. Passing `nil` to a `%@` specifier will cause the output to have “(null)” there, so `objectForKey:` returning `nil` wasn't the problem. Something else is wrong with your code to generate the URL.
Peter Hosey
+1  A: 

This:

2010-06-23 09:05:19.346 iPhoneExample[1093:207] address = (
    {
    City = "Cupertino";
    Country = "United States";
    CountryCode = us;
    State = CA;
    Street = "1 Infinite Loop";
    ZIP = 95014;
}
)

Is a NSDictionary. You will want to access its members with [dictionary objectForKey:'City']

So, your updated code should read:

NSDictionary* address = [detailItem addressArray];
NSLog(@"address = %@", address);
NSString* addressToString = @"";
int counter = 0;
for (id object in myDictionary) {
  if (counter != 0)
    addressToString = [addressToString stringByAppendingString:@","];
  addressToString = [addressToString stringByAppendingString:object];   
  counter++;
}
huntaub
The only problem with this is that the dictionary `objectEnumerator` doesn't necessarily return things in any order, since a dictionary itself doesn't maintain order. Plus, you should use the `for..in` syntax. It's simpler. Plus, you never incremented `counter`. I've edited the code for you. :) Also as an aside, this is going to create a *lot* of extra autoreleased objects. I'd use an `NSMutableString` instead.
Dave DeLong
Thanks! I forgot to add the counter increment... :)I knew about the issues with ordering and autoreleased objects, but I wanted to stay as close to the OP's original code as possible.
huntaub
huntaub: No, `address` is an NSArray instance (described as `(…)`). Its sole element (described as `{…}`) is an NSDictionary.
Peter Hosey
A: 

If you could change your addressArray method to actually return an array instead of a dictionary, then you could do:

NSString * addressString = [[detailItem addressArray] componentsJoinedByString:@","];

And that's it...

Dave DeLong