Objective-C today's note

Source: Internet
Author: User
Tags add time

Keep writing logs.

Differences between printf and nslog

Nslog will automatically add line breaks without adding line breaks. nslog will add time and process information, while printf will only output the input content without adding anything additional. The input types of the two are also different. nslog expects nsstring *, while printf expects const char *. The most essential difference lies in the log output by nslog. Under debug, nslog output is written to system. Log, while printf does not have the log attribute.

Nslog Definition

Nslog is defined in nsobjcruntime. H, as follows:

Void nslog (nsstring * format ,...);

Basically, nslog is similar to printf and will also output the display result on the console. The difference is that the formatted characters passed in are nsstring objects, rather than char * string pointers.

Nslog can be used as follows:

Nslog (@ "this is a test ");

Nslog (@ "string is: % @", string );

Nslog (@ "x = % d, y = % d", 10, 20 );

However, the following statement cannot be used:

Int I = 12345;

Nslog (@ "% @", I );

The reason is that % @ needs to display the object, while int I is obviously not an object. to display the object correctly, write it:

Int I = 12345;

Nslog (@ "% d", I );

The nslog format is as follows:

% @ Object

% D, % I integer % u unsigned integer % F floating point/double character % x, % x binary integer % O octal integer % zu size_t % P pointer % E floating point/double character (Scientific Computing) % G floating point/double character % s c string %. * s Pascal string % C character % C unichar % LLD 64-bit long integer (long) % LlU no character 64-bit long integer % lf 64-bit double character

The statement starts with a common prefix and a return type in parentheses:

-(Void)

The parameter type is specified in parentheses, and ":" is the most important component of a method name.

If the method uses parameters, a colon is required; otherwise, no.

In objective-C language, it is very easy to create a class. It is typically divided into two parts.

The class interface is usually stored in the classname. h file, which defines the instance parameters and some public methods.

Class implementation is in the classname. M file. It contains the real running code and the methods. It also often defines some private methods. These private methods are invisible to sub-classes.

Here is a rough idea of an interface file. Class Name photo, so the file name is photo. h:

# Div_code IMG {border: 0px none ;}

# Import

@ Interface photo: nsobject {

Nsstring * Caption;

Nsstring * photographer;

}

@ End

 

First, we import cocoa. h. Most of the basic classes of cocoa applications do this. # The import macro command automatically prevents the same file from being contained multiple times.

The @ interface symbol indicates that this is the declaration of the photo class. The colon specifies the parent class. In the preceding example, the parent class is nsobject.

In the large arc, there are two variables: caption and photographer. Both are of the nsstring type. Of course, they can also be any other type, including the ID type.

Finally, @ end ends the entire statement.

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.