Common structures and classes of the Foudation framework

Source: Internet
Author: User

Represents the struct of the range: nsange:
There are three ways to create a new nsange:

1. nsange range;
Range. location = 17;
Range. length = 4;
2. nsange range = {17, 4 };
3. nsange range = NSMakeRange (17,4); (recommended)

Represents the data type used to process ry: NSPoint (point coordinate) and NSSize (length and width) also have a rectangular data class
Type (composed of vertices and sizes) NSRect


Cocoa provides methods to create these data types:

NSMakePoint (),

NSMakeSize ()

NAMakeRect ()


NSString
NSString * heigth = [NSString stringWithFormat: @ "You heigth is % d feet, % d inches", 5, 11];


The created class object contains a pointer to the superclass, a class name, and a pointer to the list of class methods. The Class Object also contains a long data and specifies the size for the newly created class object.
Returns the number of characters in a string:
Unsigned int length = [heigth length];


Returns the string comparison function of the Bool value:
-(BOOL) isw.tostring :( NSString *) aString; // compare the two strings to see if they are equal.


You can also use the compare: Method
-(NSComparisonResult) compare :( NSString *) string; // compare characters one by one


Case-insensitive comparison:
-(NSComparisonResult) compare :( NSString *) string options :( unsigned) mask;

Note: NSComparisonResult is an enumerated value. options is a bit mask, that is:
NSCaseInsensitiveSearch: case insensitive
NSLiteralSearch: complete comparison, case sensitive
NSNumericSearch: compares the number of characters in a string, not the character value.


Check whether the string starts with another string
-(BOOL) hasPrefix: (NSString *) aString;


Judge whether the string ends with another string
-(BOOL) hasSuffix: (NSString *) aString;


If you want to know whether a string contains other strings somewhere, use the rangeOfString: method.
-(Nsange) rangeOfString :( NSString *) aString;


NSString is unchangeable, and NSMutableString is variable. Use stringWithCapacity: To create NSMutableString * string = [NSMutableString stringWithCapacity: 42];
AppendString: Or appendFormat: Can Be Used to append a new string:
-(Void) appendString :( NSString *) aString;
-(Void) appendFormat :( NSString *) format ,...;
You can use deleteCharactersInRange to delete characters in a string.
-(Void) deleteCharactersInRange: (nsange) range;


Collection family:
NSArray: used to store an ordered list of objects (any type of objects)
Restrictions: only Objective-C objects can be stored, and basic data types (int, float, enum, struct, or random pointers in NSArray) of the C language cannot be stored ). At the same time, nil cannot be stored (the zero or NULL value of the object)


// Create a new NSArray

NSArray * array = [NSArray arrayWithObjects: @ "one", @ "two", nil];


// Obtain the number of objects contained
-(Unsigned) count;


// Obtain the object at the specified index
-(Id) objectAtIndex :( unsigned int) index;


Split array:
Use componentsSeparatedByString: To split NSArray,
NSString * string = @ "oop: ack: bork: greeble: ponies ";
NSArray * chunks = [string componentsSeparatedByString: @ ":"];


Use componentsJoinedByString: to merge elements in NSArray and create strings.
String = [chunks componentsJoinedByString: @ ":-)"];


NSArray is an unchangeable array, and the objects contained in the array can be changed, but the array object itself will not change.


Variable array NSMutableArray creates a variable array using the class method arrayWithCapacity:
+ (Id) arrayWithCapacity :( unsigned) numItems;
NSMutableArray * array = [NSMutableArray arrayWithCapacity: 17];


Use addObject: add an object to the end of the array
-(Void) addObject :( id) anObject


Delete objects of a specific index
-(Void) removeObjectAtIndex :( unsigned) index;


Note: Variable Arrays can also insert objects in specific indexes, replace objects, and sort arrays. NSArray also provides a lot of useful functions.
Enumeration:
NSEnumerator is used to describe the operation method of this set iterator:
To use NSEnumerator, You need to request the enumerator from the array through objectEnumerator:
-(NSEnumerator *) objectEnumerator;


You can use this method as follows:
NSEnumerator * enumerator;
Enumerator = [array objectEnumerator];


