iOS Development Primer (ii): Introduction to Objective-c's simple syntax

Source: Internet
Author: User
Tags uikit

One: Object-oriented thinking Objective-c and C language programming ideas, C language is the process-oriented programming, and Objective-c is object-oriented programming, so-called object-oriented, my personal understanding, is abstract, will have a certain commonality of the physical abstraction into a class, the encapsulation , inheriting, manipulating the physical itself. II: The History of Objective-c Objective-cis based on Smalltalk, built on the C language, and compatible with C and C + + a programming language. Designed by Brad J.cox in the early the 1980s,Apple released Objective-c 2.0 in 2007 and started using objective-c for development on the iphone. Three: Objective-c frame objective-c programming is mainly used in the framework is Cocoa, It is one of the five major APIs in data-scayt_word= Macos x "x in one of the five APIs, It consists of two different frames Foundationkit  and Applicationkit. foundation is the most basic framework, Nsarray,nsdictionary and NSNumber." The iphone app development   will also use the UIKITK framework, which will be introduced later. Four: Objective-c extension OC is a combination of C and Smalltalk, which supports the same basic C language syntax.            As with C, the file is divided into header and source files with the. h and. M. h header files that contain definitions, types, functions, and declarations of constants, respectively. . m source files This typical extension is used to define the source file, which can contain both C and Objective-c code. Five: Header file introduced #import in Objective-c, including header files have a better method than # include #import. It's used the same as # include, but you can guarantee that your program contains only the same header files once. For example, introducing the Uikit framework
#import <UIKit/UIKit.h>
VI: Standard output function NSLog () the print function in Objective-c is NSLog (), and of course it can be used with printf (), but NSLog () provides features such as timestamps, date stamps, and automatic line breaks, which are more convenient to use, so NSLog () It will be more convenient to use. For example:
NSLog (@ "HelloWorld");
Seven: Class method, instance method Objective-c method definition contains method type, return type, one or more keywords, parameter type and parameter name. And there are two types of methods in Objective-c: instance methods, class methods. The instance method before using "-" indicates that the class method uses "+" to indicate that the instance method needs to be called with an instance object of the class, and the class method can only be called by the class name. Here is an example of a class method and an instance method:
-(void) SETCC: (nsstring*) C;  // instance method + (void) SETCC: (nsstring*) C; // class Method
Eight. The properties of a variable's property variable provide a more convenient way to access than the method. Replace getter and setter methods with the @property identifier. The way to use this is to use the @property identifier in the class interface. h file followed by the properties of the variable, including copy, Tetain, assign, ReadOnly, readwrite,nonatomic, then the variable name and then in the implementation file. m using @ The synthesize plus variable name implementation format declares the property's syntax as follows: @property (attribute 1, attribute 2) type name;
. h
@property BOOL flag; @property (nonatomic) nsstring* name;
. m
@synthesize Flag,name;
The meaning of each parameter is as follows: ReadWrite produces Setter\getter method readonly only produces simple getter, no setter. Assign the default type, the setter method assigns a value directly, without retain operation retain setter method to release the old value of the parameter, and then retain the new value. Copy setter method Copy operation, like retain nonatomic prohibit multi-threading, variable protection, improve performance properties of another advantage is that you can use the point "." Syntax to access nine: Object initialization there are two methods of initializing an OC object: one is [class name new] and the second is [[Class name Alloc]init].         The two methods are equivalent, however, the usual cocoa convention is to use Alloc and init instead of new. In general, cocoa programmers only use new as a helper method when they do not have enough proficiency to use the Alloc and Init methods. [[Class name Alloc]init] has two actions. Alloc is the allocation of memory, a piece of memory obtained from the operating system and designated as the location of the instance variable that holds the object. The Alloc method also initializes the memory area to 0.    corresponding to the assignment action is initialization. The custom initialization method format is as follows:
-(ID) init{       if(self = [super init]           ) {//... // code  there         }
return self;}

The purpose of using self= [Super init] is to have the parent class do their own initialization work. This will be introduced in the future.

Ten: The selector selector selector is just a method name that is encoded in the special way that the OC runtime uses to quickly execute the query. You can use the @selector () precompiled directive to specify the selector, where the method name is in parentheses. As in a class SETCC: The selector for the method is: @selector (SETCC:). Cond...

iOS Development Primer (ii): Introduction to Objective-c's simple syntax

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.