ObjectC ---- several common classes

Source: Internet
Author: User

ObjectC ---- several common classes

// Create By Guo Zai March 31, 2015 20:54:20

1. NSString class

 

 

// Evaluate the string length

NSString * str = @ "Hello Guo String ";

NSUInteger len = [str length]; // NSUInterger is long

NSLog (@ "% ld", len );

 

// Obtain the substring

 

 

NSString * substr = [str substringFromIndex: 5]; // obtain the substring starting from subscript 5

NSLog (@ "% @", substr );

// The End Of The substring from the start to the position where the subscript is 5

NSString * substr2 = [str substringToIndex: 5];

NSLog (@ "% @", substr2 );

 

Nsange rang = {2, 3}; // start position and length

// Obtain the substring of 3 from the starting position 2

NSString * substr3 = [str substringWithRange: rang];

NSLog (@ "% @", substr3 );

 

// Concatenate a string

 

NSString * comStr1 = [str stringByAppendingString: @ "IOS"];

NSLog (@ "% @", comStr1 );

 

NSString * comStr2 = [str stringByAppendingFormat: @ "hehe % @", @ "hhh", @ "jjj"];

NSLog (@ "% @", comStr2 );

 

 

// String replacement

 

NSString * newStr1 = [str stringByReplacingOccurrencesOfString: @ "string" withString: @ "world"];

NSLog (@ "% @", newStr1 );

 

 

// Judge whether the string is equal

// Use the iseconto method to determine whether the string is equal. The = method cannot be used to determine whether the string is equal.

// Whether the content of the string is equal when the issimilar to method is used, and = whether the two pointers point to the same address

 

NSString * cmpStr = @ "Hello String ";

BOOL eq = [str isw.tostring: cmpStr];

If (eq ){

NSLog (@ "equal ");

}

Else {

NSLog (@ "not equal ");

}

// Determine whether the prefix is equal to the given string, that is, whether the string starts

BOOL prefix = [str hasPrefix: @ "Hello"];

If (prefix ){

NSLog (@ "starting with Hello ");

}

Else {

NSLog (@ "not starting with Hello ");

}

 

 

 

// Determine the suffix

// Determine whether the image has ended with png. If it is png, replace it with jpg. Otherwise, splice the image with jpg.

BOOL isPng = [str hasSuffix: @ "png"];

If (isPng ){

// Execute replacement

NSString * tem = [str stringByReplacingOccurrencesOfString: @ "png" withString: @ "jpg"];

NSLog (@ "% @", tem );

}

Else

{// Execute the mosaic jpg

NSString * tem = [str stringByAppendingString: @ "jpg"];

NSLog (@ "% @", tem );

}

 

 

 

========================================================== ========================================================== =====

2. NSMutableString // variable string

NSMutableString * mulStr = [NSMutableString stringWithString: @ "Hello"];

NSLog (@ "% @", mulStr );

 

// Splicing

[MulStr appendFormat: @ "guozai"];

NSLog (@ "% @", mulStr );

[MulStr appendString: @ "mutible"];

NSLog (@ "% @", mulStr );

 

 

// Delete the substring

Nsange ran = {4, 1}; // struct type

[MulStr deleteCharactersInRange: ran];

NSLog (@ "% @", mulStr );

 

// Replace

Nsange ran2 = {3, 2 };

[MulStr replaceCharactersInRange: ran2 withString: @ "yy"];

NSLog (@ "% @", mulStr );

 

// Insert

[MulStr insertString: @ "tt" atIndex: 2];

NSLog (@ "% @", mulStr );

 

 

// A new string is created for String concatenation, segmentation, and other operations of an immutable string NSString.

// For variable string NSMutableString, such as String concatenation, segmentation, replacement, and other operations are based on the original string

// Modify the string. No new string is created.

// NSMutableString is a subclass of NSString. Therefore, the NSMutableString method is also

// Available

// In the future study, any Mutable class will not have a subclass of the Mutable class:

// For example, NSMutableArray is a subclass of NSArray and NSMutableDictionary is a child of NSDictonary

// Class

========================================================== ========================================================== ==== 3. NSArray

 

// Array

// The final nil cannot be lost

NSArray * arr = [NSArray arrayWithObjects: @ "guozai", @ "guo", @ "zaiguo", nil];

 

// Obtain the number of array elements

NSUInteger count = [arr count];

NSLog (@ "% lu", count );

 

 

