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>
Community
  • 1
  • 1
e.k
  • 1,333
  • 1
  • 17
  • 36
  • 1
    Please provide some code. It is really hard to understand where is your problem without a code. – Roman Podymov Dec 02 '16 at 11:34
  • I just want to log the contents of NSSet. – e.k Dec 02 '16 at 11:35
  • 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! – CRD Dec 02 '16 at 11:41
  • I edited your question to include the code you added as a comment on one of the answers. – James Webster Dec 02 '16 at 11:50
  • you can override the `-description` in the `BUYAddress` class to provide customised information about the instance during logging. – holex Dec 02 '16 at 12:00
  • 2
    The question is wrong, it's supposed to be : *How to print something human readable rather than the hex address of a custom class* ;-) – vadian Dec 02 '16 at 12:04
  • Possible duplicate of [What is the Objective-C equivalent for "toString()", for use with NSLog?](http://stackoverflow.com/questions/1104746/what-is-the-objective-c-equivalent-for-tostring-for-use-with-nslog) – Larme Dec 02 '16 at 12:46

2 Answers2

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);
}
James Webster
  • 31,873
  • 11
  • 70
  • 114
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;
 } 
Saranjith
  • 11,242
  • 5
  • 69
  • 122
  • I tried like this NSArray *ar = [customer.addresses allObjects]; for (int i = 0; i – e.k Dec 02 '16 at 11:48
  • instead of loop for printing, just give NSLog("%@",ar) – Saranjith Dec 02 '16 at 11:50
  • Now I get arr ( "" ) – e.k Dec 02 '16 at 11:58
  • why you need to have "[customer.addresses allObjects]" change it to "customer" it will work. – Saranjith Dec 02 '16 at 12:01
  • Change "theNSSet"in my question with "result".Still if problem is there edit your question with some more code. – Saranjith Dec 02 '16 at 12:26
  • - (NSMutableSet *)addressesSet { [self willAccessValueForKey:@"addresses"]; NSMutableSet *result = (NSMutableSet *)[self mutableSetValueForKey:@"addresses"]; [self didAccessValueForKey:@"addresses"]; NSLog(@"%@",[result allObjects]); return result; } Edit code like this – Saranjith Dec 02 '16 at 12:27
  • What is this? It is their code the created NSSet like that. – e.k Dec 02 '16 at 12:29
  • i have added print statement along with that. please take a look at my edit – Saranjith Dec 02 '16 at 12:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129622/discussion-between-saranjith-and-e-k). – Saranjith Dec 02 '16 at 12:36