OC, oc Language
OC language-07-OC language-Foundation framework structure
Nsange/CGRange
NSPoint/CGPoint
NSSize/CGSize
NSRect/CGRect
Example
# Import <Foundation/Foundation. h> int main () {@ autoreleasepool {BOOL result; // the return value NSString * str for storing the function; // store the converted string // create two vertex objects NSPoint point = NSMakePoint (5, 8); NSPoint point2 = NSMakePoint (50, 8 ); // create two size objects NSSize size = NSMakeSize (10, 20); NSSize size2 = NSMakeSize (10, 20); // create two rectangular objects NSRect rect = NSMakeRect (0, 0, 20, 30); NSRect rect2 = NSMakeRect (0, 1, 20, 30); // judge whether the point and point2 are the same result = NSEqualPoints (point, point2 ); // determine whether the point is in a rectangle rect result = NSPointInRect (point, rect); // determine whether point2 is in a rectangle rect result = NSPointInRect (point2, rect ); // determine whether the size and size2 are the same result = NSEqualSizes (size, size2); // determine whether the rect and rect2 are the same result = NSEqualRects (rect, rect2 ); // determine whether rect contains rect2 result = CGRectContainsRect (rect, rect2); // converts point, size, and rect into a string str = NSStringFromPoint (point ); str = NSStringFromSize (size); str = NSStringFromRect (rect);} return 0 ;}
String
Set
NSNumber and NSValue
NSValue
- OC class, used to encapsulate common type data
NSNumber
- It is inherited from NSValue and is usually used to convert common data types such as int and char to corresponding OC objects.
Common initialization Methods
- + (NSNumber *) numberWithChar :( char) value to convert the char type to an OC object
- Other methods are similar to the preceding method. You only need to replace char with other type names, such as int and long.
Common object initialization Methods
- -(NSNumber *) initWithInt :( int) value to convert int type data to OC object
- Other methods are similar to the preceding method. You only need to replace Int with the corresponding data type, such as char and short.
Common methods to operate NSNumber objects
- -(NSComparisonResult) compare :( NSNumber *) otherNumber compares two NSNumber objects and returns the enumerated type
- -(BOOL) isw.tonumber :( NSNumber *) number. Compare two NSNumber objects and return the BOOL type.
NSDate
Function
- OC class, used to store time information
- You can use NSDateFormatter to specify the time format.
Common Methods
- -(Instancetype) initWithTimeIntervalSinceReferenceDate :( NSTimeInterval) ti: Creates a time object and uses ti for initialization. NSTimeInterval is essentially a long object.
- -(NSTimeInterval) timeIntervalSinceDate :( NSDate *) anotherDate, returns the number of seconds from a certain time point to the Code call
- -(NSComparisonResult) compare :( NSDate *) other, compare two dates, return the enumeration type
- -(BOOL) is1_todate :( NSDate *) otherDate, compare whether two dates are equal
- \ TimeIntervalSince1970 is a member variable. You can call the get method to return the time (in seconds) that has elapsed since January 1, 1970)
- + (Instancetype) date, returns the current 0 Time Zone Time
Example
# Import <Foundation/Foundation. h> int main () {@ autoreleasepool {// defines an NSDate type, which is used to output the NSDate * date = [NSDate date] When the program is executed. // NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; formatter. dateFormat = @ "yyyy/MM/dd/HH: mm: ss"; // converts the time type to the string NSString * time = [formatter stringFromDate: date]; NSLog (@ "% @", time); // defines a range. It is used to specify the string of a range, such as nsange range = NSMakeRange (5, 4 ); // create an NSString object quickly. The storage name is NSString * str = @ "name: Jack"; // create an NSNumber object. The storage number is NSNumber * number = [NSNumber numberWithInt: 1]; // create an unchangeable array and store multiple name objects NSArray * objects = @ [str, @ "name: Lily", @ "name: Lucy"]; // create a variable array and store multiple student ID objects NSMutableArray * keys = [NSMutableArray arrayWithObjects: number, @ 2, @ 3, nil]; // create a variable dictionary, through the array objects, keys initializes NSMutableDictionary * dictionary = [[NSMutableDictionary alloc] initWithObjects: objects forKeys: keys]; // quickly traverses the objects array for (id obj in objects) {// locate the substring NSString * name = [obj substringWithRange: range]; NSLog (@ "% @", name);} specified range in obj );} // traverse the element [dictionary enumerateKeysAndObjectsUsingBlock: ^ (id key, id obj, BOOL * stop) {NSLog (@ "% @-% @", key, [obj substringWithRange: range]); if ([[obj substringWithRange: range] isdue to: @ "Jack"]) {// stop * stop = YES ;}}] when traversing to Jack; // clear the key-value pair with the key value of @ 1 [dictionary removeObjectForKey: @ 1]; // check the result. All the elements in the dictionary will be traversed and the [dictionary enumerateKeysAndObjectsUsingBlock: ^ (id key, id obj, BOOL * stop) will not be stopped) {NSLog (@ "% @-% @", key, [obj substringWithRange: range]); if ([[obj substringWithRange: range] isto to: @ "Jack"]) {// stop when traversal to Jack (this key-value pair is not in the dictionary and will not be stopped) * stop = YES ;}}] ;} return 0 ;}