Learning some special aspects of objective-C

Source: Internet
Author: User
Although objective-C is based on C, it is quite different from C ++, Java, and C, it also makes me very uncomfortable. First, call the method [circle setfillcolor: kredcolor] cricle is the class name, setfillcolor is the method name, kredcolor is the parameter value, and C # is used by people, the estimation is depressing. Two important points: (1) Definitions and calls of objective-C multi-parameter Methods

Example of method definition:

-(Void) insertobject :( ID) anobject atindex :( nsinteger) Index

Explanations:

1. Method Modifier

-This method is an entity method. It must be a class instance and can be called only through an instance.

+ Indicates that this method is a static method of the class and can be called directly without generating a class instance.

2. Parameter type

ID and nsinteger are the anobject and index parameters respectively.

3. Method Signature

In this example, insertobject and atindex constitute the signature keyword of this method.

Here is an example:

-(Void) setto: (INT) n over: (INT) d

{
Numerator = N;
Denominator = D;
}

[Afraction setto: 100 over: 200]; // call

Note: The method parameter name of objective-C is somewhat odd. The first parameter does not have a parameter name. If it is hard to say yes, it is the method name,

In general, when we see the colon, the parameter name is prior to the colon.

For example, a method without a parameter name is defined and called:

-(INT) set: (INT) N: (INT) D; [afraction set: 1: 3]; // call (two objective-C polymorphism -- Use of dynamic type IDs (comparison with C ))

In C #, we use interfaces to implement polymorphism. For example, the interface iob defines a method F. There are two classes A and B that implement the IOB interface.

Iob item = new ();

Item. F (); // The execution is a.f ();

Item = new B ();

Item. F (); // The executed B. F ();

In objective-C, the meaning of the interface is very different from that of C #, and cannot be used in this way.

So how can we achieve similar results. This is a special type ID. See the following code snippet. Note: Both fraction and complex contain the print method.

# Import "fraction. h"
# Import "complex. h"
Int main (INT argc, char * argv [])
{
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
Id datavalue; // defines an ID type variable
Fraction * F1 = [[fraction alloc] init];
Complex * C1 = [[complex alloc] init];
[F1 setto: 2 over: 5];
[C1 setreal: 10.0 andimaginary: 2.5];
// First datavalue gets a fraction
Datavalue = F1;
[Datavalue print]; // call the print method of Fraction
// Now datavalue gets a complex number
Datavalue = C1;
[Datavalue print]; // call complex's print method

[C1 release];
[F1 release];
[Pool drain];
Return 0;
}

Example of method definition:

-(Void) insertobject :( ID) anobject atindex :( nsinteger) Index

Explanations:

1. Method Modifier

-This method is an entity method. It must be a class instance and can be called only through an instance.

+ Indicates that this method is a static method of the class and can be called directly without generating a class instance.

2. Parameter type

ID and nsinteger are the anobject and index parameters respectively.

3. Method Signature

In this example, insertobject and atindex constitute the signature keyword of this method.

Here is an example:

-(Void) setto: (INT) n over: (INT) d

{
Numerator = N;
Denominator = D;
}

[Afraction setto: 100 over: 200]; // call

Note: The method parameter name of objective-C is somewhat odd. The first parameter does not have a parameter name. If it is hard to say yes, it is the method name,

In general, when we see the colon, the parameter name is prior to the colon.

For example, a method without a parameter name is defined and called:

-(INT) set: (INT) N: (INT) D; [afraction set: 1: 3]; // call (two objective-C polymorphism -- Use of dynamic type IDs (comparison with C ))

In C #, we use interfaces to implement polymorphism. For example, the interface iob defines a method F. There are two classes A and B that implement the IOB interface.

Iob item = new ();

Item. F (); // The execution is a.f ();

Item = new B ();

Item. F (); // The executed B. F ();

In objective-C, the meaning of the interface is very different from that of C #, and cannot be used in this way.

So how can we achieve similar results. This is a special type ID. See the following code snippet. Note: Both fraction and complex contain the print method.

# Import "fraction. h"
# Import "complex. h"
Int main (INT argc, char * argv [])
{
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
Id datavalue; // defines an ID type variable
Fraction * F1 = [[fraction alloc] init];
Complex * C1 = [[complex alloc] init];
[F1 setto: 2 over: 5];
[C1 setreal: 10.0 andimaginary: 2.5];
// First datavalue gets a fraction
Datavalue = F1;
[Datavalue print]; // call the print method of Fraction
// Now datavalue gets a complex number
Datavalue = C1;
[Datavalue print]; // call complex's print method

[C1 release];
[F1 release];
[Pool drain];
Return 0;
}

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.