Objective-c Study Notes

Source: Internet
Author: User

The following notes reference site: http://blog.csdn.net/huanglx1984/archive/2009/07/06/4325377.aspx

  1. Currently, apple only uses objective-C as the supported language.
  2. The differences with C ++ are:
    • All classes in the O-C must inherit from nsobject.
    • All objects in the O-C are pointer-based.
    • The O-C replaces this with self.
    • The O-C uses ID instead of void *.
    • The O-C uses nil to represent null
    • O-ck only supports single inheritance.
    • O-C uses YES/no to indicate true/false
    • The O-C uses # import instead # include
    • The method that represents the class with messages in the O-C and uses the [ainstance method: argv] Call form.
    • O-C supports reflection mechanism
    • The O-C supports dynamic typing, dynamic binding, and dynamic loading.
  3. The similarities with C ++ are:
    • The shared part is the same as that shared by C.
    • You can use assert (bool), which is generally replaced by nscparameterassert (bool.
  4. Description of naming prefixes in O-C:
    • NS-: nextstep
    • CF-: Core Foundation
    • Ca-: Core Animation
    • CG-: Core graphics
    • UI-: User Interface
  5. Message particularity in O-C:
    • The class that calls a message does not know how to respond to the message. If it does not know how to process the message, it automatically transfers the message to another class, such as its parent class.
    • The class that calls the message can be nil. In C ++, before using class methods, we need to check whether the object is empty. Therefore, when implementing the destructor, we usually have the following code, suchIf (VAR) {Delete var ;}But in Objective C, we can directly write[Var release];Even if Var = nil, there is no problem.
  6. The function declaration formats in the O-C include:
    • -/+ (Return type) function_name;

    • -/+ (Return type) function_name: (parameter type) parameter;

    • -/+ (Return type) function_name: (parameter type) parameter1Otherparameter: (Parameter_type) parameter2

    • The preceding parameter description:-indicates a general function, and + indicates a static function. Otherparameter is the parameter alias (the alias of the first parameter is omitted), which can be conveniently specified during function calls.

  7. Constructor/destructor in O-C

    • The init ()/release () in the O-C corresponds to the constructor/destructor of C ++. Alloc ()/dealloc () corresponds to the new and delete functions of C ++. dealloc () does not need to be called manually because of the automatic call of reference count.

    • The init ()/release () function of the parent class in the O-C needs to be manually called by the subclass. And must be called every time. Different from automatic calls of C ++.

    • The constructor (-(ID) Init) calls the form csample * psample = [csample alloc] init]. Among them, alloc (+ (ID) alloc) is the inherited static function, init is an inherited General function. For example, when a general function is rewritten, it is equivalent to overwriting (without parameters) or overloading (with parameters) of C ++ ).

    • The Destructor (-(void) Release reduces the reference count by 1. When the value is 0, the release () of the parent class automatically calls dealloc (-(void) dealloc );

  8. It is recommended that you retain it when the O-C does not have a data member.

  9. The inherited method, for example,-(ID) Init can be omitted in the header file. We recommend that you retain

  10. In 0-c, There is only access restriction for data members, and there is no access restriction for methods.

    • Like C ++, data members have three access restrictions: public, protected, and private. The default value is protected.

    • Example:@ Interface accessexample: nsobject {
      @ Public
      Int publicvar;
      @ Protected
      Int protectedvar;
      @ Private
      Int privatevar;
      }
      @ End

    • Method access restrictions can be implemented through category

    • Example:

      @ Interface myclass

      -(Void) sayhello {

      Nslog (@ "hello ");

      }

      @ End


      @ Interface myclass (private)

      -(Void) KissGoodbye;

      @ End

  11. There is no static variable for the class in the O-C, only the global variable

  12. Arrays in the O-C nsarray can save different types of data.

  13. The O-C also supports class type checks during Run-Time

    • -(Bool) iskindofclass: classobj 
      Used to determine whether the object belongs to a class or its subclass

    • -(Bool) ismemberofclass: classobj
      Used to determine whether the object belongs to a class (this does not include subclass)

    • -(Bool) respondstoselector: Selector
      Used to determine whether the object can respond to a message. Here, we can understand the parameters following @ selector as function pointers in C ++.
      Note: 1) do not forget @ 2) @ selector is followed by () instead of []. 3) The message name must be followed by: whether or not the message contains parameters. For example, [psquare respondstoselector: @ selector (set: andheight :)].

    • + (Bool) instancesrespondtoselector: Selector
      Used to determine whether the class can respond to a message. This is a static function.

    • -(ID) specify mselector: selector: Call the selector method of the object.

    • Similar to respondstoselector, conformstoprotocol is used to dynamically check whether an object complies with a certain protocol.

  14. Category: adds some new functions to an existing class without source code.

    • Only new methods can be added. New data members cannot be added.

    • CategoryMust be unique.

  15. Protocol: equivalent to pure virtual class in C ++

    • For example, @ interface mydate: nsobject <printing >{}@ end
    • Usage:Mydate * dat = [[mydate alloc] init];ID <printing> Var = dat; [Var print].
    • Note: We first declare the Printing Protocol. Any class that complies with this Protocol must implement the print method. In Objective C, we use <> to indicate compliance with an agreement. When a class declaration complies with a protocol, it must implement all methods in the Protocol in the. M file. Use Id <printing> as the type, rather than the printing * VaR in C ++.
  16. Iboutlet and ibaction: These two things do not actually play a major role in syntax. If you want to see the control object in interface builder, add iboutlet before the definition to see the outlet of this object in IB, if you want to control an object to execute certain actions in interface builder, add (ibaction) before the method ).
  17. Avoid nesting more than two layers in a row of statements.
  18. Message forwarding:-(void) forwardinvocation: (nsinvocation *) aninvocation;
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.