Object, objectid

Source: Internet
Author: User

Object, objectid

Object-c OOP, source code organization, Foundation framework details 1

1.1 So what is OOP? OOP is a way of constructing software composed of objects. Objects are like little machines living inside your computer and talking to each other to get work done.

Oop is a software composed of objects. Objects are like some small machines in your computer and work through conversations.

 

1.2 Source File organization

If you use. mm for the file extension, you're telling the compiler you 've ve written your code in Objective-C ++, which lets you use C ++ and Objective-C together.

If you use the. mm file, you are telling the compiler that you are writing code with oc ++.

 

1.3

1.3.1 a quick tour of the foundation kit

Foundation framework has a bunch of useful low-level, data-oriented classes and types. We'll be visiting a number of these, such as NSString, NSArray, NSEnumerator, and NSNumber.

The basic framework has a series of low-level data-oriented classes and types.

 

Foundation framework is built on top of another framework called CoreFoundation.

The basic framework is built on the core framework.

If you come into SS function names or variable names that start with "CF," they are part of CoreFoundation.

Most of them have equivalents in Foundation framework, and some of them can be easily converted from one to the other.

 

1.3.2 Some useful types

Home on the range

 

Typedef struct _ nsange

{

Unsigned int location;

Unsigned int length;

} Nsange;

 

First, you can assign the field values directly:

Nsange range;

Range. location = 17;

Range. length = 4;

Second, you can use the C aggregate structure assignment Mechanic (doesn't that sound impressive ?) :

Nsange range = {17, 4 };

Finally, Cocoa provides a convenience function called NSMakeRange ():

Nsange range = NSMakeRange (17, 4 );

1.3.3 Geometric types

You'll often see types that deal with geometry and have the prefix "CG," such as CGPoint and CGSize. These types are provided by the Core Graphics framework, used for 2D rendering.

CG prefix. These types are provided by the core Graphics Library Framework. Used for 2d display.

 

CGPoint represents an (x, y) point in the Cartesian plane: Cartesian coordinate system coordinate

Struct CGPoint

{

Float x;

Float y ;};

CGSize holds a width and a height:

Width and height

Struct CGSize

{

Float width;

Float height;

};

 

Cocoa provides a rectangle type, which is a composition of a point and a size:

Struct CGRect

{

CGPoint origin;

CGSize size;

};

Cocoa gives us convenience functions for making these bad boys too: CGPointMake (), CGSizeMake (), and CGRectMake ().

 

1.3.4 String us along

Cocoa's NSString has a bunch of built-in methods that make string handling much easier.

NSString has many internal methods that make it easier to process strings.

 

1.3.4.1Build That String

NSString's stringWithFormat: method creates a new NSString just like that, with a format and arguments:

When you declare a method with the plus sign, you 've marked the method as a class method.

When you declare a method with a plus sign (+), you mark this method as a class method.

Class methods used to create new objects are called factory methods.

Class methods are usually used to create new objects, called factory methods.

 

1.3.4.2 Size matters length Problem

Another handy NSString method (an instance method) is length, which returns the number of characters in the string:

-(NSUInteger) length;

 

NSUInteger length = [height length];

 

1.3.4.3 Comparative Politics comparison Policy

IsEqualToString: compares the caller (the object that the message is being sent to) with a string that's passed in as an argument. isEqualToString: returns a BOOL (YES or NO) indicating if the two strings have the same contents. it's declared like this:

-(BOOL) isw.tostring: (NSString *) aString;

To compare strings, use the compare: method, which is declared as follows:-(NSComparisonResult) compare: (NSString *) aString;

[@ "Export dvark" compare: @ "zygote"] wocould return NSOrderedAscending.

 

1.3.4.4insensiti1_training

Compare: does a case-sensitive comparison. in other words, @ "Bork" and @ "bork", when compared, won't return NSOrderedSame. there's another method, compare: options:, that gives you more control:

-(NSComparisonResult) compare: (NSString *) aString

Options: (NSStringCompareOptions) mask;

For example, if you want to perform a comparison ignoring case but ordering numbers correctly, you wowould do this:

If ([thing1 compare: thing2 options: NSCaseInsensitiveSearch | NSNumericSearch]

= NSOrderedSame)

{

NSLog (@ "They match! ");

}

 

1.3.4.5 Is it inside? Inside?

The first checks whether a string starts with another string, and the second determines if a string ends with another string:

-(BOOL) hasPrefix: (NSString *) aString;

-(BOOL) hasSuffix: (NSString *) aString;

And you 'd use these methods as follows:

NSString * fileName = @ "draft-chapter.pages ";

If ([fileName hasPrefix: @ "draft"])

{

// This is a draft

}

If ([fileName hasSuffix: @ ". mov"])

{

// This is a movie

}

If you want to see if a string is somewhere inside another string, use rangeOfString:-(nsange) rangeOfString: (NSString *) aString;

Nsange range = [fileName rangeOfString: @ "chapter"];

1.3.4.6 Mutability

NSStrings are immutable. Cocoa provides a subclass of NSString called NSMutableString. Use that if you want to slice and dice a string in place.

You can create a new NSMutableString by using the class method stringWithCapacity:, which is declared like so:

You can use the class method to create NSMutableString

+ (Id) stringWithCapacity: (NSUInteger) capacity;

The capacity is just a suggestion to NSMutableString, like when you tell a teenager what time to be home.

This capacity is just a suggestion, as you tell young people when to go home.

Once you have a mutable string, you can do all sorts of wacky tricks with it. A common operation is to append a new string, using appendString: or appendFormat:, like this:

-(Void) appendString: (NSString *) aString;

-(Void) appendFormat: (NSString *) format ,...;

NSMutableString * string = [NSMutableString stringWithCapacity: 50];

[String appendString: @ "Hello there"];

[String appendFormat: @ "human % d! ", 39];

You can remove characters from the string with the deleteCharactersInRange: method:

-(Void) deleteCharactersInRange: (nsange) aRange;

 

NSMutableString * friends = [NSMutableString stringWithCapacity: 50];

[Friends appendString: @ "James BethLynn Jack Evan"];

Nsange jackRange = [friends rangeOfString: @ "Jack"];

JackRange. length ++; // eat the space that follows

[Friends deleteCharactersInRange: jackRange];

 

 

 

 

 

 

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.