About Objective-c languages

Source: Internet
Author: User

2011-05-11 11:20 anonymous Baidu Encyclopediafont Size:T | T

Objective-c is an object-oriented programming language that expands C. Mainly used in Mac OS X and GNUstep, two systems using OPENSTEP standards, while in NeXTSTEP and OpenStep it is the basic language. Objective-c can be written and compiled in GCC-operated systems because GCC contains objective-c compilers.

AD: Dry Goods come, don't wait! WOT2015 Beijing Station lecture ppt open download!

Objective-c, usually writing OBJC and less used Objective C or OBJ-C, is an object-oriented programming language that expands C. It is mainly used in Mac OS X and GNUstep, which use the OpenStep standard system, and it is the basic language in NeXTSTEP and OpenStep. Objective-c can be written and compiled in GCC-operated systems because GCC contains objective-c compilers. At the beginning of the 1980, Brad Cox invented objective-c at his company StepStone. He is very concerned about the real availability of software design and programming. Objective-c's most important description was his 1986 object oriented Programming:an evolutionary approach, which he published. Addison Wesley. ISBN 0-201-54834-8.

Advantages and Disadvantages

Objective-c is a very "real" language. It uses a very small run-time library written in C, which only makes the size of the application small, and most OO systems use great VM execution times instead of the entire system. The program written by OBJC is usually not much larger than its original code. and its function library (usually not attached to the software release) also and Smalltalk system to use great memory to open a window in the case of the opposite. The initial version of OBJECTIVE-C does not support garbage collection. At the time, this was one of the main points of contention, with many considering the long "time of death" in Smalltalk recycling, which made the system useless. Objective-c does not have this feature to avoid this problem. Although some third-party versions have joined this feature (especially GNUstep), Apple has not introduced this feature in its Mac OS X 10.3. Another problem is that OBJC does not include the namespace mechanism (namespace mechanism). Instead, programmers must prefix their category names and often cause conflicts. In the 2004, in the Cocoa programming environment, all Mac OS X categories and functions have "ns" as prefixes, such as nsobject or Nsbutton, to make it clear that they belong to Mac OS X core, and that "NS" is due to the names of these categories when NeXTSTEP development. Although Objective-c is a parent set of C, it does not consider the basic type of C as the first-level object. Unlike C + +, OBJECTIVE-C does not support operator multi-load (it does not support Ad-hoc multi-type). is also different from C + +, but as with Java, objective-c only allows objects to inherit one category (without multiple inheritance). Categories and protocols not only provide many benefits of multiple inheritance, but also do not have many drawbacks, such as excessive execution time and binary incompatibility.

Objective-c Leaderboard

TIOBE 2010 May Programming Language list has been released recently, the main change in this period is objective-c ranking into the top ten. Although Objective-c's share was only 0.08% per cent last month, it took an important step in the rankings – the first 10. This step is significant, you know, since the June 2001 Tiobe programming list, only 13 programming languages have ever entered the top 10. The main reason for the popularity of objective-c may be that it is the only language that can be programmed for iphone and ipad. But from the point of view of programming language alone, the Object-c born in 1986 didn't show much originality.

TIOBE June 2010 Programming Language list has been released recently, Objective-c rankings continue to climb, from 10th to 9th place. Thus, Objective-c has a lot of development space.

Comparison of Objective-c and C + +

Single Inheritance:

OBJECTIVE-C does not support multiple inheritance, (same as Java and Smalltalk), while the C + + language supports multiple inheritance.

Dynamic:

Objective-c is dynamically trained (Dynamicaly typed) so its class cubby C + + is easy to manipulate. Objective-c can allow access to methods and classes based on string names at run time, and can also dynamically connect and add classes.

C + + follows the Simula 67 (an early OO language) school in object-oriented programming, while Objecive-c belongs to the Smalltalk school.

