Objective,objectivec

來源:互聯網
上載者:User

Objective,objectivec

Objective - c  Foundation 架構詳解2 

 

Collection Agency

Cocoa provides a number of collection classes such as NSArray and NSDictionary whose instances exist just to hold onto other objects.

cocoa 提供了一系列的集合類,例如,NSarray,NSdictionary。它們存在的目的就是為了保持其他對象。

1.1.1NSArray is a Cocoa class that holds an ordered list of objects. You can put any kind of objects in an NSArray: NSString, Car, Shape, Tire, or whatever else you want, even other arrays and dictionaries.

NSArray 是cocoa 類,它提供一個有序的對象列表。裡面可以填任何對象。 

 

NSArray has two limitations. First, it holds only Objective-C objects. You can't have primitive C types, like int, float, enum, struct, or random pointers in an NSArray. Also, you can't store nil (the zero or NULL value for objects) in an NSArray.

NSarray 有兩個限制。第一,只能存objective -c 對象,不能存C 語言對象,如float等。第二,不能存Nil 。

 

You can create a new NSArray using the class method arrayWithObjects:. You give it a comma- separated list of objects, with nil at the end to signal the end of the list (which, by the way, is one of the reasons you can't store nil in an array):

arraywithobject 最後要用nil ,作為結束符。

NSArray *array = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];

1.1.2Once you have an array, you can get a count of the number of objects it contains:

- (NSUInteger)count;

And you can fetch an object at a particular index:

- (id)objectAtIndex:(NSUInteger)index;

 

for (NSInteger i = 0; i < [array count]; i++)

{

     NSLog (@"index %d has %@.",i, [array objectAtIndex:i]);

}

You can also write the preceding code using the array literal syntax:

for (NSInteger i = 0; i < [array count]; i++)

{

  NSLog (@"index %d has %@.",i, array[i]);

}

1.1.3 Mutable arrays 

It uses a class method, arrayWithCapacity, to make a new mutable array:

+ (id) arrayWithCapacity: (NSUInteger) numItems;

NSMutableArray *array = [NSMutableArray arrayWithCapacity: 17];

Add objects to the end of the array by using addObject:.
- (void) addObject: (id) anObject;

You can add four tires to an array with a loop like this:

 

for (NSInteger i = 0; i < 4; i++)

{

 Tire *tire = [Tire new];

 [array addObject: tire];

}

You can remove an object at a particular index. For example, if you don't like the second tire, you can use removeObjectAtIndex: to get rid of it. Here's how the method is defined:

- (void) removeObjectAtIndex: (NSUInteger) index;

You use it like this:

[array removeObjectAtIndex:1];

 

1.2.1Enumeration Nation 枚舉

NSEnumerator, which is Cocoa's way of describing this kind of iteration over a collection. 

枚舉,cocoa的方式,描述一個容器的迭代。

use NSEnumerator, you ask the array for the enumerator using objectEnumerator:

- (NSEnumerator *)objectEnumerator;

You use the method like this:例如:

NSEnumerator *enumerator = [array objectEnumerator];

After you get an enumerator, you crank up a while loop that asks the enumerator for its

nextObject every time through the loop:

- (id) nextObject;

When nextObject returns nil, the loop is done.

如果nextObject 返回nil,那麼迴圈就結束了。

NSEnumerator *enumerator = [array objectEnumerator];

while (id thingie = [enumerator nextObject])

{

 NSLog (@"I found %@", thingie);

}

 

There's one gotcha if you're enumerating over a mutable array: you can't change the container

如果你在操作一個可變數組的話,遍曆的時候不要改動容器。

 

1.2.2 快速枚舉 Fast Enumeration

for (NSString *string in array)

{

 NSLog (@"I found %@", string);

}

 

To support the blocks feature, Apple has added a method to enumerate objects in NSArray using blocks, and it looks like this.

為了支援block,蘋果增加了一個方法來支援數列用block。

- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block

[array enumerateObjectsUsingBlock:^(NSString *string, NSUInteger index, BOOL *stop) {

    NSLog (@"I found %@", string);

}];

 

Now, the question is, "Why would we use this instead of fast enumeration?" With blocks, one of the options is that the loop can execute in parallel. With fast enumeration, execution proceeds through the items linearly.

這樣做的目的就是為了並發執行。

 

1.3NSDictionary  字典 

An NSDictionary stores a value (which can be any kind of Objective-C object) under a given key (usually an NSString).

字典 儲存任意的一個值,在給定的一個關鍵字下(通常是NSString)

However, the NSMutableDictionary class lets you add and remove stuff at will.

NSMutableDictionary允許你自由的增加刪除。

The easiest way to get started with a dictionary is to use the dictionary literal syntax, which is similar to the class method dictionaryWithObjectsAndKeys:.

構造一個詞典用 逐字 最容易:

The literal syntax is defined as @{key:value,...}; 

字面是key:value。 

NSDictionary *tires = [NSDictionary dictionaryWithObjectsAndKeys: t1,

    @"front-left", t2, @"front-right", t3, @"back-left", t4, @"back-right", nil];

