How to print the contents of NSSet?
Asked Answered
H

2

6

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>
Hobo answered 2/12, 2016 at 11:32 Comment(7)
Please provide some code. It is really hard to understand where is your problem without a code.Selfabuse
I just want to log the contents of NSSet.Hobo
You wrote "So I followed..." - that is the code you need to add to your question here. Nobody can tell you where your following went wrong without seeing the code. Edit your question and add it. And be quick before someone closes this question as a duplicate of the one you reference!Sadyesaechao
I edited your question to include the code you added as a comment on one of the answers.Variegate
you can override the -description in the BUYAddress class to provide customised information about the instance during logging.Kavanaugh
The question is wrong, it's supposed to be : How to print something human readable rather than the hex address of a custom class ;-)Woodberry
Possible duplicate of What is the Objective-C equivalent for "toString()", for use with NSLog?Multiplex
V
3

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);
}
Variegate answered 2/12, 2016 at 11:47 Comment(8)
They dont have description method.Hobo
Exactly...you should add one. If for whatever reason you can't, check out the edit.Variegate
I added that sdk through pod.Hobo
Did you just copy/paste the code exactly as I wrote it. :| You'll probably need to modify it.Variegate
Yes I modified it according to my need.Hobo
Property "city" not found for the object.Hobo
they used some thing like - (NSMutableSet *)addressesSet { [self willAccessValueForKey:@"addresses"]; NSMutableSet *result = (NSMutableSet *)[self mutableSetValueForKey:@"addresses"]; [self didAccessValueForKey:@"addresses"]; return result; }Hobo
Thanks a lot James Webster you saved my day. Today I will leave my office happily.Hobo
S
2

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;
 } 
Sheba answered 2/12, 2016 at 11:41 Comment(9)
I tried like this NSArray *ar = [customer.addresses allObjects]; for (int i = 0; i<ar.count; i++) { NSLog(@"arr %@",ar[i]); } But the out put is arr <BUYAddress: 0x7fd451f6e050>Hobo
instead of loop for printing, just give NSLog("%@",ar)Sheba
Now I get arr ( "<BUYAddress: 0x7fff39f51760>" )Hobo
why you need to have "[customer.addresses allObjects]" change it to "customer" it will work.Sheba
Change "theNSSet"in my question with "result".Still if problem is there edit your question with some more code.Sheba
- (NSMutableSet *)addressesSet { [self willAccessValueForKey:@"addresses"]; NSMutableSet *result = (NSMutableSet *)[self mutableSetValueForKey:@"addresses"]; [self didAccessValueForKey:@"addresses"]; NSLog(@"%@",[result allObjects]); return result; } Edit code like thisSheba
What is this? It is their code the created NSSet like that.Hobo
i have added print statement along with that. please take a look at my editSheba
Let us continue this discussion in chat.Sheba

© 2022 - 2024 — McMap. All rights reserved.