Some advantages of objective-c

Source: Internet
Author: User
Tags access properties

Although the grammar of objective-c is so different, so that beginners so uncomfortable, but in fact, it still has a lot of advantages (mainly compared to JAVA).


Protocol protocal
In fact, it is the interface, but because OC in the definition of class has used the name interface, so here is called protocal. I think there are two major advantages to this agreement:
1. Allow informal agreements: reminds me of the way the interface of the GO language was defined in the previous period. That is, when a class does not explicitly declare a protocol, as long as it implements the corresponding method, it can be used as the corresponding protocol.
2. Optional: In JAVA, when inheriting a interface, it is necessary to implement all of its methods, but in practice, many methods do not have to be implemented, resulting in many lines of useless code. But the way to allow tag protocol in OC is optional, so you just need to implement the part you want.


access to Properties (members) (Getter/setter)
This is actually not OC exclusive, I just want to spit slot JAVA. JAVA is usually the recommended class of properties is not exposed, and by means of access, the advantage is that if you need to access the property at the time of adding some logical processing is very convenient. However, there is not much time required for this kind of logic processing, so writing getter/setter for all attributes is simply adding junk code. OC's Way (C # is the same, others are not too clear) is to write getter/setter when needed. If the same getter/setter is achieved, the difference between the two is:
In JAVA:
Direct Access properties: member = Object.member; Object.member = member;
by Getter/setter:member = Object.getmember (); Object.setmember (member);
OC Inside Unity is:
member = Object.member; Object.member = member;
The difference is that, in OC, you only implement Getter/setter logic when you need it, without having to change the code elsewhere. And in JAVA, if you start without writing getter/setter, then you need to add the processing logic later, you need to change all the places to access the property, if you write in the beginning, it just adds a lot of junk code.


Delegate
This is also something in C #. Of course, JAVA can also implement the delegate mode, of course, is to use the interface to create a listener and other things. But, in fact, many times you just need a way, you don't need a interface. Although there is no real difference, but it is very uncomfortable to write. And you have to think of a name for your interface, and it's usually not a short name. If you define a delegate, you just need to give the method a name and you're OK.


Category
This thing has two advantages:
1. You can distribute the logic of the implementation to different files, reducing the size of a single class file
2. You can extend the original class as needed, without inheriting it, which is very convenient.
There's something similar in C # (I really like C #) very early on.


nsdictionary
This thing is worth talking about. The collection type in JAVA requires that the data in the set be of the same type (you can, of course, declare the collection as an Object collection ...). )。 Data is not required to be of the same type in nsdictionary, so it is very flexible to use.


A small summary: When designing the programming language, in the consideration of powerful, the appropriate addition of syntactic sugar is also very necessary, because the syntax of sugar can greatly simplify the code, the number of lines of code becomes less, many problems can be avoided. In fact, I would like to say that the most favorite is C #, unfortunately Microsoft does not give strength AH. Java Although there are a lot of things that make me uncomfortable, but in fact, it is his flaws, but the progress of Java is really too slow, and some of the more basic things are difficult to improve.

Some advantages of objective-c

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.