吴忠躺衫网络科技有限公司

您好,歡迎來電子發燒友網! ,新用戶?[免費注冊]

您的位置:電子發燒友網>源碼下載>通訊/手機編程>

iOS中關于泛型的解析

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

文章圍繞這五點:

1. 泛型是什么

2. 為什么要用泛型

3. 泛型怎么用

4. 泛型進階

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.

大意是講:

泛型可以讓你使用定義的類型來編寫靈活的、可重用的函數和類型,可以避免重復,以清晰,抽象的方式表達其意圖。用人話來說(????),泛型給予我們更抽象的封裝函數或類的能力,不嚴謹的來講,一門語言越抽象使用越方便。Swift中的Array和Dictionary都是基于泛型編寫的集合類型,如果不太理解也沒關系,下面講幾個例子理解下。

1. Objective-C中的泛型

在2015年WWDC上蘋果推出了Swift 2.0版本,為了讓開發者從Objective-C更好得過渡到Swift上,蘋果也為Objective-C帶來了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.

所以我們經常看到的OC中的泛型比如:

1

2

3

4  // 實例化一個元素類型為`NSString`的數組

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中的泛型大概做了些什么:

打開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》

聲明一個Generics的格式如下:

1

2  @interface 類名 《占位類型名稱》

@end

占位類型后也可以加入類型限制,比如:

1

2  @interface MBCollection 《t: nsstring *》

@end《/t: nsstring *》

非常好我支持^.^

(0) 0%

不好我反對

(1) 100%

      發表評論

      用戶評論
      評價:好評中評差評

      發表評論,獲取積分! 請遵守相關規定!

      ?
      网络百家乐现金游戏哪里的信誉好啊| 太阳城百家乐官网坡解| 新花园百家乐官网的玩法技巧和规则 | 怎样赢百家乐官网的玩法技巧和规则 | 百家乐官网是骗人的么| 百家乐一年诈骗多少钱| 保单百家乐官网技巧| 全讯网财运| 太阳百家乐官网路单生| 庄浪县| 英皇百家乐官网的玩法技巧和规则 | 红宝石百家乐官网娱乐城| 博E百百家乐娱乐城| 百家乐官网输一压二| 金木棉百家乐网络破解| 快乐之都| 网上百家乐骗人| 百家乐官网赌博牌路分析| 太阳城娱乐城备用网址| 打百家乐纯打庄的方法| 百家乐推荐怎么看| 百家乐官网牌九| 大发888游戏是真的么| 广东百家乐官网网| 真人百家乐官网软件博彩吧| 奉贤区| 梦幻城百家乐的玩法技巧和规则| 可信百家乐官网的玩法技巧和规则 | 全讯网a3322| 百家乐娱乐平台网| 百家乐官网赌场网| 百家乐官网注册平台排名| 德州扑克比大小| 百家乐游戏规则介绍| 百家乐代理每周返佣| 属鼠做生意办公桌摆貔貅好不好| 百家乐官网技巧经| 百家乐官网建材| 百家乐官网电子路单下载| 在线玩轮盘| 一筒百家乐的玩法技巧和规则|