IPhone development (1)-Understanding iPhone OS/SDK and objective-C 2.0

Source: Internet
Author: User
  • Blogger: Yi Feifei
  • Link: http://www.yifeiyang.net/iphone-development-advanced-1-depth-understanding-of-iphone-os-sdk-and-objective-c-2-0/
  • 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 OS

    The 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 SDK

    The 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 Management

    Although 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.

    Container

    There are three main types of containers in objective-C:

    • Array
    • Dictionary
    • Set

    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.

    Notification

    Notification 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

     

  • 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.