Note: You can use the reverseobjectEnumerator method to enumerate the set from the back to the front.
After obtaining the enumerator, start the while loop. Each loop requests its nextObject from this enumerator.
-(Id) nextObject; // The returned nil indicates that the loop ends.
Note: The array container cannot be changed during enumeration.


Quick enumeration example:
For (NSString * string in array ){
NSLog (@ "I found % @", string );
}


Array sorting:
For example, a record is the information of a card, including (NSString * name and NSString * email)
-(Void) sort
{
[Book sortUsingSelector: @ selector (compareNames :)]
}

Where:
@ Selector (compareNames :)
// Create a selector of the SEL type, sortUsingSelector: use this method to compare two elements in the array,
SortUsingSelector: The method needs to complete this comparison. It first calls the specified selector method, then sends a message to the first record of the array (receiver), and compares its parameters with this record. The Return Value of the specified method is of the NSComparisonResult type. The return value is NSOrderedAscending if it is smaller than the return value. NSOrderedSame is returned if it is equal to the return value. NSOrderedDescending is greater than the return value.


-(NSComparisonResult) compareNames :( id) element
{
Return [name compare: [element name];
}


NSDictionary: (a combination of keywords and definitions)
NSDictionary typically stores a value (which can be any Class Object) under a given keyword (usually an NSString string ). Then you can use this keyword to find the corresponding value.


Use dictionaryWithObjectsAndKeys: to create a dictionary
+ (Id) dictionaryWithObjectsAndKeys :( id) firstObject ,...


For example:
Tire * t1 = [Tire new];
NSDictionary * tires = [NSDictionary dictionaryWithObjectsAndKeys: t1, @ "front-left", nil];


How to Use objectForKey: to obtain the value in the dictionary
-(Id) objectForKey :( id) akey;


You can search for tires as follows:
Tire * tire = [tires objectForkey: @ "front-left"];
Create a new NSMutableDictionary object and send a dictionary message to the NSMutableDictionary class. You can also use the dictionaryWithCapacity: method to create a new variable dictionary.
+ (Id) dictionaryWithCapacity :( unsigned int) numItems;
You can use setObject: forKey: to add an element to the dictionary.
SetObject: forKey :( id) aKey


The following is another method for creating a dictionary by sending a dictionary message:
NSMutableDictionary * tires;
Tires = [NSMutableDictionary dictionary];
[Tires setObject: t1 forKey: @ "front-left"];
...
Note: If you use the setObject: forKey: Method for the existing keywords in the dictionary, replace it with a new one.
You can use removeObjectForKey: to delete a keyword in a variable dictionary.
-(Void) removeObjectForKey :( id) aKey;
Note: Do not create a subclass of NSString, NSArray, or NSDictionary. You can solve the problem in a composite manner.


Use this method to enumerate dictionaries:
For (NSString * key in g)
{
...
}


Set object:
Set is a Set of Single-value objects, which can be mutable or immutable. operations include searching, adding, and deleting members in a Set (only used for a variable Set), and comparing two sets, calculates the intersection and union of two sets.
# Import <Foundation/NSSet. h>
Common NSSet Methods

Method

Description

+ (Id) setWithObjects: obj1, obj2,..., nil

Create a new set with a column of Objects

-(Id) initWithObjects: obj1, obj2,..., nil

Use a column of objects to initialize the newly assigned set

-(NSUInteger) count

Returns the number of members of the set.

-(BOOL) containsObject: obj

Determine whether the set contains obj

-(BOOL) member: obj

Use isEqual: To determine whether the set contains obj

-(NSEnumerator *) objectEnumerator

Returns an NSEnumerator object for all objects in the set.

-(BOOL) isSubsetOfSet: nsset

Check whether every member of the aggreger is in nsset.

-(BOOL) intersectsSet: nsset

Determine whether at least one of the referers is in the nsset object.

-(BOOL) isw.toset: nsset

Determine whether the two sets are equal


Common NSMutableSet methods (subclass of NSSet)
Method Description

-(Id) setWithCapacity: size

Create a new set with the initial space for storing size members

-(Id) initWithCapacity: size

Set the newly allocated set to the storage space of size members.

-(Void) addObject: obj

Add object obj to collection

-(Void) removeObject: obj

Delete object obj from collection

-(Void) removeAllObjects

Delete all members of the recipient

-(Void) unionSet: nsset

Add all the nsset members to the receiver.

-(Void) minusSet: nsset

Delete the Left and Right nsset members from the recipient

-(Void) intersectSet: nsset

Delete all elements in the recipient that do not belong to nsset


Note: NSInteger is not an object. typedef of the basic data type is converted into 64-bit long or 32-bit int values by typedef:
NSNumber:
You can use an object to encapsulate basic values.
NSNumber class to encapsulate basic data types.
+ (NSNumber *) numberWithChar :( char) value;
+ (NSNumber *) numberWithInt :( int) value;
+ (NSNumber *) numberWithFloat :( float) value;
+ (NSNumber *) numberWithBool :( BOOL) value;
There are also unsigned versions and various long-type data and long integer data.
Example: NSNumber * number = [NSNumber numberWithInt: 42];
After encapsulating a basic type to NSNumber, you can obtain it again using the following methods:
Method description
+ (Id) setWithObjects: obj1, obj2,..., nil use a column of objects to create a new set
-(Id) initWithObjects: obj1, obj2,..., nil uses a column of objects to initialize the newly allocated set.
-(NSUInteger) count returns the number of members of the set.
-(BOOL) containsObject: obj determines whether the set contains obj
-(BOOL) member: obj use isEqual: method to determine whether the set contains obj
-(NSEnumerator *) objectEnumerator returns
NSEnumerator object
-(BOOL) isSubsetOfSet: nsset determines whether each of the referers is present in
Nsset
-(BOOL) intersectsSet: nsset determines whether at least one of the referers appears
In the object nsset
-(BOOL) isEqualToSet: nsset determines whether the two sets are equal
Method description
-(Id) setWithCapacity: size creates a new set and has the initial space to store size members.
-(Id) initWithCapacity: size: Set the newly allocated set to the storage space of size members.
-(Void) addObject: obj: Add object obj to the set
-(Void) removeObject: obj: delete object obj from the collection
-(Void) removeAllObjects deletes all the members of the recipient.
-(Void) unionSet: nsset adds all the members of the nsset object to the receiver.
-(Void) minusSet: nsset deletes the left and right members of nsset from the recipient.
-(Void) intersectSet: nsset deletes all elements in the recipient that do not belong to nsset.
-(Char) charValue;
-(Int) intValue;
-(Float) floatValue;
-(BOOL) boolValue;
-(NSString *) stringValue;
NSValue:
NSNumber is actually a subclass of NSValue. NSValue can encapsulate any value. You can use NSValue to put the structure into NSArray and NSDictionary.
Create a new NSValue:
+ (NSValue *) valueWithBytes: (const void *) value objCType: (const char *) type;
@ Encode the compiler command can accept the name of the Data Type and generate a suitable string for you.
NSRect rect = NSMakeRect (1, 2, 30, 40 );
NSValue * value;
Value = [NSValue valueWithBytes: & rect objCType: @ encode (NSRect)];
Use getValue: To extract a value (passing the address of the variable for storing this value) (first finding the address and then taking the value)
Value = [array objectAtIndex: 0];
[Value getValue: & rect];
Note: Cocoa provides a convenient way to convert common struct data into NSValue:
+ (NSValue *) valueWithPoint :( NSPoint) point;
+ (NSValue *) valueWithSize :( NSSize) size;
+ (NSValue *) valueWithRect :( NSRect) rect;
-(NSSize) sizeValue;
-(NSRect) rectValue;
-(NSPoint) pointValue;
NSNull:
If the attribute is NSNull under the keyword, it indicates that this attribute does not exist. If there is no value, it indicates that you do not know whether this attribute exists. [NSNull null] // always returns the same value
+ (NSNull *) null;
For example:
[Contast setObject: [NSNull null] forKey: @ "home"];
Access it:
Id home = [contast objectForKey: @ "home"];
If (home = [NSNull null]) {
...
}
NSFileManager allows operations on the file system (creating directories, deleting files, moving files, or obtaining File Information)
// Create an NSFileManager object
NSFileManager * manager = [NSFileManager defamanager manager];
// Replace the character '~ 'Replace with the main directory
NSString * home = [@"~ "StringByExpandingTildeInPath];
// Output file extension
-(NSString *) pathExtension

 

 

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.