OC Foundation First Day

Source: Internet
Author: User

One. Basic OC Syntax

1. source file suffix name

C program's source file suffix named. c

The suffix of the source file for the OC program is. M message. Message mechanism.

OC is fully compatible with C language. So in the. m file, you can write arbitrary C code.

The. m file. You can write OC Code, C code, C + + code.

2. The main function is still the entry and exit of the OC program.

The return value represents the end state of the program.

Parameters can receive the user's data.

You can also take no parameters.

3. #import指令.

1). This is 1 preprocessing instructions. will be executed before compiling.

2). Function: is an enhanced version of the #inlcude directive.

At the time of precompilation, copy the contents of the subsequent files to the place where the instructions were written.

3). Enhancement: The same 1 files will only be included 1 times regardless of the import number.

4). If you want to use the #inlcude command! To achieve this effect, it will be more troublesome.

Must be implemented with #ifndef directives.

5). #import指令的原理: When 1 files are included. The underlying will first determine if the file is contained.

If it is included, it will skim if it is not included.

You do not need to use conditional compilation directives.

6). Use of the import directive

#import "path" first find the directory where the current source file is located and not go to the Xcode compiler directory to find.

#import < path > Find the Xcode compiler directory directly.

If it's our own file. Don't put it in the same directory as the current source file.

"" Contains your own files

<> files that contain the system.

4. Framework

Apple or a third party writes some of the features of Ox x (commonly used) in advance. Encapsulate these functional modules in 1 1 classes.

Make these features available to everyone. The collection of these classes is called the framework.

The framework is plainly a function set. These features are written by Apple or other experts. We're going to use a direct call.

Equivalent to a library of functions in the C language. Location .....

Foundation framework:

Provides some of the most basic functions of OC. Support for data types, input and output .....

Address of Foundation.h:

/applications/xcode.app/contents/developer/platforms/macosx.platform/developer/sdks/macosx10.10.sdk/system/ Library/frameworks/foundation.framework/headers

Foundation.h This file contains all the header files in the foundation framework.

So, as long as we include this header file, it's equivalent to including all the header files in the foundation framework.

So we can use all of the things in the foundation framework as soon as we write this.

5. @autoreleasepool This is 1 automatic release pools

1) code can be written directly in the pool.

2) can also be deleted so far with no effect.

6. NSLog function.

1). is an enhanced version of the printf function.

2). Function: Output information to the console.

3). Enhancement of the place.

A. Automatic line wrapping, do not need to append the ' \ n ' nslog function will automatically wrap after the output information is complete.

B. Some other information related to the program is also exported at the same time as the output information.

---Shows the time to execute this code is specific to milliseconds.

Show this code is executed by those 1 programs.

Process ID PID When the program is running

Thread ID.

The last side is the information we output.

4). printf supports the use of format controls. The NSLog function supports all the format controls of the printf function and is used the same way.

5). Note

A. The string parameter of the NSLog function must be preceded by a 1 @ symbol.

B. The NSLog function defaults to wrapping after the output information is complete.

If we manually add ' \ n ' at the back of the string, the NSLog function will fail.

6). From now on, forget the printf function using the NSLog function.

7. A string in OC.

1). How to store the string in the C language.

A. Using character arrays to store

Char str[] = "Jack";

B. Use the character pointer to save the address stored in the constant area string.

Char *str = "Rose";

2). In OC, 1 new types are specifically designed to hold strings. We don't have to worry about how the interior is implemented.

A pointer variable of 1 nsstring types was designed. Use this pointer variable to save the address of the string.

The string constants in OC must be prefixed with an @ symbol of 1.

@ "Jack" @ "Rose" @ "Beijing I Love You" is the OC string.

"@jack" This is a C-language string. The @ Sign of the OC string constant is written outside the double quotation mark.

"Jack" "Rose" "Beijing Europe You dare" This is the C language string.

A string constant of OC uses a pointer variable of type NSString to save the address.

3). Pointer variables of type nsstring can only hold OC strings. The string assigned to it must be prefixed with the @ symbol.

The C language string cannot be saved.

4). Use the%@ format control character to output a string of type NSString.

5). The 1th parameter of the NSLog function is 1 nsstring type so the string can only give 1 OC strings and cannot give 1 C language strings.

8. NS Prefix

1). NextStep---Cocoa was actually developed by next company.

In order to differentiate these things, the predecessor was NeXTSTEP for their home. 1 prefixes NS

9. @ symbol.

1). Converts a C-language string to a string of OC.

"Jack"

@ "Jack"

2). Most of the new keywords in OC start with @.

@autoreleasepool

@interface

@implementation

@public

@protected

.........

10. Notes

Same as C language.

11. Definition and invocation of functions.

Same as C language.

OC Foundation First Day

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.