Objective-c (3) relationship between has a relation---object

Source: Internet
Author: User

    • What is a composite relationship?

The compound relationship of a class is the most common one in the relationship of a class.

A composite is a relationship with a, such as a person class that contains a member property, a book object, and someone has a.


    • The realization of compound relation

As required in OC, all objects are referenced as pointers, so this relationship should be reflected as:

The object of the person class contains the address of a book object.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7C/7C/wKiom1bRV6byR3ejAAFDC5LOVCM808.png "title=" screen shot 2016-02-27 pm 4.00.02.png "alt=" Wkiom1brv6byr3ejaafdc5lovcm808.png "width=" "height=" 471 "border=" 0 "hspace=" 0 " Vspace= "0" style= "width:400px;height:471px;"/>

As shown in the figure, three objects are included:

Person object (created by the new method)

NSString object (the literal value of the string Object @ "Zhang San" assignment)

Book object (created by the new method)


    • Memory conditions for composite relationships

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7C/7B/wKioL1bRW8ySFn6MAAB9otZiE0Y576.png "title=" screen shot 2016-02-27 pm 4.15.58.png "alt=" Wkiol1brw8ysfn6maab9otzie0y576.png "width=" "height=" 444 "border=" 0 "hspace=" 0 " Vspace= "0" style= "width:400px;height:444px;"/>

Obviously, the P pointer must be a local variable in a function or method, and its memory uses a stack control

All objects in the objective-c must be in the heap, so the memory of the three objects, the person, the NSString, the book, is the heap space

The memory space of person, nsstring, book is not contiguous or mutually inclusive

Person with a nsstring is represented as the address of the NSString object in the Person object

Person with a book behaves as the address of a book object in the Person object


    • Circular reference issues

If there are two classes in which properties are compounded, the header file contains a circular reference to each other

Using @class in the header file is the class that declares the compound, and the source file contains the header file of the class to solve the problem

Amperson.h@class Ambook; @interface amperson:nsobject {ambook * book;} @end
Amperson.m#import "AMBook.h" @implementation Amperson//... @end
Ambook.h@class Amperson; @interface ambook:nsobject {Amperson * person;} @end
Ambook.h#import "AMPerson.h" @implementation ambook//... @end




This article is from the "Teacheran" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1745563

Objective-c (3) relationship between has a relation---object

Related Article

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.