当前位置:网站首页>Swiftui swift internal skill: five skills of using opaque type in swift
Swiftui swift internal skill: five skills of using opaque type in swift
2022-07-07 11:45:00 【Knowledge fatness】
If you are Swift Novice , You may not be familiar with the concept of opaque types . In short , An opaque type is a type that cannot be subclassed or initialized from outside its definition module . let me put it another way , This is a way to hide implementation details from other parts of the code .
although opaque Type can be used for many purposes , But they are especially suitable for creating easy-to-use and difficult to misuse API. In this post , We will share in Swift Use in opaque Five skills of type .
1. Use opaque types to hide implementation details
One of the biggest benefits of using opaque types is that they allow you to hide implementation details from other parts of the code . When you want to create something that is easy to use and difficult to misuse API when , This is especially useful .
for example , Suppose you are creating a library that provides custom view controllers . You may want to hide the implementation details of the view controller from the users of the Library . By using opaque types , You can do this .
2. Use opaque types to enforce type safety
Another benefit of using opaque types is that they help you enforce type safety in your code . This is because opaque types cannot be subclassed or initialized from outside their definition module .
for example , Suppose you have a that returns an opaque type API. If you try to subclass or initialize the type from outside the definition module , You will receive compiler errors . This means that you can be sure that only the types you expect are used in your code .
3. Use opaque types to improve performance
In addition to helping hide implementation details and enforce type safety , Opaque types can also improve performance . This is because the compiler can optimize code that uses opaque types more effectively .
for example , Suppose you have a method that takes an opaque type as a parameter . The compiler can inline this method , For better performance .
4. Use opaque types to limit the visibility of types
Another advantage of using opaque types is that they help you limit the visibility of types . This is because opaque types can only be initialized from their definition modules .
When you want to create something for a specific group of people API when , It would be very helpful . for example , Suppose you
边栏推荐
- 什么是高内聚、低耦合?
- SwiftUI Swift 内功之 Swift 中使用不透明类型的 5 个技巧
- Android 面试知识点
- Use metersphere to keep your testing work efficient
- Half of the people don't know the difference between for and foreach???
- Activity lifecycle
- 科普达人丨一文弄懂什么是云计算?
- Software design - "high cohesion and low coupling"
- Solve the problem that vscode can only open two tabs
- The road to success in R & D efficiency of 1000 person Internet companies
猜你喜欢

Activity lifecycle

The database synchronization tool dbsync adds support for mongodb and es

About how to install mysql8.0 on the cloud server (Tencent cloud here) and enable local remote connection

How to write test cases for test coupons?

分布式数据库主从配置(MySQL)

Le Cluster kubernets en cours d'exécution veut ajuster l'adresse du segment réseau du pod

【滤波跟踪】基于matlab捷联惯导仿真【含Matlab源码 1935期】

Half of the people don't know the difference between for and foreach???

STM32F1与STM32CubeIDE编程实例-315M超再生无线遥控模块驱动

CMU15445 (Fall 2019) 之 Project#2 - Hash Table 详解
随机推荐
Creative information was surveyed by 2 institutions: greatdb database has been deployed in 9 places
MIF file format record
对比学习之 Unsupervised Learning of Visual Features by Contrasting Cluster Assignments
博客搬家到知乎
audit 移植
科普达人丨一文弄懂什么是云计算?
How to write test cases for test coupons?
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
清华姚班程序员,网上征婚被骂?
Have you ever met flick Oracle CDC, read a table without update operation, and read it repeatedly every ten seconds
Software design - "high cohesion and low coupling"
核舟记(一):当“男妈妈”走进现实,生物科技革命能解放女性吗?
Blog moved to Zhihu
使用MeterSphere让你的测试工作持续高效
Internet Protocol
常用sql语句整理:mysql
大佬们有没有人遇到过 flink oracle cdc,读取一个没有更新操作的表,隔十几秒就重复读取
Distributed database master-slave configuration (MySQL)
How much do you know about excel formula?
千人規模互聯網公司研發效能成功之路