Quick Notes for Learning IOS-dev as a beginner (1)

*Readers should know : The Notes Series is just a quick scratch to record what I learnt about IOS developing as a beginner. So actually it's for myself but not for you guys who as newbies want to learn sth from here.  

 

  • ObjC looks different very much with C, C++ Languages, although which actually both have the same inner genes with it. And it's also different with other major languages that you maybe heard or use about(such Java, C#, etc). The most obvious difference is the sign"@" that's used as the directive and a pair of "[]" which is used to contain so-called messages. They'll appear nearly everywhere in a piece of code written by ObjC.
  • If you want to call an object's method in ObjC, you need to accept a concept that you actually send a message to a receiver, that's [receiver message] ( in C++ it's just recevier->message() ) 
  • The definition&implementation of a class written by ObjC is totally different with by C++.   ObjC uses @interface ...@end to define the class and uses @implementation ... @end to implement methods. 
  • And also there're plenty of special directives such as @public, @private, @protected(basically they work the same as in C++, while you can't directly access the instance variables even if it's in the scope of @public, which is different with in C++), and @property, @synthesize(the two directives is a very important concept that simplify the way to quick set setter&getter methods and also hence become a recommend way to access the inner instance variables). @selector is another special directive that's used to determine class hierarchy issue and to verify whether the special function is supported by the current class or not. it returns SEL type.
  • To define a member function that contains (multiple) parameters, you probably write like this in C++ style: 
           returnType TheClassName::TheFunctionName(paramType1 param1, paramType2 param2, ...) {
              //...
           }
          
    However, in ObjC style, you need to write like this:  
    @implementation TheClassName
    ...
    -(returnType)  TheFunctionName: (paramType1) param1 And: (paramType2) param2 And: ...{
      //...
    }
    @end
    Confused, hmm? Actually TheFunctionName:And:And:... is the function name which is split by colons(:) that're followed by parameters.
  • keywords super is used to specify the base(parent) class in the inheriting tree and self is used to specify the class itself. by using these you could determine which methods you want to call (because they're maybe overwritten and hard to tell which is which). 
  • Compared with C++, there is one special type named id which is used to store of any type. In a sense, it's a generic object type. This type is often related to dynamic binding and polymorphism, which is another one important concept in ObjC. 
  • ObjC is a Objective-Oriented Language that natively support polymorphism&dynamic binding, that is , different with C++ which is a dynamic-static combined language, all the methods in a class are virtual functions by default in ObjC. So you don't need to declare a function as virtual if you want to use the polymorphism feature. For example, you plan to overwrite a member function test: in a subclass that's inherited from a baseclass, which has a same function test: . Then when you use a baseclass pointer to reference a subclass object and call the test: function, the corresponding method will be called, according to which the object really belongs to(in this case the subclass's test: function would be called). This feature is usually cooperate with the type id. Take a piece code as another example:
    baseClass* baseObj = [[baseClass alloc] init];
    subClass* subObj = [[subClass alloc] init]; 
    id eitherObj = baseObj;
    [eitherObj test];     //  will call the baseClass's test: function
    eitherObj = subObj;
    [eitherObj test];    //  will call the subClass's test: function 
  • Categories & Extensions is a powerful ability provided specially by ObjC to let users extend classes' functions(methods) not from inheritance. That means if you want a class has some extra methods, you're not necessary to write a subclass which's inherited from it. Instead, you could write a category for it. This concept provides a way to classify methods you want to use to extend classes, which may even not belong to yours.  A category looks like this : 
    @interface className(categoryName)
    -( void) externMethodName : ( int) param_1 : ( float) param_2;
    // ....
    @end
    Extensions is one special case of Categories, since it's actually an anonymous Category and methods declared by it need to be all implemented.
  • Protocols & Delegates is another powerful concept in ObjC. A protocol declares methods but not implement it. Instead, Every class which adopts that protocol takes charge of the implementation. It's like a little bit pure virtual classes in C++ and interfaces in Java(just declare but not define).  A protocol can define like this : 
    @protocol theProtocol
    @optional
    -( void) oneInterfaceMethod;
    @required
    -( int) theOtherOneInterfaceMethod : ( int) param1 with: ( float)param2;
    @end
    The directive @optional means the classes that adopts this protocol  don't need to implement the methods it specifies while @required means all the methods in this protocol must be implemented by the classes which adopts this protocol.
    Delegate is a design pattern and it's frequently used in IOS developing. For example, the UIKit framework defines lots of base classes and protocols and delegate the real work to the classes implemented by the user. 

你可能感兴趣的:(inner)