// Obtain the first object

NSString * p1 = [arr firstObject];

NSLog (@ "% @", p1 );

 

 

// Obtain the last object

NSString * p2 = [arr lastObject];

NSLog (@ "% @", p2 );

 

// Obtain the object corresponding to the subscript

NSString * p3 = [arr objectAtIndex: 1];

NSLog (@ "% @", p3 );

 

 

// Traverse the Array

For (int I = 0; I <[arr count]; I ++ ){

NSLog (@ "% @", [arr objectAtIndex: I]);

}

========================================================== ========================================================== ===== 4. NSMutableArray

 

// Variable array

// The content of one array is assigned to another array.

NSMutableArray * mulArray = [NSMutableArray arrayWithArray: arr];

// Delete the object whose subscript is index

[MulArray removeObjectAtIndex: 2];

 

// For (int I = 0; I <[arr count]; I ++ ){

// NSLog (@ "% @", [arr objectAtIndex: I]);

//}

//

// Add an object element

[MulArray addObject: @ "guoguo"];

 

// Exchange element objects corresponding to subscript

[MulArray exchangeObjectAtIndex: 0 withObjectAtIndex: [mulArray count]-1]; // swap the first and last elements

========================================================== ========================================================== ===== See the following example: you can use a variable array to manage the BOOk class. to add, delete, query, and modify a BOOK, there are two member variables: _ name, _ price;

Book * book1 = [[Book alloc] initWithName: @ "guozai1" andPrice: 10];

Book * book2 = [[Book alloc] initWithName: @ "guozai2" andPrice: 15];

Book * book3 = [[Book alloc] initWithName: @ "guozai3" andPrice: 13];

 

// Array assignment

NSMutableArray * books = [NSMutableArray arrayWithObjects: book1, book2, book3, nil];

 

Book * book4 = [[Book alloc] initWithName: @ "guozai4" andPrice: 12];

 

// Add a book

[Books addObject: book4];

 

// Delete a book

[Books removeObjectAtIndex: 2];

 

For (int I = 0; I <[books count]; I ++ ){

NSLog (@ "% @, %. 2f", [[books objectAtIndex: I] name], [[books objectAtIndex: I] price]);

}

 

 

// Find the book named guozai3 and print the price

For (int I = 0; I <[books count]; I ++ ){

If ([[books objectAtIndex: I] name] isw.tostring: @ "guozai3"]) {

NSLog (@ "% f", [[books objectAtIndex: I] price]);

}

}

 

// Sort the array by price from high to low

For (int I = 0; I <[books count]-1; I ++ ){

For (int j = 0; j <[books count]-I-1; j ++ ){

If ([books [j] price] <[books [j + 1] price]) {

[Books exchangeObjectAtIndex: j withObjectAtIndex: j + 1];

}

}

}

 

 

For (int I = 0; I <[books count]; I ++ ){

NSLog (@ "% @, %. 2f", [books [I] name], [books [I] price]);

}

========================================================== ========================================================== ========= 5. NSNumber: converts a basic data type to an object type.

// Convert the basic data type int to the object type

NSNumber * intNum = [NSNumber numberWithInt: 100]; // Constructor

NSMutableArray * ar = [NSMutableArray arrayWithObjects: intNum, nil];

NSNumber * tem = [ar objectAtIndex: 0];

 

// Convert the object type to the basic data type

Int result = [tem intValue];

NSLog (@ "% d", result );

========================================================== ========================================================== ========= 6. NSValue: converts struct into objects

// Convert a vertex into an NSValue object

NSPoint point = {1, 2 };

// Convert a struct into an NSValue object

NSValue * vPoint = [NSValue valueWithPoint: point];

 

// Convert a vPoint into a struct

NSPoint point2 = [vPoint pointValue];

// NSLog (@ "%. 2f, %. 2f", point2.x, point2.y );

 

// NSStringFromPoint can convert a vertex into a string

NSLog (@ "% @", NSStringFromPoint (point2 ));


The analogy of other examples is as follows:

 

// Convert the NSsize struct into an NSValue object

NSSize size = {22, 44 };

NSValue * sValue = [NSValue valueWithSize: size];

 

// Convert the NSValue object into an NSSize struct;

NSSize size2 = [sValue sizeValue];

NSLog (@ "% @", NSStringFromSize (size2 ));

========================================================== ========================================================== ==========

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.