OC, oc Language

Source: Internet
Author: User

OC, oc Language
OC language-07-OC language-Foundation framework structure

  • Nsange/CGRange

    • Used to indicate the range of an element in another element. nsange is equivalent to CGRange.
    • There are two attributes:

      • NSUInteger location: indicates the position of one element in another element.
      • NSUInteger length: the length of the element.
    • If the specified element cannot be found, NSNotFound (integer-1) is returned)
    • Common functions

      • Nsange NSMakeRange (NSUInteger loc, NSUInteger len)
        Create a variable of the nsange type and initialize its two attributes as loc and len.
        And return the created variable.
      • BOOL NSLocationInRange (NSUInteger loc, nsange range)
        Check whether a location is within a certain range and return a BOOL type data
      • BOOL NSEqualRanges (nsange range1, nsange range2)
        Determine whether the two ranges are equal when range1.location = range2.location
        And range1.length = range2.length is equal
      • NSString * NSStringFromRange (nsange range) returns a range
        Convert to string
      • Nsange NSStringFromRange (NSString * string) returns a string
        Convert to range
  • NSPoint/CGPoint

    • Used to store a vertex. NSPoint is equivalent to CGPoint.
    • There are two attributes:

      • CGFloat x: indicates the abscissa value of the vertex.
      • CGFloat y: indicates the ordinate value of the vertex.
    • Common functions

      • NSPoint NSMakePoint (CGFloat x, CGFloat y), used to create
        And use x and y to initialize member variables.
      • BOOL NSEqualPoints (NSPoint aPoint, NSPoint bPoint)
        Determine whether aPoint and bPoint are the same point
      • BOOL NSPointInRect (NSPoint aPoint, NSRect aRect) Judgment point
        Whether the aPoint is in the Rectangular aRect
      • NSString * NSStringFromPoint (NSPoint aPoint ),
        Convert to string
      • NSPoint NSPointFromString (NSString * aString ).
        String into a vertex
  • NSSize/CGSize

    • Used to store the size of an element. NSSize is equivalent to CGSize.
    • There are two attributes:

      • CGFloat width: indicates the width of the element.
      • CGFloat height: indicates the height of the element.
    • Common functions

      • NSSize NSMakeSize (CGFloat w, CGFloat h), used to create
        NSSize element, and initialize member variables with x and y
      • BOOL NSEqualSizes (NSSize aSize, NSSize bSize), determination ruler
        Is the size of an inch aSize equal to the size of bSize?
      • NSString * NSStringFromSize (NSSize aSize), The Size element aSize
        Convert to string
      • NSSize NSSizeFromString (NSString * aString) to convert the string
        Size element aSize
  • NSRect/CGRect

    • Used to store the size information of an element in the upper left corner and the element, NSRecta
      Equivalent to CGRect
    • There are two attributes:

      • CGPoint origin: used to represent the point in the upper left corner of the element.
      • CGSize size: used to indicate the size of the element.
    • Common functions

      • CGRect CGRectMake (CGFloat x, CGFloat y, CGFloat width,
        CGFloat height), used to create a CGRect variable, and initialized with x and y
        The member variable origin. Use width and height to initialize the member variable size.
      • Bool CGRectEqualToRect (CGRect rect1, CGRect rect2 ),
        Compare whether two rect1 and rect2 are equal
      • Bool CGRectContainsPoint (CGRect rect, CGPoint point ),
        Determines whether the point is in the element rect.
      • Bool CGRectContainsRect (CGRect rect1, CGRect rect2 ),
        Determine whether rect1 contains rect2. rect1 and rect2 are not empty and do not include boundaries.
  • 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
  • NSString

    • Is an OC class used to operate OC character strings
    • The created OC String object is unchangeable.
    • Generally, a class method corresponds to an object method that completes the same function.
    • , Returns an unchangeable string
    • Common class methods for creating strings (usually starting with a class name)

      • + (Instancetype) string to create an NSString object
      • + (Instancetype) stringWithString :( NSString *) string to create an NSString object and initialize it with the OC String
      • + (Instancetype) stringWithCharacters :( const unichar *) character slength :( NSUInteger) length, create an NSString object through a string pointer and a string of a certain length
      • + (Instancetype) stringWithFormat :( NSString *) format to synthesize a string
      • + (Instancetype) stringWithCString :( const char *) cStringencoding :( NSStringEncoding) enc: Create an OC character string using a C string
      • + (Instancetype) stringWithContentsOfURL :( NSURL *) url encoding :( NSStringEncoding) enc error :( NSError **) error, create an OC character string using the unified Resource Locator URL
      • + (Instancetype) stringWithContentsOfFile :( NSString *) path encoding :( NSStringEncoding) enc error :( NSError **) error; create an OC string through the file content
    • Common methods for creating string objects

      • Object methods are similar to class methods. They generally start with init and are not listed here.
    • Common string operations

      • -(Unichar) characterAtIndex :( NSUInteger) index, returns the character at the specified index position in the string
      • -(Void) getCharacters :( unichar *) buffer range :( nsange), aRange, locate the string at the specified position and store it in the buffer.
      • -(Void) getCharacters :( unichar *) buffer to obtain the string in the buffer.
      • -(NSString *) substringWithRange :( nsange) range, returns the substring of a range
      • -(NSComparisonResult) compare :( NSString *) string, compare two strings, return the variable enumerating NSComparisonResult
      • -(BOOL) containsString :( NSString *) aString to determine whether the string aString is included
      • -(Nsange) rangeOfString :( NSString *) aString, returns the range of aString in this string
  • NSMutableString

    • Inherit from NSString to create variable string objects
    • NSMutableString Extension Method

      • -(Void) replaceCharactersInRange :( nsange) range withString :( NSString *) aString, replacing the string of the specified range with aString
      • -(Void) insertString :( NSString *) aString atIndex :( NSUInteger) loc, insert the string aString at the index loc
      • -(Void) deleteCharactersInRange :( nsange) range: Delete the string within the specified range
      • -(Void) appendString :( NSString *) aString. concatenate the aString at the end of the string to expand the string.
      • -(Void) appendFormat :( NSString *) format, which is used to expand the string through a success-to-Child string format
      • -(BOOL) writeToFile :( NSString *) path atomically :( BOOL) useAuxiliaryFile, write the string to the file that represents the path in the string form
