Reprinted text.Advanced iPhone development (1) --- in-depth understanding of iPhone OS/SDK and objective-C 2.0 To do well, you must first sharpen your tools. When developing iPhone applications, it is important to have a deep understanding of iPhone OS/SDK and objective-C 2.0.
IPhone OSThe iPhone OS consists of four main parts. The following is a simple list of their functions.
-
Cocoa touch
- Windows and views
- Event Management
- User Interface
- Acceleration Sensor
- Camera
-
Media
- Core graphics (2d graphic interface)
- Core animation (animation)
- OpenGL
- Core audio)
- Openal
- Media Player (MPEG4, MP3)
-
Core Services
- Address book
- Core Foundation
- Core location
- Cfnetwork (HTTP, https, FTP, SSL, TLS)
- Network Security
- SQLite (SQL database)
- XML
-
Core OS
- Multithreading
- Network Application (BSD socket)
- File System
- Bonjour (using wireless networks to connect to other machines)
IPhone SDKThe iPhone SDK mainly includes the following four tools.
- Xcode-project management, code editing, compilation, and debugging (IDE)
- Interface builder-GUI Design
- IPhone Simulator-Simulator
- Instrument-performance testing and adjustment
In the actual development process, xcode and interface Builder are basically used. Debugging uses a simulator or a real device. It should be noted that the simulation program on the PC, due to the clock speed of the PC, the performance is higher than the actual device, so it cannot only Debug on the simulator. In addition, some classes and functions cannot be used in simulators, such as nsdatecalendar or camera functions.
Objective-C 2.0 Memory ManagementAlthough objective-C 2.0 supports garbage collection, it is not available in iPhone OS. Therefore, we need to manage the memory by ourselves. Objective-C memory management method and reference counting method, that is to say, an object has a counter that references the object once, and the counter is incremented by one. When the counter is 0, the memory of this object is released.
When you create an object instance (init, alloc), the application calculates and adds one. During execution, if other objects need this object, you need to use (retain) to reference it, add one to the application counter of this object. You do not need to release an object using release. In this case, the reference counter is reduced by one. When the counter is 0, the memory of the object is released.
- Init, alloc-counter + 1
- Retain-counter + 1
- Release-Counter-1
In addition, if you do not use retain or release, you can use (autorelease) to automatically release objects.
ContainerThere are three main types of containers in objective-C:
The content added to the container cannot be Int or float directly. It must be implemented through encapsulation classes such as nsnumber. In objective-C 2.0, you can use enumerator to access elements in the container in sequence.
NotificationNotification is a Message notification function. Use the nsnotificationcenter class. Register the objects, methods, and events to be notified.
Archive)Archive means to save the object's memory layout to the file system as it is. Similarly, the object generated by the data in the file is called unachive. Use the nskeyedarchiver and nskeyedunarchiver classes in the iPhone SDK.
Generally, the current State is saved at the end of the program. When the program is started again, unachive will return to the exited state. The following is an example:
// Mykeyedarchiver. h
# Import <Cocoa/cocoa. h>
@ Interface nskeyedarchiver (mykeyedarchiver)
-(Void) encodevalueofobjctype const char *) valuetype at const void *) address;
@ End
# Import "mykeyedarchiver. H"
@ Implementation nskeyedarchiver (mykeyedarchiver)
-(Void) encodevalueofobjctype const char *) valuetype at const void *) Address
{
Nsmutabledata * datas = [nsmutabledata data];
Nsarchiver * arch = [[nsarchiver alloc] initforwritingwithmutabledata: datas];
[Arch encodevalueofobjctype: valuetype
At: address];
[Self encodeobject: [nsdata datawithdata: datas];
[Arch release];
}
@ End
// Mykeyedunarchiver. h
# Import <Cocoa/cocoa. h>
@ Interface nskeyedunarchiver (mykeyedunarchiver)
-(Void) decodevalueofobjctype const char *) valuetype at Void *) data;
@ End
# Import "mykeyedunarchiver. H"
@ Implementation nskeyedunarchiver (mykeyedunarchiver)
-(Void) decodevalueofobjctype const char *) valuetype at Void *) data
{
Nsdata * datas = [self decodeobject];
Nsunarchiver * unarch = [[nsunarchiver alloc] initforreadingwithdata: datas];
[Unarch decodevalueofobjctype: valuetype
At: Data];
[Unarch release];
}
@ End