How to print the contents of NSSet?

I request and receive an NSSet that contains the client address from the Internet. Since I am new to c object development, I don't know how to get the country, zip code, etc. From this 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 an object "0x7f99997b7a50". How to print all lines in a 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 output: arr:

 <BUYAddress: 0x7fd451f6e050>
+4
source share
2 answers

If you have a custom object, you may need to override description

Without redefinition:

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

gives:

2016-12-02 11:45:55.342 Playground[95359:4188387] (
    "<CustomObject: 0x600000037a20>",
    "<CustomObject: 0x60000003ae20>"
)

, description CustomObject:

-(NSString*) description
{
    return [NSString stringWithFormat:@"%@ (%@)", self.name, self.jobTitle];
}

:

(
    "Holly T Canine (Pet Dog)",
    "James Webster (Code Monkey)"
)

- , ; - :

NSArray *ar = [customer.addresses allObjects]; 
for (int i = 0; i<ar.count; i++) 
{ 
    NSLog(@"arr %@ (%@)",ar[i].name, ar[i].address); 
}

, . :

for (BUYAddress *address in customer.addresses)
{
    NSLog(@"Address: %@, %@, %@", address.address1, address.address2, address.city);
}
+3

NSSet ,

 NSSet *theNSSet = [NSSet setWithObjects:@"Chennai",@"Mumbai",@"Delhi", nil];

NSArray,

NSArray *array = [theNSSet allObjects]; // theNSSet is replaced with your NSSet id

,

NSLog(@"%@",array);

im,

(     Chennai,     ,     Mumbai     )

:

- (NSMutableSet *)addressesSet { 
 [self willAccessValueForKey:@"addresses"];
 NSMutableSet *result = (NSMutableSet *)[self mutableSetValueForKey:@"addresses"]; 
 [self didAccessValueForKey:@"addresses"];
 NSLog(@"%@",[result allObjects]); // printing the nsset 
 return result;
 } 
+2

Source: https://habr.com/ru/post/1662626/


All Articles