In objective-C, several common classes in foundation 1

Source: Internet
Author: User

To use the prompt function of xcode, you only need to remember the class name and common functions. Other functions can be viewed as prompted. Press ESC to view functions of this class.

Foundtion framework

The cocoa program mainly uses two frameworks: Foundation and applicationkit (uikit). The foundation framework mainly defines some basic classes, and applicationkit mainly defines several basic classes for Mac development, in iOS, uikit is used for interface development. All classes in the foundation framework inherit from nsobject, which is called god. Foundation mainly provides some functions that are not directly related to the graphic user interface, such as strings, values, container sets, and so on.

1. Processing of numeric objects

Package numbers into numeric objects

Int age = 24; bool ismarry = no; float Pi = 3.14f; // use the class method. The other basic data types are the same as nsnumber * myage = [nsnumber numberwithint: age]; nsnumber * aboutmarry = [nsnumber numberwithbool: ismarry]; // use the initialization method nsnumber * aboutpi = [[nsnumber alloc] initwithfloat: Pi];

Convert a numeric object to a basic data type.

    age = [myAge intValue];    isMarry = [aboutMarry boolValue];    pi = [aboutPi floatValue];
 

2. Common application of strings

Since OC is based on C, for the sake of distinction, the string in OC must start with @, and the class content in the quotation marks inside @ is the content of the string itself.

Once an nsstring object is created, it cannot be modified. If you want to create a String object that can be modified, use nsmutablestring. the NSS ring here is like the string class in Java, the nsmutablestring class is like stringbuffer in Java.

// Create a string // method 1 nsstring * name = @ "Jim Green "; // method 2 // create an empty string nsstring * name1 = [nsstring alloc] init]; // instance method // create a non-null string nsstring * name3 = [[nsstring alloc] initwithstring: @ "Jim Green"]; // create a formatting character string nsstring * myself = [[nsstring alloc] initwithformat: @ "I am % @, % d years old, know that the PI value is % F ", name3, age, Pi];

Comparison of strings:

// The following print result is equal to nsstring * str1 = @ "niao"; nsstring * str2 = @ "niao"; if (str1 = str2) {nslog (@ "equal");} else nslog (@ "not equal"); If ([str1 is1_tostring: str2]) {nslog (@ "equal ");} else nslog (@ "not equal"); // The print results here are also equal (created in the constant area) nsstring * str3 = [[nsstring alloc] initwithstring: @ "Ge"]; nsstring * str4 = [[nsstring alloc] initwithstring: @ "Ge"]; If (str3 = str4) {nslog (@ "equal ");} else nslog (@ "not equal"); // The print here is also equal if ([str3 isw.tostring: str4]) {nslog (@ "equal ");} else nslog (@ "not equal "); //************************************** * ****************** demarcation line
// Create nsstring * str5 = [nsstring stringwithformat: @ "Ni % d", 5]; nsstring * str6 = [nsstring stringwithformat: @ "Ni % d ", 5]; // print here not equal if (str5 = str6) {nslog (@ "5 equals 6");} else nslog (@ "5 not equal 6 "); // The print here is also equal if ([str5 isequaltostring: str6]) {nslog (@ "5 equals 6");} else nslog (@ "5 not equal 6 ");

The = and isbetween tostring values above the demarcation line are equal, the = print values below the demarcation line are not equal, and the isbetween tostring values are equal. It is easy to understand that isbetween tostring values are equal, comparison with equal signs

That's because the = is used to compare the address of the Object Pointer, not the object itself.

Objects above the demarcation line are created in the open-light area, so the = comparison is equal.

However, the decomposition is first created in the heap, so the = comparison is not equal.

Str5 and str6 create two objects in the heap, so the address of the object is different. So the = is different, and the content of the object is the same, so isdomaintostring is the same.

Compare the size of a string

Nsstring * str7 = @ "A"; nsstring * str8 = @ "B"; nsstring * str9 = @ "A"; nslog (@ "comparison result: % @", [str7 compare: str8]? @ "Yes": @ "no"); // comparison result: Yes // ignore case-sensitive nslog (@ "ignore Case 1, % lD", [str7 caseinsensitivecompare: str8]); // ignore Case 1,-1 nslog (@ "ignore case 2, % lD", [str9 caseinsensitivecompare: str7]); // ignore case 2, 0

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.