Object-c OOP, source organization, Foundation framework detailed 1

Source: Internet
Author: User

? object-c? Oop?? Source organization??, Foundation framework? 1

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

Oop? is the software that is made up of objects. Objects are like small machines that exist in your computer and work through mutual dialogue.

?

1.2 Source File organization?

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

If you are using a. mm file, then you are telling the compiler that you are using oc++? in writing code.

?

1.3?

? 1.3.1? A quick tour of the foundation kit?

The 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 underlying framework has a series of low-level, data-oriented classes and types.

?

Foundation Framework is built on top of another framework called corefoundation.

The underlying framework is built on the core framework.

If you come across function names or variable names this start with "CF," they is part of Corefoundation.

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

?

1.3.2? Some useful types?

Home on the range?

?

typedef struct _NSRANGE

{

? unsigned int location;

? unsigned int length;

} Nsrange;

?

First, you can assign the field values directly:

Nsrange range;

Range.location = 17;

Range.length = 4;

Second, can use the C aggregate structure assignment mechanism (doesn ' t, this sound impressive?):

Nsrange range = {17, 4};

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

Nsrange range = Nsmakerange (17, 4);

1.3.3 Geometric types?

You'll often see types this deal with geometry and has the prefix "CG," such as Cgpoint and Cgsize. These types is provided by the Core Graphics framework and used for 2D rendering.

CG prefix.? These types are provided by the core Graphics library framework. Used as a 2d? display.

?

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

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, the make string handling much easier.

NSString? There are many intrinsic ways to make it easier for us to work with strings.

?

1.3.4.1Build that String?

NSString ' s Stringwithformat:method creates a new nsstring just like this, 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 + sign, you mark the method as a class method.

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

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

?

1.3.4.2 size matters? Length issue

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

-(nsuinteger) length;

?

Nsuinteger length=[height length];

?

1.3.4.3 comparative politics?? Compare policies

Isequaltostring:compares the receiver (the object, the message is being sent to) with a string of that ' s passed in as an Argument. Isequaltostring:returns a BOOL (YES or NO) indicating if the strings has the same contents. It ' s declared like this:

-(BOOL) isequaltostring: (NSString *) astring;

To compare strings, with the Compare:method, which is declared as follows:-(Nscomparisonresult) Compare: (NSString *) as Tring

[@ "Aardvark" Compare: @ "zygote"] would return nsorderedascending.

?

1.3.4.4Insensitivity Training

Compare:does a case-sensitive comparison. In the other words, @ "Bork" and @ "Bork", when compared, won ' t return nsorderedsame. There ' s another method, Compare:options:, that gives-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 would does this:

if ([Thing1 compare:thing2 Options:nscaseinsensitivesearch | Nsnumericsearch]

? ? = = Nsorderedsame)

{

? NSLog (@ "they match!");

}

?

1.3.4.5 is it inside?? In there?

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

-(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 was somewhere inside another string, use rangeofstring:-(Nsrange) rangeofstring: (nsstring *) astring;

Nsrange range = [fileName rangeofstring: @ "chapter"];

1.3.4.6 mutability

Nsstrings is immutable. Cocoa provides a subclass of NSString called Nsmutablestring. Use the If you want to slice and dice a string of place.

You can create a new nsmutablestring by using the class method Stringwithcapacity:, which are declared like so:?

Can I use the class method? Create Nsmutablestring

+ (ID) stringwithcapacity: (Nsuinteger) capacity;

The capacity is just a suggestion to nsmutablestring, as if you are a teenager what time to be home.

This volume is just a suggestion, just like when you tell a young man when to go home.

Once you has a mutable string, you can do any sorts of wacky tricks with it. A common operation is to append a new string, using the 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: (Nsrange) Arange;

?

nsmutablestring *friends = [nsmutablestring stringwithcapacity:50];

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

Nsrange Jackrange = [Friends rangeofstring: @ "Jack"];

jackrange.length++; Eat the space that follows

[Friends Deletecharactersinrange:jackrange];

?

?

?

?

?

?

Object-c OOP, source organization, Foundation framework detailed 1

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.