当前位置:网站首页>Swift 基础 Swift才有的特性
Swift 基础 Swift才有的特性
2022-06-24 06:55:00 【冯汉栩】
一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希望未来技术之巅上有你们也有我。
前言
这篇文章时介绍swift相对oc,swift特有的用法,结合开发中的需求来讲一下
正题
internal
internal修饰的属性,对象地址永远只有一个,就像单例一样
class Service {
internal static let name = "fenghanxu"
}
控制器A使用属性
控制器B使用属性
打印结果
A控制器,属性地址为: 0xb21b7768d2b563c4
B控制器,属性地址为: 0xb21b7768d2b563c4
A控制器,属性地址为: 0xb21b7768d2b563c4
throws
我们在使用系统的方法的时候经常看到系统有写方法里使用了throws属性来去修饰这个方法。例如:看下面的图片。
如果方法里面有用到throws修饰的话,使用方法的时候需要抛出异常。
使用这个方法一般比较常见的两种方式:
使用方式1:
使用方式2:
guard
在swift开发中guard是非常常见的,一般使用情况都是,例如:A属性不确认是否有值,但是下面又需要用到A的值,如果A没有只就没法走下去了。所以需要用到guard对A进行拆包看是否有值,例如看下面的代码。
因为num_0不确认是否有值=,所以使用前的时候需要拆包
第二个是用guard使用比较常见的地方就是在闭包里面。
说明一下为什么要对self进行拆包,因为闭包回来之后当前的self不知道是否还存在,如果被释放了就无法对self里面的属性进行操作,所以在使用self的属性之前先对self进行拆包,如果有self才往下走。没有就退出。
如果不进行拆包使用self都会带 问号(?) 例如:看下面的代码
(?)问号的使用
什么情况属性后面带问号。这个就比较简单了
例如上面的图片,如果tableview创建出来时就像下面的图片形式一样创建的,使用的时候就会tableview后面带问号。因为使用这个对象的时候,并不知道它已经实力化对象没有,如果没有就不设置相关属性,如果实力化才设置。
那么一般什么需求下才会使用上面图片的方式来去创建属性呢。
在一个控制器里面,需要弹出一个框,这个框是需要用到的时候才会创建的,如果创建属性的方式就想上面图片的形式来去创建,用完这个框的时候就会移除掉它。
(??)双问号的使用
从上面的图片可以看出,我们要把height赋值给对象statusBarHeight,但是height是从statusBarManager对象拿出来的,但是statusBarManager不知道是否存在,如果不存在,height就不存在,但是statusBarHeight确认需要得到一个值。所以使用值的时候后面需要带一个 双问号,如果height拿不到值就会拿双问号后面的默认值。
边栏推荐
- Basics of reptile B1 - scrapy (learning notes of station B)
- Unity culling related technologies
- decltype用法介绍
- Latest news of awtk: new usage of grid control
- 某问答社区App x-zse-96签名分析
- Case examples of corpus data processing (cases related to sentence retrieval)
- [测试开发]初识软件测试
- Interview tutorial - multi thread knowledge sorting
- Pair class notes
- Chapter 2 line graph of canvas
猜你喜欢
有关iframe锚点,锚点出现上下偏移,锚点出现页面显示问题.iframe的srcdoc问题
[008] filter the table data row by row, jump out of the for cycle and skip this cycle VBA
调用Feign接口时指定ip
Specify IP when calling feign interface
Echart 心得 (一): 有关Y轴yAxis属性
Application of JDBC in performance test
Graphmae - - lecture rapide des documents
More than observation | Alibaba cloud observable suite officially released
1279_VMWare Player安装VMWare Tools时VSock安装失败解决
1-4metasploitable2介绍
随机推荐
From jsonpath and XPath to spl
GraphMAE----論文快速閱讀
Application of JDBC in performance test
宝塔面板安装php7.2安装phalcon3.3.2
GraphMAE----论文快速阅读
[nilm] non intrusive load decomposition module nilmtk installation tutorial
Specify IP when calling feign interface
Vulnhub靶机:BOREDHACKERBLOG_ CLOUD AV
使用 kubeconfig 文件组织集群访问
Serialization of unity
L1-019 who goes first (15 points)
Leetcode exercise - jumping game, combination summation
Svn actual measurement common operation record operation
Duilib display memory picture
Echart 心得 (一): 有关Y轴yAxis属性
Mousse shares listed on Shenzhen Stock Exchange: gross profit margin continued to decline, and marketing failed in the first quarter of 2022
Jenkins is too old try it? Cloud native ci/cd Tekton
JS implementation to check whether an array object contains values from another array object
调用Feign接口时指定ip
Graphmae - - lecture rapide des documents