iOS Basics (ii) object-oriented thinking

Source: Internet
Author: User

Use the foundation framework when writing OBJECTIVE-C programs.

What is a framework?

A framework is a library of many classes (class) that you can use to write programs.

Object (Object)

    • objects can hold data and can contain a set of functions.
    • The function can use the data saved by the object.
    • A function that sends a message to an object that can be used to start a response.
    • This type of message-based function is called method.
    • OC is a weak grammar.
Create the first OBJECTIVE-C program

Creating a project, create a new Xcode project, OS X under Application, Command line Tool, named Firstoc, as shown in:

For files that contain object-c code, the suffix. M is generally used. Find main.m in Xcode and open and enter the following lines of code:

  #import  <foundation/foundation.h> int  Main (int  argc, const  char  * argv[]) {@autoreleasepool {NSLog ( @ " hello, world!   "                );        NSDate  *now = [NSDate Date]; NSLog ( @ " the date is%@   "  now);  return  0  ;}

In this code, the program sends a date message to the NSDate class, and the date method of the NSDate class creates an NSDate instance, initializes it to the current datetime, and returns the start address of the new object.

The program then saves the resulting address in the now variable, which is a pointer to the NSDate object.

Build and run. The output is roughly as follows:

------£º36.130 firstoc[67855: 3381337  Hello, world!. ---£º36.135 firstoc[67855  :3381337Thedate is 2015-03-16 12:44:36 +0000

NSLog () outputs the date, time, program name and process ID, then outputs the actual content, and The time information will be different .

NSLog () is a powerful output function provided by the foundation, similar in function to printf (), which can output not only strings, but also integers, C-cell strings and Objective-c objects.

1) The first parameter of NSLog () is a string constant, which is filled with the corresponding variable, using the%-formatted placeholder in the string.

Format characters supported by the NSLog () function

Format characters Description
%d output integers in decimal form
%o Output integers in eight binary form
%x Output integers in 16 binary form
%@ Output nstring string for "descriptive information" of the OC Object
%p Pointer to output object
%f Output floating point number
%s Output C-language strings

2) The formatted string for NSLOG () must be prefixed with @, and its output will be wrapped automatically, without the need to add the last \ n.

In Java programming, you use the System.out.println () method to output a string, which involves using a static keyword. System is a class within the Java.lang, and out is a data member within the system, but this member is not the base class, but the object of the Java.io.PrintStream class. Data members or methods that are modified by the static keyword can be referenced directly through the class name. data member or class name. method, without having to establish the object first. So System.out is applied out of this static data member. And out is a Java.io.PrintStream class object, so out must be able to call the Java.io.PrintStream class inside the method, println () is a method in the Java.io.PrintStream class, it is used to want the console output information.

 Public class firsthello{   publicstaticvoid  main (string[] args) {         System. out. println ("Hello xpws. " ):    }    }    

two ID

When you declare a pointer to an object, the class of the corresponding object is usually written out explicitly, as shown in the example above:

NSDate *now = [NSDate Date];

However, when programming, it is possible to encounter the following situation, when declaring pointers do not know the exact type of the object being referred to, you can use the ID type.

The meaning of the ID type is: pointer, and can point to any type of objective-c object, the following is the syntax description:

ID delegate ;

Examples of using ID examples to declare dates:

ID now = [NSDate Date]; NSLog (@ "Today is%@" , now);

Note: Asterisks are not used in this ze variable declaration, and the ID already implies the effect of asterisks.

iOS Basics (ii) object-oriented thinking

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.