Quick notes for learning iOS-Dev as a beginner (1)

Source: Internet
Author: User

* Readers shoshould know: The notes series is just a quick scratch to record what I learned about IOS developing as a beginner. so actually it's for myself but not for you guys who as newbies want to learn something from here.

 

  • Objc looks different very much with C, C ++ ages, although which actually both have the same inner genes with it. and it's also different with other major versions ages 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"[]"Which is used to contain so-calledMessages. 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 actuallySendAMessageToCycler, 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... @ endTo define the class and uses@ Implementation... @ endTo implement methods.
  • And also there're plenty of special directives such@ 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 ).@ SelectorIs 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 returnsSelType.
  • To define a member function that contains (multiple) parameters, you probably write like this inC ++Style:
    ReturntypeTheclassname: thefunctionname (Paramtype1 param1,Paramtype2 param2,...){
    //...
    }

    However, in objc style, you need to write like this:
    @ Implementation theclassname
    ...
    -(Returntype) Thefunctionname:(Paramtype1) param1And:(Paramtype2) param2And :...{
    //...
    }
    @ End
    Confused, hmm? ActuallyThefunctionname: And :...Is the function name which is splitColons (:)That're followed by parameters.
  • KeywordsSuperIs used to specify the base (parent) Class in the inheriting tree andSelfIs used to specify the class itself. By using these you coshould 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 namedIDWhich is used to store of any type. In a sense, it's a generic object type. This type is often relatedDynamic bindingAndPolymorphism, Which is another one important concept in objc.
  • Objc is a objective-oriented language that natively supportPolymorphism & dynamic binding, That is, different with C ++ which is a dynamic-static combined language, all the methods in a class areVirtual FunctionsBy 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 functionTest:In a subclass that's inherited from a baseclass, which has a same FunctionTest:. Then when you use a baseclass pointer to reference a subclass object and callTest:Function, the corresponding method will be called, according to which the object really belongs to (in this case the subclass's test: function wocould be called). This feature is usually cooperate withType 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 & extensionsIs 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 cocould writeCategoryFor 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

    ExtensionsIs one special caseCategories, Since it's actuallyAnonymousCategory and Methods declared by it need to be all implemented.

  • Protocols & delegatesIs 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 bitPure virtual classesIn C ++ andInterfacesIn 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@ OptionalMeans the classes that adopts this Protocol don't need to implement the methods it specifies while@ RequiredMeans all the methods in this protocol must be implemented by the classes which adopts this protocol.
    DelegateIsDesign PatternAnd 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.

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.