object -c  OOP ,  源碼組織  ,Foundation 架構 詳解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 就是由對象構成的軟體。 對象就像一些小的機器存在在你的電腦中,通過相互對話來進行工作。 


1.2 Source File organization 

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

如果你用.mm檔案,那麼你在告訴編譯器你在用oc++ 在寫代碼。



 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.

基礎架構 有一系列低層次,面向資料的類和類型。


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


If you come across 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 _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, you can use the C aggregate structure assignment mechanism (doesn't that 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 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 . 這些類型由 核心圖形庫架構提供。 用作2d 展示。


CGPoint represents an (x, y) point in the Cartesian plane:笛卡爾座標系座標

struct CGPoint


float x;

float y; };

CGSize holds a width and a 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 有許多內在方法讓我們處理字串更容易。 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.

當你聲明方法前有+號時, 你標註了該方法為類方法。

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

類方法通常用來建立新對象,被稱作Factory 方法。 Size matters 長度問題

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



NSUInteger length=[height length]; Comparative Politics  比較策略

isEqualToString: compares the receiver (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) isEqualToString: (NSString *) aString;

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

[@"aardvark" compare: @"zygote"] would return NSOrderedAscending. 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 would do this:

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

    == NSOrderedSame)


  NSLog (@"They match!");

} Is it 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: - (NSRange) rangeOfString: (NSString *) aString;

NSRange range = [fileName rangeOfString: @"chapter"]; 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:  

可以用類方法 建立NSMutableString

+ (id) stringWithCapacity: (NSUInteger) capacity;

The capacity is just a suggestion to NSMutableString, like when you tell a teenager what time to be 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: (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];








Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >



如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。