开发者

NSArray objectAtIndex is not working. Please help

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];}


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 ... */
  ];
}


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++;
}


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