Set
  • NSArray/NSMutableArray

    • OC array. The array object is any OC object. Non-OC objects must be converted to corresponding OC objects.
      Object
    • The array element cannot be nil, and nil indicates the end of the OC array.
    • You can use @ [] to quickly return an unchangeable array.
    • Common methods for creating Arrays

      • + (Instancetype) array to create an empty array object
      • + (Instancetype) arrayWithObject :( id) anObject, which creates an array through the OC object
      • + (NSArray) ArrayWithContentsOfFile :( NSString) Path creates an array through the file content
      • + (NSArray) ArrayWithContentsOfURL :( NSURL) Url to create an array using the Uniform Resource Locator URL
    • Common methods for creating array objects

      • The object method is similar to the class method and starts with init.
    • Common array operation functions

      • -(NSUInteger) indexOfObject :( id) anObject, locate the index of the specified object
      • -(BOOL) isw.toarray :( NSArray *) otherArray to determine whether the elements in the two arrays are the same
      • -(NSArray *) sortedArrayUsingSelector :( SEL) comparator uses SEL to call a method to sort Arrays
      • -(NSArray *) subarrayWithRange :( nsange) range, returns the range of the array Neutron Array
      • -(Void) enumerateObjectsUsingBlock :( void (^) (id obj, NSUInteger idx, BOOL * stop) block to enumerate the elements in the array and call the block. It is usually used to traverse an array.
    • NSMutableArray inherits from NSArray and creates a variable array.
    • NSMutableArray Extension Method

      • -(Void) addObject :( id) anObject to add an OC object to the array
      • -(Void) insertObject :( id) anObject atIndex :( NSUInteger) index inserts an OC object at the specified index location
      • -(Void) removeObjectAtIndex :( NSUInteger) index: Delete the object at a certain index location.
      • -(Void) removeAllObjects: delete all array elements.
      • -(Void) replaceObjectAtIndex :( NSUInteger) index withObject :( id) anObject: Replace the object at the specified index location with the specified object
      • -(Void) removeObject :( id) anObject inRange :( nsange) range: delete an object in a certain range.
  • NSSet/NSMutableSet

    • NSSet, unordered array, immutable
    • NSMutableSet, unordered array, variable
  • NSDictionary/NSMutableDictionary

    • NSDictionary, unchangeable dictionary class
    • The element is Object: key-value pair. The key value must be unique and the key-value pair is unordered.
    • Common methods for creating dictionary objects

      • + (Instancetype) dictionaryWithObject :( id) object forKey :( id <NSCopying>) key, which creates a dictionary through key-value pairs
      • -(Instancetype) initWithObjects :( const id []) objects forKeys :( const id []) keys count :( NSUInteger) cnt, create a dictionary through the objects array and keys Array
      • + (Instancetype) dictionaryWithObjectsAndKeys :( id) firstObject,... create an array through multiple key-value pairs
    • Common dictionary operation functions

      • -(BOOL) isEqualToDictionary :( NSDictionary *) otherDictionary checks whether the two dictionaries are equal
      • -(NSArray) ObjectsForKeys :( NSArray) Keys notFoundMarker :( id) marker, returns an object array through a key-value Array
      • -(Void) enumerateKeysAndObjectsUsingBlock :( void (^) (id key, id obj, BOOL * stop) blocks use blocks to traverse dictionaries
      • -(Void) removeObjectForKey :( id) aKey, delete the object with the specified key value
      • -(Void) removeAllObjects: delete all objects.
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 ;}

 

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.