1 #import "AddressBook.h"2 #import "Person.h"3 /*4 1. Why it is possible to use the variable array below without initializing the direct definition5 2. Why should I accept the return value with an immutable group?6 3.self usage, in the method why not replace _addressbook, but with Self.addressbook can7 4. When to receive with Nsarray, and when to use an immutable array8 */9 Ten @implementationAddressBook One //overriding initialization methods A-(instancetype) init - { -Self =[Super init]; the if(self) { -_addressbook =[Nsmutabledictionary dictionary]; - } - returnSelf ; +}
1. We rewrite the Init method because if Init is called in the main function using the system's default Init method, the parent class's init is invoked, but we have to do some initialization for our own defined instance variables, which means that our own defined class must have at least one initialization method. Whether you are customizing or overriding the system initialization method
1 if(APerson.name.length = =0|| APerson.phone.length = =0) {2NSLog (@"The contact name and phone number you added cannot be empty");3 returnNO;4}Else{5 6 if([[[_addressbook AllKeys] containsObject:aPerson.groupName]) {7Nsmutablearray *parr =[_addressbook objectForKey:aPerson.groupName];8 for(Person *pinchParr) {9 if([p.name isEqualToString:aPerson.name]) {TenNSLog (@"Duplicate Contacts"); One returnNO; A } - } - [Parr Addobject:aperson]; the returnYES; - } -Nsmutablearray *grop = [NsmutablearrayNew]; - [Grop Addobject:aperson]; + [_addressbook setobject:grop forKey:aPerson.groupName]; - + } A at returnNO;
2. In the above code, the variable group Parr and Grop represent two different modes of operation, the array name is the address space, Parr the address space of the key-value pairs in the dictionary, so there is no need to initialize, and Grop does not point to any address space, so you need to initialize
We usually in the assignment process must pay attention to the copy of the address, or re-open the space to replicate the value, the former can change the original space values, and the latter cannot
initialization, and assignment issues