您好,歡迎來(lái)電子發(fā)燒友網(wǎng)! ,新用戶(hù)?[免費(fèi)注冊(cè)]

您的位置:電子發(fā)燒友網(wǎng)>源碼下載>通訊/手機(jī)編程>

iOS中關(guān)于泛型的解析

大小:0.3 MB 人氣: 2017-09-25 需要積分:2

文章圍繞這五點(diǎn):

1. 泛型是什么

2. 為什么要用泛型

3. 泛型怎么用

4. 泛型進(jìn)階

5. 泛型的延伸使用

泛型(Generics)是什么?

引用Apple中Generics的描述:

Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner.

Generics are one of the most powerful features of Swift, and much of the Swift standard library is built with generic code. In fact, you’ve been using generics throughout the Language Guide, even if you didn’t realize it. For example, Swift’s Array and Dictionary types are both generic collections. You can create an array that holds Int values, or an array that holds String values, or indeed an array for any other type that can be created in Swift. Similarly, you can create a dictionary to store values of any specified type, and there are no limitations on what that type can be.

大意是講:

泛型可以讓你使用定義的類(lèi)型來(lái)編寫(xiě)靈活的、可重用的函數(shù)和類(lèi)型,可以避免重復(fù),以清晰,抽象的方式表達(dá)其意圖。用人話來(lái)說(shuō)(????),泛型給予我們更抽象的封裝函數(shù)或類(lèi)的能力,不嚴(yán)謹(jǐn)?shù)膩?lái)講,一門(mén)語(yǔ)言越抽象使用越方便。Swift中的Array和Dictionary都是基于泛型編寫(xiě)的集合類(lèi)型,如果不太理解也沒(méi)關(guān)系,下面講幾個(gè)例子理解下。

1. Objective-C中的泛型

在2015年WWDC上蘋(píng)果推出了Swift 2.0版本,為了讓開(kāi)發(fā)者從Objective-C更好得過(guò)渡到Swift上,蘋(píng)果也為Objective-C帶來(lái)了Generics泛型支持

Generics. Allow you to specify type information for collection classes like NSArray, NSSet, and NSDictionary. The type information improves Swift access when you bridge from Objective-C and simplifies the code you have to write.

所以我們經(jīng)常看到的OC中的泛型比如:

1

2

3

4  // 實(shí)例化一個(gè)元素類(lèi)型為`NSString`的數(shù)組

NSArray 《nsstring *》 *array = [NSArray new ];

// 或者字典

NSDictionary 《nsstring *, nsnumber *》 *dict = @{@ “manoboo” : @1}《/nsstring *, nsnumber *》《/nsstring *》

或者:

1

2  - (void)touchesBegan:(NSSet《uitouch *》 *)touches withEvent:(UIEvent *)event {

}《/uitouch *》

我們先看看OC中的泛型大概做了些什么:

打開(kāi)NSArray.h 我們可以看到:

1

2

3

4

5

6

7  @interface NSArray《__covariant ObjectType》 : NSObject 《nscopying, nsmutablecopying, nssecurecoding, nsfastenumeration》

@property (readonly) NSUInteger count;

- (ObjectType)objectAtIndex:(NSUInteger)index;

- (instancetype)init NS_DESIGNATED_INITIALIZER;

- (instancetype)initWithObjects:(const ObjectType _Nonnull [_Nullable])objects count:(NSUInteger)cnt NS_DESIGNATED_INITIALIZER;

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

@end《/nscopying, nsmutablecopying, nssecurecoding, nsfastenumeration》

聲明一個(gè)Generics的格式如下:

1

2  @interface 類(lèi)名 《占位類(lèi)型名稱(chēng)》

@end

占位類(lèi)型后也可以加入類(lèi)型限制,比如:

1

2  @interface MBCollection 《t: nsstring *》

@end《/t: nsstring *》

非常好我支持^.^

(0) 0%

不好我反對(duì)

(1) 100%

      發(fā)表評(píng)論

      用戶(hù)評(píng)論
      評(píng)價(jià):好評(píng)中評(píng)差評(píng)

      發(fā)表評(píng)論,獲取積分! 請(qǐng)遵守相關(guān)規(guī)定!

      ?