In C + +, the static type of an object determines whether you can send a message to it, and for objective-c, it is determined by the dynamic type. The Simula 67 School is more secure because most errors can be detected at compile time. And the Smalltalk school is more flexible, such as some smalltalk seemingly unmistakable procedures to get SIMUALR 67 there will not be able to pass.

In many ways, the difference between C + + and objective-c is more of a way of thinking than it is about technology. Do you want to be more secure and abandon flexibility? Supporters of the Simular67 school argue that there is no need for flexibility when it comes to programming excellence, while the Smalltalk school argues that to be flexible you can tolerate many errors in operation.

Objective-c Concise Tutorials

Objective

Before you begin, assume that you have some basic knowledge of C, including variable types, functions, return values, and the related concepts of pointers. If you are not aware of these, it is recommended to first learn rectification's "C Programming".

Objective-c, a derivative of C, inherits all C-language features. There are some exceptions, but they are not the language attributes that inherit from C itself.

Nil: You may have used NULL in C/s + + and nil in objective-c. The difference is that you can pass a message to nil (for example [nil message];), which is perfectly legal, but you can't do the same with NULL.

BOOL:C does not have a formal Boolean type, and in objective-c it is not "really" there. It is included in the foundation Classes (base Class library) (that is, import Nsobject.h;nil is also included in this header file). BOOL has two types in objective-c: YES or no, not TRUE or FALSE.

#import vs #include: As you can see in the Hello World paradigm, we use #import. #import supported by the GCC compiler. I do not recommend the use of #include, #import is basically the same as the #ifndef #define #endif. h gear. Many programmers agree that it is foolish to use these things. In any case, the use of #import is right. This will not only avoid trouble, but in the event that GCC takes it off one day, there would be enough objective-c programmers to keep it or put it back. Secretly told you that Apple also used #import in their official code. So in case something like this happens one day, it's not hard to predict that Apple will provide a GCC branch version that supports #import.

In Objective-c, the Word method and message are interchangeable. However, messages has special characteristics, the specific difference in the back of the introduction.

compiling Hello World

    1. Hello.m
    2. #import
    3. int main (int argc, const char *argv[]) {
    4. printf ("Hello world\n");
    5. return 0;
    6. }

Output

Hello World

Use #import instead of #include in objective-c

The default file name for the objective-c is. m

Create Classe

@interface

Based on the example of "Programming in Objective-c," "Copyright (C) 2004 by Sams Publishing book, and is published as permitted.

    1. Fraction.h
    2. #import
    3. @interface Fraction:nsobject {
    4. int numerator;
    5. int denominator;
    6. }
    7. -(void) print;
    8. -(void) Setnumerator: (int) n;
    9. -(void) Setdenominator: (int) D;
    10. -(int) numerator;
    11. -(int) denominator;
    12. @end

Nsobject:nextstep the abbreviation for Object. Because it has been renamed OpenStep, so this is not so meaningful today.

Inheritance (inheritance) is expressed as class:parent, like the fraction:nsobject above.

Sandwiched in @interface class:parent {...} is called instance variables.

When access is not set (protected, public, private), the default access rights are protected. The way you set permissions will be explained in a little later.

Instance methods is followed by a member variable (that is, Instance variables). The format is: scope (returntype) MethodName: (Parameter1type) Parameter1name;scope has class or instance two kinds.

Instance methods begins with-The class level methods starts with +.

Interface ends with a @end.

Native Hello World code

    1. #import
    2. int main (int argc, const char * argv[]) {
    3. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    4. Insert code here ...
    5. NSLog (@ "Hello, world!");
    6. [Pool drain];
    7. return 0;
    8. }

The above is the default. m file content in the Xcode development environment, which is the native Hello World sample code.

More Extensions to Hello World

The difference between #import and #include in objective-c

In Objective-c, #import is used as an improved version of the #include directive. In addition, #import determine that a file can only be imported once, which allows you to have no problem with recursive inclusion. It's up to you to decide which one to use. In general, when importing the OBJECTIVE-C header file, use the #import, #include when the C header file is included.

About Objective-c languages

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.