The first day of "Objective-c learning record"

Source: Internet
Author: User

1.Foundation Frame

Reference a picture

Cocoa can be seen as a collection of many frameworks, including the foundation framework and the Uikit framework in iOS, where the foundation framework is primarily API-ready for program development, and Uikit includes many UI-drawn functions.

2. String manipulation

Initialize string:

NSString *STR1 = [[NSString alloc] init];

STR1 = @ "Hello,ios.";

NSString *STR2 = [[NSString alloc] initwithstring:@ "Hello,ios."];

NSString *STR3 = [[NSString alloc] initwithformat:@ "Age was%i,name is%.2f", 20,56.0f];

NSString *STR4 = [[NSString alloc] initwithutf8string:@ "Hello,ios."];

Format case:

Convert to lowercase

NSLog (@ "%@", [@ "Hello,ios." lowercasestring]);

Convert to uppercase

NSLog (@ "%@", [@ "Hello,ios." uppercasestring]);

First letter uppercase, other lowercase letters

NSLog (@ "%@", [@ "Hello,ios." capitalizedstring]);

Compare strings:

Judging if they are equal

BOOL res = [@ "abc" isequaltostring:@ "abc"];

Size comparison, there are three kinds of results: nsorderedascending/nsordereddescending/nsorderedsame

Nscomparisonresult res = [@ "abc" compare:@ "abc"];

if (res = = nsorderedascending)

NSLog ("Left < Right");

else if (res = = nsordereddescending)

NSLog ("Left > Right");

Else

NSLog ("left = right");

String prefixes and suffixes are judged:

[@ "abcdef" hasprefix:@ "abc"];

[@ "ABCdef" hassuffix:@ "def"];

To find a string:

Nsrange range = [@ "ABCDEFG" rangeofstring:@ "CD"];

if (range.location = = Nsnotfound)

NSLog (@ "not found.");

Else

NSLog (@ ' range is%@ ', Nsstringfromrange (range));//range is {2,2}

String segmentation:

NSLog (@ "%@", [@ "abcdef" substringfromindex:3]);//def

NSLog (@ "%@", [@ "abcdef" substringtoindex:3]);//abc

NSLog (@ "%@", [@ "abcdef" substringwithrange:nsmakerange (2,3)]);//CDE

NSString *str = @ "A.B.C.D.E.F";

Nsarray *array = [str componentsseparatedbystring:@ "];

NSLog (@ "%@", array);//(A,B,C,D,E,F);

Type conversions:

NSLog (@ "%i", [@ "intvalue]);//12

NSLog (@ "%zi", [@ "Hello,ios." length]);//10

NSLog (@ "%c", [@ "Hello,ios." characteratindex:0]);//h

const char *s = [@ ' Hello,ios. Utf8string];

NSLog (@ "%s", s);//hello,ios.

File operation:

Read File contents

NSString *path = @ "/users/shvier/desktop/demo.txt";

NSString *str = [NSString stringwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil];

NSLog (@ "%@", str);//hello,ios.

Write file contents

NSString *path = @ "/users/shvier/desktop/demo.txt";

NSString *str = @ "Hello,ios";

[Str writetofile:path atomically:yes encoding:nsutf8stringencoding error:nil];//atomically represents a one-time write, if an error occurs during writing, Don't write it all up.

The first day of "Objective-c learning record"

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.