Start from scratch-07. common structures of the foundation framework

Source: Internet
Author: User

I. Use of common structures of the foundation framework: nsange \ cgrange, nspoint \ cgpoint, nssize \ cgsize, nsrect \ cgrect

1. Basic usage:

// Use nsange R1 = nsmakerange (2, 4) for the nsange; // The first parameter is. location, the second parameter is. length nsstring * STR = @ "Learning OC"; nsange range = [STR rangeofstring: @ "Learning"]; // locate the position and length of a string in the STR string nslog (@ "loc = % lD, Len = % lD", range. location, range. length); nsange range1 = [STR rangeofstring: @ "not found"]; // searches for the position and length of a string in the STR string nslog (@ "loc = % d, len = % lD ", range1.location, range1.length); // if the location cannot be found, the return value of location is-1. A negative number is generated only when the location value is output to % d, by default, % LD is unsigned. // cgpoint is used for cgpoint point1 = nsmakepoint (10, 10); nspoint point2 = cgpointmake (10, 10 ); // This method is often used to define a cgpoint object // nsszie's nssize size1 = cgsizemake (10, 20); cgsize size2 = nsmakesize (10, 20 ); // use cgrect rect1 = cgrectmake (0, 0, 50, 50); cgrect rect2 = {point1, size1 }; // Method for printing struct nsstring * s = nsstringfrompoint (point1); nslog (@ "% @", S); nsstring * S1 = nsstringfromrect (rect1 ); nslog (@ "% @", S1 );

2. Other related methods

// Determine whether the struct nspoint \ cgpoint, nssize \ cgsize, nsrect \ cgrect contains the same value cgpointequaltopoint (point1, point2); cgrectequaltorect (rect1, rect2); reset (rect1, rect2); // determines whether a rect contains a certain point. The framework coregraphic must be referenced. to call the cgrectcontainspoint method bool b1 = cgrectcontainspoint (rect1, point1); nslog (@ "% d", B1); Return 0;

Ii. common foundation framework classes

1. nsstring

// Create a string nsstring * S1 = @ "test"; nsstring * S2 = [[nsstring alloc] initwithformat: @ "test is % C", 'C']; // This method can convert a C string to an OC string nsstring * S3 = [[nsstring alloc] initwithuf8string: "name"]; // convert the OC string to the C string const char * cs = [S3 utf8string]; nsstring * S4 = [[nsstring alloc] initwithcontentsoffile: @ "/users/lpmac/desktop/My. text "encoding: nsutf8stringencoding error: Nil]; [nsstring stringwithcontentsoffile: @"/users/lpmac/desktop/My. text "encoding: nsutf8stringencoding error: Nil]; nsurl * url = [[nsurl alloc] initwithstring: @" file: // users/lpmac/desktop/My. text "]; // method of the class [nsurl urlwithstring: @" file: // users/lpmac/desktop/My. text "]; nsstring * S5 = [[nsstring alloc] initwithcontentsofurl: URL encoding: nsutf8stringencoding error: Nil]; // method of the class [nsstring stringwithcontentsoffile: @" file: /// users/lpmac/desktop/My. text "encoding: nsutf8stringencoding error: Nil]; nslog (@" % @ ", S5 );

2. Use of nsmutablestring (Dynamic string)

Nsmutablestring * mutablestring1 = [nsmutablestring stringwithformat: @ "test"]; // concatenate a string [mutablestring1 appendstring: @ "APPEND Str"]; // Delete the nsange range = [mutablestring1 rangeofstring: @ "test"]; [mutablestring1 deletecharactersinrange: range]; nslog (@ "% @", mutablestring1 );

3. nsarray and nsmutablearray

// Nsarray * array = [nsarray arraywithobject: @ "test"]; nsarray * array1 = [nsarray arraywithobjects: @ "test1", @ "Test2 ", @ "Test2", nil]; nsarray * array2 = @ [@ "test1", @ "Test2", @ "Test2"]; // advocate quick writing // array access [array1 objectatindex: 0]; array1 [0]; // advocate // array traversal for (INT I = 0; I <array. count; I ++) {nslog (@ "% @", array [I]);}
// Quickly traverse for (id obj in array2) {nsuinteger loc = [array2 indexofobject: OBJ]; nslog (@ "% @, % lD", OBJ, Loc );} [array2 enumerateobjectsusingblock: ^ (id obj, nsuinteger idx, bool * Stop) {nslog (@ "% @, % lD", OBJ, idx); If (idx = 1) {* Stop = Yes ;}}];

Nsmutablearray is a dynamic array and cannot be initialized using the syntax @ [].

Start from scratch-07. common structures of the foundation framework

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.