Basic Framework Foundation in oC

Source: Internet
Author: User
• The foundation framework provides many useful classes, such as nsstring: String nsarray: array nsdictionary: dictionary nsdate: Date nsdata: Data nsnumber: Number 1. first, we will introduce nsstringa. nsstring creation 1) the most direct method (this is a constant string)

Nsstring * STR = @ "hehe ";

2) Format

Nsstring * STR = [nsstring stringwithformat: @ "My age is % d", 10];

Nsstring * STR = [[nsstring alloc] initwithformat: @ "My age is % d", 10];

3) read from the file

// Used to save the error message

Nserror * error = nil;

// Read the file content

Nsstring * STR = [nsstring stringwithcontentsoffile: @ "/users/MJ/desktop/test.txt" encoding: nsutf8stringencoding error: & error];

// If an error message exists

If (error ){

Nslog (@ "read failed, error cause: % @", [error localizeddescription]);

} Else {// if no error message exists

Nslog (@ "read Successful, file content: \ n % @", STR );

}

4) read from the URL

Nserror * error = nil;

Nsurl * url = [nsurl urlwithstring: @ "file: // users/MJ/desktop/test.txt"];

Nsstring * STR = [nsstring stringwithcontentsofurl: URL encoding: nsutf8stringencoding error: & error];

If (error ){

Nslog (@ "read failed, error cause: % @", [error localizeddescription]);

} Else {

Nslog (@ "read Successful, file content: \ n % @", STR );

}

2. nsstring Storage

Nsstring * STR = @ "HAHAHA ";

[STR writetofile: @ "/users/MJ/desktop/str.txt" atomically: Yes encoding: nsutf8stringencoding error: Nil];

[STR writetourl: [nsurl urlwithstring: @ "/users/MJ/desktop/str.txt"] atomically: Yes encoding: nsutf8stringencoding error: Nil];

3. Some important methods in the nsstring header file

1) convert all characters into uppercase letters-(nsstring *) uppercasestring; 2) convert all characters into lowercase letters-(nsstring *) lowercasestring3) change the first letter to uppercase, -(nsstring *) capitalizedstring4) Comparison of strings-(bool) isw.tostring :( nsstring *) astring; 5) string search. whether to start with astring-(bool) hasprefix :( nsstring *) astring; B. whether to end with astring-(bool) hassuffix :( nsstring *) astring; C. used to check whether the string contains astring-(nsange) rangeofstring :( nsstring *) astring; 6. some methods of string truncation. starting from the specified position from (including characters at the specified position) to the end-(nsstring *) substringfromindex :( nsuinteger) from; B. it is intercepted from the beginning of the string to the specified position to, but does not include the character-(nsstring *) substringtoindex :( nsuinteger) to; C. extract the substring-(nsstring *) substringwithrange (nsange) range; D. replacement with target-(nsstring *) stringbyreplacingoccurrencesofstring :( nsstring *) Target withstring :( nsstring *) Replacement, you do not need to remember these methods. You only need to understand these methods. When using these methods, you can find the corresponding methods in the header file.

Basic Framework Foundation in oC

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.