I query and get a NSSet which contains customer address from web. Since I'm new to objective c development I don't know how to get country,zip code etc from that set.So I followed Objective-C How to print NSSet on one line (no trailing comma / space) but my output is in the form of object "0x7f99997b7a50". How can I print all the strings in the set? Thanks in advance.
I tried like this
NSArray *ar = [customer.addresses allObjects];
for (int i = 0; i<ar.count; i++)
{
NSLog(@"arr %@",ar[i]);
}
But the output is arr:
<BUYAddress: 0x7fd451f6e050>
If you have a custom object, you may need to override description
Without overriding:
-(void) testCustomObjects
{
CustomObject *co1 = [[CustomObject alloc] init];
co1.name = @"James Webster";
co1.jobTitle = @"Code Monkey";
CustomObject *co2 = [[CustomObject alloc] init];
co2.name = @"Holly T Canine";
co2.jobTitle = @"Pet Dog";
NSSet *set = [NSSet setWithObjects:co1, co2, nil];
NSLog(@"%@", [set allObjects]);
}
produces:
2016-12-02 11:45:55.342 Playground[95359:4188387] (
"<CustomObject: 0x600000037a20>",
"<CustomObject: 0x60000003ae20>"
)
However, if I override the description
method in my CustomObject
class:
-(NSString*) description
{
return [NSString stringWithFormat:@"%@ (%@)", self.name, self.jobTitle];
}
I get the following:
(
"Holly T Canine (Pet Dog)",
"James Webster (Code Monkey)"
)
If for whatever reason, you can't add a description method, you'd just have to access the relevant parts of the object; something like the following:
NSArray *ar = [customer.addresses allObjects];
for (int i = 0; i<ar.count; i++)
{
NSLog(@"arr %@ (%@)",ar[i].name, ar[i].address);
}
I've had a little look at the library you're using. Try the following:
for (BUYAddress *address in customer.addresses)
{
NSLog(@"Address: %@, %@, %@", address.address1, address.address2, address.city);
}
Consider NSSet below,
NSSet *theNSSet = [NSSet setWithObjects:@"Chennai",@"Mumbai",@"Delhi", nil];
Convert it into NSArray using
NSArray *array = [theNSSet allObjects]; // theNSSet is replaced with your NSSet id
Then print it like
NSLog(@"%@",array);
Output im getting
( Chennai, Delhi, Mumbai )
In your case:
- (NSMutableSet *)addressesSet {
[self willAccessValueForKey:@"addresses"];
NSMutableSet *result = (NSMutableSet *)[self mutableSetValueForKey:@"addresses"];
[self didAccessValueForKey:@"addresses"];
NSLog(@"%@",[result allObjects]); // printing the nsset
return result;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With