iOS Development Series-OBJECTIVE-C Basic Overview OC Formatted output example

Source: Internet
Author: User

Overview

We have used a few chapters of the content of the C language, of course, through a few articles complete introduction of C language knowledge is not very realistic, such as C language file operation, memory application, etc. we have not focused on the introduction, of course, the core knowledge points have been mentioned, there is time later we will continue to expand. Today we formally began to learn objective-c, hereafter referred to as "OBJC", OBJC is based on the C language with a layer of object-oriented features, it is fully compatible with the C language, even can be mixed with C + +. It is the main development language for Mac OS X and iOS, which can be said to be a straight-up trend after release from iOS, and currently OBJC is the third largest programming language after C and Java. OBJC Today, let's take a brief look at some of the basic elements of C:

    1. First OBJC Program
    2. Basic data types
First OBJC Program

Open Xcode to create a new project

Choose a command-line program

Select the program language as OJBECTIVE-C and enter the project name

Select Save Directory

A basic project structure is generated by default

Program source Code

////main.m//basics////Created by Kenshin Cui on 14-2-1.//Copyright (c) 2014 Kenshin Cui. All Rights reserved.//#import<Foundation/Foundation.h>intMain (intargc,const Char* argv[]) {@autoreleasepool {//Insert code here ...NSLog (@"Hello, world!."); }    return0;}

Above is the first OBJC program created through Xcode, which is a classic "hello,world! ”。 The results of the operation are as follows:

Here is a simple explanation for the above code:

    1. First #import is an import command, similar to the C # include command, but relative to # Include, #import有效的处理了重复导入的问题, also mentioned in the C language how to resolve the duplicate import through conditional compilation, and in OBJC you do not need to process, #import已经自动进行了重复处理; similar to # include, Import system class library using <>, import custom class libraries using "";  
    2. @autoreleasepool is the keyword of OBJC, its role is to be included in this keyword is included in the program will be automatically memory recycling, do not require developers to manually release useless objects And, of course, it appears to be somewhat similar to the memory garbage collection mechanism of C # and Java, but in fact their principles are completely different and will be explained in detail later.  
    3. nslog is a standard output function, similar to the printf () function in C, but it wraps itself. Of course it also supports format output (for example,%i represents output shaping data,%f output floating point data), which is declared in Fundation.h;  
    4. @ "hello,world!" is a string constant that requires the @ symbol before the string in OBJC;

Extended--OJBC Syntax Features
    • There is no namespace (C #) or package (Java) concept in OBJC, which means that there cannot be two identical class names at the same time in the same application, which is usually distinguished by a prefix, such as nsstring in OBJC, NS in NSLog.

The NS is a nextstep abbreviation, a system developed by Steve Jobs at next after he left Apple, which was not widely accepted at the time but laid the groundwork for Mac Ox behind.

    • Many of the keywords in OBJC are preceded by @ (for example, @autoreleasepool, @interface, @protocol), and it is important to note that in order to differentiate the strings in the C language, the strings in OBJC must be prefixed with @.
Basic data types

The basic data types in OBJC are in addition to char, int, float, and double in the C language:

    1. type bool, containing two values Yes and no, in fact the Boolean type in OBJC is the integers 1 and 0;
    2. The ID type, which is an object type that can represent all objects;
    3. NSString is a string type defined in the Fundation framework;
////main.m//basics////Created by Kenshin Cui on 14-2-1.//Copyright (c) 2014 Kenshin Cui. All Rights reserved.//#import<Foundation/Foundation.h>intMain (intargc,const Char* argv[]) {@autoreleasepool {Chara=' A '; intb=1; floatc=8.5; Doubled=8.5e+12; Chare=' A ';        BOOL F=yes,g=no; NSLog (@"A=%c,b=%i,c=%f,d=%f,e=%c,f=%i,g=%i", a,b,c,d,e,f,g); //Result: a=a,b=1,c=8.500000,d=8500000000000.000000,e=a,f=1,g=0nsstring *[email protected]"hello,world!"; NSLog (@"str=%@", str); //Result: str=hello,world! NSLog (@"Object=%p", nil);//Result: object=0x0}    return0;}

About the above ID type here does not do too much explanation, later we will specifically, the following list of common format characters:

Attention

1. In C or OBJC can use%i can also use%d to format, only use the scanf () input when the difference between the two (because%d is always interpreted by the compiler as a decimal intergeer so even if the input 010 is considered to be 10, and the% I will consider the preceding 0 or 0x as the input is an octal or hexadecimal integer);

2.%p essence is the output pointer address, under the 32-bit compiler for a pointer type using the%x output and%p output is actually the same, but the latter will be preceded by "OX" (note that under the 64-bit compiler because the pointer length of 8byte, and shaping only 4byte so%x output content is only% P's low data);

iOS Development Series-OBJECTIVE-C Basic Overview OC Formatted output example

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.