or

NSDictionary *tires = @{@"front-left" : ti, @"front-right" : t2, @"back-left" : t3,

    @"back-right" : t4};

 

To access a value in the dictionary, use the objectForKey: method, giving it the key you previously stored the value under:

擷取一個值用詞典:

- (id) objectForKey: (id) aKey;

or

tires[key];

 

To make a new NSMutableDictionary, send the dictionary message to the NSMutableDictionary class. 

 

+ (id) dictionaryWithCapacity: (NSUInteger) numItems;

You can add things to the dictionary using setObject:forKey:.

可以增加實物 用setObject:forKey

- (void)setObject:(id)anObject forKey:(id)aKey

NSMutableDictionary *tires = [NSMutableDictionary dictionary];

[tires setObject:tl forKey:@"front-left"];

[tires setObject:t2 forKey:@"front-right"];

[tires setObject:t3 forKey:@"back-left"];

[tires setObject:t4 forKey:@"back-right"];

If you want to take a key out of a mutable dictionary, use the removeObjectForKey: 

刪除資料:

method: - (void) removeObjectForKey: (id) aKey;

[tires removeObjectForKey:@"back-left"];

 

1.4 NSNumber 數字

Cocoa provides a class called NSNumber that wraps (that is, implements as objects) the primitive numeric types.

NSNumber 封裝了原生的資料類型。

You can create a new NSNumber using these class methods:

+ (NSNumber *) numberWithChar: (char) value;

+ (NSNumber *) numberWithInt: (int) value;

+ (NSNumber *) numberWithFloat: (float) value;

+ (NSNumber *) numberWithBool: (BOOL) value;

 

You can also use the literal syntax to create these objects:

也可以逐字建立對象。

NSNumber *number;

number = @'X'; // char

number = @12345; // integer

number = @12345ul; // unsigned long

number = @12345ll; // long long

number = @123.45f; // float

number = @123.45; // double

number = @YES; // BOOL

 

After you create an NSNumber, you can put it into a dictionary or an array:

這樣以後你就可以把它放進array 或dictionary了。

NSNumber *number = @42;

[array addObject number];

[dictionary setObject: number forKey: @"Bork"];

 

Once you have a primitive type wrapped in an NSNumber, you can get it back out using one of these instance methods:

一旦你獲得了一個原生資料封裝的NSNumber,可以通過下方還原:

- (char) charValue;

- (int) intValue;

- (float) floatValue;

- (BOOL) boolValue;

- (NSString *)stringValue;

 

1.5 NSValue 值

NSNumber is actually a subclass of NSValue, which wraps arbitrary values. You can use NSValue to put structures into NSArrays and NSDictionary objects.

NSValue 是NSNumber 的基類。NSValue 可以儲存任意的值 。

Create a new NSValue using this class method:

+ (NSValue *) valueWithBytes: (const void *) value objCType: (const char *) type;

 

You pass the address of the value you want to wrap (such as an NSSize or your own struct). Usually, you take the address (using the & operator in C) of the variable you want to save.

你可能傳遞一個值得地址。

So, to put an NSRect into an NSArray, you do something like this:

如果你想把一個NSRect 放進NSArray中,

NSRect rect = NSMakeRect (1, 2, 30, 40);

NSValue *value = [NSValue valueWithBytes:&rect objCType:@encode(NSRect)];

[array addObject:value];

You can extract the value using getValue:
- (void)getValue:(void *)buffer;

You can extract the value using getValue:
- (void)getValue:(void *)buffer;
When you call getValue:, you pass the address of a variable that you want to hold the value: 

當你調用getValue時,你可以傳遞地址到value。

value = [array objectAtIndex: 0];

 [value getValue: &rect];

 

Convenience methods are provided for putting common Cocoa structs into NSValues, and we have conveniently listed them here:

為了方便 ,列出以下常用類:

+ (NSValue *)valueWithPoint:(NSPoint)aPoint;

+ (NSValue *)valueWithSize:(NSSize)size;

+ (NSValue *)valueWithRect:(NSRect)rect;

- (NSPoint)pointValue;

- (NSSize)sizeValue;

- (NSRect)rectValue;

To store and retrieve an NSRect in an NSArray, you do this:

儲存或擷取NSrect在NSarray 中:

value = [NSValue valueWithRect:rect];

[array addObject: value];

...

NSRect anotherRect = [value rectValue];

 

1.6 NSNull 空 

We've told you that you can't put nil into a collection, because nil has special meaning to NSArray and NSDictionary. But sometimes, you really need to store a value that means "there's nothing here at all."

我們不能儲存nil到容器中,因為nil有特殊意義。但是有時我們確實需要這麼做。 

NSNull is probably the simplest of all Cocoa classes. It has but a single method: 

NSNull 很簡單。有一個單獨的類方法。 

+ (NSNull *) null;

[contact setObject: [NSNull null]

forKey: @"home fax machine"];

id homefax = [contact objectForKey: @"home fax machine"];

if (homefax == [NSNull null])

{

// ... no fax machine. rats.

}

 

 

 

 

 

 

 

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

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

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.