Search code examples
objective-cclassnsarraynsdictionary

Creating Person Class with Objective-C


I'm starting to learn Objective-C and I want to create a simple Person class with name, photo, address (house number, street name, postal code, city), phone.

I'm not sure if I have to use NSArray or NSDictionary for the address attribute. I already created a Person class and I placed my code in Person.h

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *photo;
@property (nonatomic, copy) NSString *address;
@property (nonatomic, copy) NSArray *phone;

Solution

  • The address should not be an NSArray or an NSDictionary. It should be another custom class Address. This class should then have its own attributes for house number, street name, postal code, city, country, etc.

    If you want a person to have more than one address, then address should be a dictionary where the keys are labels for each address and the values are instance of Address.

    Your phone property should also be a dictionary if you wish to support more than one phone number per person.

    You should look at the CNContact and related classes for examples of how Apple does it.