initialization, and assignment issues

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.