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

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.