Some details in the OC class (Object Storage and struct usage in the class)

Source: Internet
Author: User

I. method name in oC

Note: Both the method name colon and the and method name colon are the method names.

I. Storage details of Objects

The class is loaded into the code area (including the attributes and methods in the class), the object is dynamically loaded into the heap memory, and the pointer to the object is stored in the stack area.

Iii. Common Definition errors

Common Errors when defining classes

1) class definitions cannot be nested

2) do not miss @ end

3) do not forget to write the implementation class. (If you forget to write the class, no errors will be reported during compilation and no errors will be reported during running)

4) braces that define member variables are often missed.

5) If you do not write @ interface, only @ implementation is acceptable, but a warning is reported. We recommend that you do not write this statement.

 

6) write the method declaration to the brackets in which the member variables are declared.

4. struct issues in the class:

# Import <Foundation/Foundation. h>

Typedef struct {

Int year;

Int month;

Int Day;

} Mydate;

// Class declaration

@ Interface Student: nsobject

{

@ Public

Nsstring * _ name;

Mydate _ birthday; // mydate is a struct

}

@ End

// Class implementation

@ Implementation student

@ End

Int main (INT argc, const char * argv [])

{

// Instantiate the object Stu

Student * Stu = [STUDENT new];

// Assign a value

Stu-> _ name = @ "Ray ";

// Stu-> _ Birthday = {, 11} cannot be written. _ birthday is initialized to 0 when the object is created, and cannot be initialized as a whole.

// Stu-> _ birthday. Year = 1999;

// Stu-> _ birthday. month = 11;

// Stu-> _ birthday. Day = 11;

// D1 is a struct variable.

Mydate d1 = {1988,11, 11 };

// Copy the D1 value to _ birthday

Stu-> _ Birthday = D1; // pass the value

For example :/*

Mydate date = {1990,3, 9 };

Date = {, 9}; // an error is reported, indicating that the struct cannot be defined after initialization.

*/

V. nsstring

Nsstring usage

Nsstring is a string class of OC.

1) The nsstring stores the OC string

Nsstring * STR = @ "l love China! ";

2) since it is a class, we can instantiate an object.

Nsstring * s = [nsstring new];

1) Allocate space 2) initialize 3) return address

S = @ "I love itcast ";

3) format the string

Nslog (@ "% d, % F", a, B );

 

Nsstring * S2 = [nsstring stringwithformat: @ "xxxxx_%02d.jpg", I];

 

STR = @ "Hello China! ";

// A method for calculating the length of a string

// OC String Length Calculation Method

// 1. [STR length], which does not contain \ 0, indicates the actual number of characters.

// 2. Use a Chinese character and a common letter as one character.

Nslog (@ "str Len = % lu", [STR length]);

// C Language Character Length

// 1. The common character occupies 1 byte

Char * SSS = "China! ";

// 2. A Chinese Character occupies 3 bytes (UTF-8 encoding), and a character still occupies 1

SSS = "China! ";

 

Some details in the OC class (Object Storage and struct usage in the class)

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.