Objectiva c Study Notes 2-

Source: Internet
Author: User

1. for the use of self in the instance method, objc is special. for example, self. name actually accesses the property name instead of the internal variable, that is, It accesses the setter/getter method.

 

@ Interface person: nsobject
{
Nsstring * Name;
}
@ Property (copy) nsstring * Name;
@ End

@ Implementation person
- ( Void ) Dosomething {
Name =   @" Fred " ; // Access Ivar directly!
Self. Name =   @" Fred " ; // Callaccessor method.
}
@ Sythesize name;
@ End

. H file, Declaration class, with private variables in braces

 

# Import < Foundation / Foundation. h >
@ Interface person: nsobject
{
// Instance variables
Nsstring * Name;
Int Age;
}
// Method declarations
- (Nsstring * ) Name;
- ( Void ) Setname :( nsstring * ) Value;

-(Int) Age;
-(Void) Setage :(Int) Age;

-(Bool) canlegallyvote;
-(Void) Castballot;
@ End

 

Implementation class

 

# Import " Person. h "

@ Implementation person
-(Int) Age {
ReturnAge;
}
-(Void) Setage :(Int) Value {
Age=Value;
}
//... And other methods like super. INIT ();
@ End

The object can be created in two steps: + alloc and-init. The former is memory allocation (requiring release) and the latter is initialization.

-Retain is to increase the count

-Release is used to reduce the count.

-The dealloc system automatically calls this function when the retain count is 0.

[Nil dosomething]; no response method

@ Property (assign) nsstring * Name; // pointer assignment
@ Property (retain) nsstring * Name; // retain called
@ Property (copy) nsstring * Name; // copy called

 

 

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.