Iphone Development Study Note 5

Source: Internet
Author: User

From. net C # porting to Cocoa Touch Objective C is slow, and about 20 classes in the two-dimensional graphics library have been transplanted. If the amount of work is too large, the system finally decides to give up, here, we will make a summary of the porting work to serve as a reference for future transplantation work.
1. Name space
Objective C does not support the command space, but uses the prefix to avoid name conflicts. There are external and internal classes in the C # library. The internal classes in the Development Kit generally have internal or do not want to open classes to developers. When porting to Objective C, you can use two prefixes to solve this problem. For example, you can use GD (Guidebee) as the internal class prefix, and MD (Mapdigit) as the external class prefix, at the end of the Development Kit release, only header files starting with MD are provided. The class starting with GD cannot be known when the Development Kit is used, and the purpose of hiding the internal class is also achieved.
2. abstract class
Objective C does not have the concept of an abstract class. It can be directly replaced by a common class during migration. If you do not want to use the Code as an instance with this abstract class, you can implement the init method to throw an exception.
3 Constants
Class constants are often defined in Class C #, which are not supported in Objective C. In this case, macro can be used for implementation.
For example
Internal class SingleFP
{
/**
* Positive Infinity.
*/
Private const int PositiveInfinity = int. MaxValue;
/**
* Negative infinity.
*/
Private const int NegativeInfinity = int. MinValue;
...
}
It can be defined in GDSingleFP. h.
/**
* Positive Infinity.
*/
# Define GDSingleFP_PositiveInfinity INT_MAX
/**
* Negative infinity.
*/
# Define GDSingleFP_NegativeInfinity INT_MIN
You can add GDSingleFP to a macro to avoid possible name conflicts.
4. C # Interface
Corresponding to the Objective Protocol
5. Private methods in C # class
First, do not define these private methods in the Objective C header file. They are directly defined in the. m file. In this way, you can hide these methods.
Second, use the private Category of Objective C to implement private methods.
6. Array Copy
The Array. Copy method is not provided in Objective C. During porting, memcpy is used to Copy the Array.
7. constructor and destructor
Objective C does not support overloading and automatic memory management.
When the constructor in C # is ported to Objective C
Use the initXXX naming method. If the class contains a NSObjective derived class, add dealloc to release the memory to avoid Memory leakage.
For example, LineFP has constructor.
Public LineFP ();
Public LineFP (LineFP l );
Public LineFP (PointFP p1, PointFP p2 );
Public LineFP (int ffX1, int ffY1, int ffX2, int ffY2 );
The following constructor can be defined in the corresponding Objective C GDLineFP. h:
-(GDLineFP *) init;
-(GDLineFP *) initWithLineGDLine *) l;
-(GDLineFP *) initWithP1GDPointFP *) p1 P2GDPointFP *) p2;
-(GDLineFP *) initWithX1int32_t) ffX1 Y1int) ffY1 X2int) ffX2 Y2int) ffY2;
8. int, long
To avoid int, long is defined differently on different platforms. int32_t and int64_t are recommended.
9. Class variable definition
In the C # class, you can define Static class variables, but in the Objective C class variables cannot be defined. In this case, you can define class variables outside the class definition and use extern in the header file.
10. Static Constructor
In Objective C, you can use the + (initialize) method to implement it. The + (initialize) method is executed before class initialization. Note that if this class has subclasses. + (Initialize) will also be executed before subclass initialization. If you want to implement singleton, be sure not to execute it multiple times.
11. Math class
The Cocoa Foundation class does not support corresponding classes. You must use the C interface in Math. h.
12. functions that return class objects
Because Objeictive C needs to manage the memory manually, the principle of who creates and releases the memory is generally used. For functions that return class objects, the caller does not know when to use the class objects, autorelease pool is generally used. You need to study the memory management mechanism of Objective C. Otherwise, memory leakage may occur.
Other C # syntaxes can basically find the corresponding Objective C syntax.

 


Excerpted from the mobile app

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.