Extension of C language in Objective-C Learning

Source: Internet
Author: User

Objective-CIn learningCIs the content to be introduced in this article,Objective-CAnd Cocoa are the core of Apple's Mac OS X operating system.Objective-C LanguageYesC LanguageMany applications with Mac OS X appearances are developed using this language. Cocoa is usedObjective-CThe toolkit contains not only all elements of the Mac OS X user interface, but also many other contents.

Objective-CThe file extension is. m, in Xcode ,. m file ,. c file Standard C program ),. cppC ++ program) files are compiled by GCCGNU Compiler Collection.

 
 
  1. #import 

AndC LanguageSimilarly, Objective-C uses header files to Contain element declarations, including struct, symbolic constants, and function prototypes.C LanguageUse the # include statement to notify the compiler to query the definition in the header file. In the Objective-C program, # import is usually used for this purpose.

# Import ensures that the header file is contained only once, no matter how many times the Command actually appears in that file. In C language, programmers usually use the # ifdef command-based solution to avoid the situation where one file contains another file, while the latter contains the first file. In Objective-C, programmers use # import to implement this function.

 
 
  1. #import <Foundation/Foundation.h>  

This statement tells the compiler to view the Foudation. h header file in the Foundation framework. The Foudation Framework processes features in layers under the user interface, such as data structures and communication mechanisms. Most programs in Objective-C are based on the Foundation framework.

 
 
  1. NSLog () and @ "string"

Cocoa adds the "NS" prefix to all its functions, constants, and type names, which tells programmers and compiler functions that come from Cocoa rather than other toolkit. When two different Toolkit use the same identifier, it will lead to name conflict, and the prefix can prevent this big problem. Since there is no centrally managed prefix registry, programmers can select any prefix. Many people use their first letter or company name as the prefix. Since Cocoa occupies the "NS" prefix, programmers should not use "NS" when adding a prefix to self-built variables or function names to avoid confusion.

The Cocoa function NSLog () is similar to printf () in C. It accepts a string as its first parameter, which can contain format specifiers such as % d ), you can also accept other parameters that match the format description. The difference between the NSLog () function and the printf () function is that the former adds some features, such as timestamp, date stamp, and automatically appended line break '\ n. Beauty note: this feature is very useful. All of my VC programs have a function called WriteLog (), which provides almost the same functionality as NSLog.

Let's take a look at this NSLog () Statement:

 
 
  1. NSLog(@"Hello, Objective-C!"); 

@ Symbol is one of the features added to Objective-C based on standard C language. There is a @ symbol before the string in double quotation marks, which indicates that the referenced string should be processed as an NSString element of Cocoa.

Most Cocoa elements are named in a very direct way, And the names try to describe the features they can implement. For example, NSArray provides arrays and NSDateFormatter helps programmers format dates in different ways. NSThread provides multithread conversion tools, and NSSpeechSynthesizer enables users to hear voice. Correspondingly, NSString is a string of characters in Cocoa.

The NSString type has many packaging features. Cocoa can be used whenever strings are needed. Below are some NSString functions.

(1) inform the length;

(2) compare itself with other strings;

(3) convert itself to an integer or floating point value.

Many other functions cannot be implemented using C-style strings. Further instructions will be made in future.

The printf () function does not have a format specifier corresponding to the NSString type, so we cannot pass it as a parameter to the printf () function. The NSLog () function adds the % @ format specifier to the NSString type, so that the NSLog () function can accept the corresponding parameter and use it as the NSString character in the string.

If you pass a C-style string instead of a special NSString @ "string" element) to NSLog (), the compiler will give a warning:

 
 
  1. main.m:46: waring: passing arg 1 of `NSLog` from incompatible pointer type  

If you want to run this program, it may crash. To capture such a problem, Xcode can always handle the alarm as an error.

Boolean Type

C LanguageBoolean data type bool, which has true and false values. Objective-C provides a similar type of BOOL, which has YES and NO values. These two different boolean types can coexist in the same program, but BOOL must be used when writing Cocoa code.

Objective-CBOOL in is actually a typedef defined for signed char of the signed character type), which uses an 8-bit storage space. YES is defined as 1, and NO is defined as 0. Use # define ).Objective-CBOOL is not treated as a boolean type that can only save the YES value or NO value. The compiler recognizes BOOL as an 8-bit binary number. The YES and NO values are just a convention. This raises a small problem: If you accidentally assign an integer value greater than 1 byte, such as a short or int value) to a BOOL variable, only the low byte will be used as the BOOL value. Assuming that the low byte is exactly 0, the BOOL value will be 0, that is, the NO value.

Summary:Objective-CIn learningC LanguageI hope this article will help you!

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.