Swift教程-类与结构体
来源:互联网 发布:mac下视频音频格式转换 编辑:程序博客网 时间:2024/09/21 09:01
swift 中类于结构体
swift中结构体与类既相似,又有不同。
相似点
- 可以保存变量
- 保存函数
不同点
- 结构体不支持继承,类支持
- 结构体是引用类型,类引用类型
.
- 定义
// 结构体定义struct SomeStructure { //成员变量 var width = 0 var height = 0}// 类定义class SomeClass { //成员变量 var resolution = Resolution() var interlaced = false var frameRate = 0.0 var name: String?}
2.实例化变量
let someResolution = Resolution()let someVideoMode = VideoMode()//带参数的初始函数let vga = Resolution(width: 640, height: 480)
3.结构体是值变量
let hd = Resolution(width: 1920, height: 1080)var cinema = hdcinema.width = 2048print("cinema is now \(cinema.width) pixels wide")// Prints "cinema is now 2048 pixels wide"print("hd is still \(hd.width) pixels wide")// Prints "hd is still 1920 pixels wide"
4.类是引用变量
类是类似于C指针一样的引用变量
let tenEighty = VideoMode()tenEighty.frameRate = 25.0let alsoTenEighty = tenEightyalsoTenEighty.frameRate = 30.0// Prints "The frameRate property of tenEighty is now 30.0"print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
5.Swift基本数据类型内的结构体与Foundation内的类
在swift中的数据类型均为结构体,如String,Array,Dictionary。即在赋值传递中均为值传递。
在Foundation库中,NSString,NSArry,NSDictionary都是用类实现,在传递的过程中为引用传递
0 0
- Swift教程-类与结构体
- Swift类与结构体
- Swift结构体与类
- Swift简明教程(五)枚举与结构体
- 苹果Swift语言中文教程(九) 类与结构
- Swift中文教程---类与结构(Struct,Class)
- Swift中文教程(九) 类与结构
- Swift中文教程(九) 类与结构
- Swift中文教程(九) 类与结构
- Swift教程之结构体
- swift与结构体
- Swift-类与结构体自测题
- Swift之类与结构体
- Swift 类 结构体
- [iOS/Swift] 类与结构体相关归纳
- Swift学习笔记(11)--类与结构体
- Swift 基本知识点之七类与结构体
- Swift 基本知识点之八 —类与结构体
- expect解决ssh无法使用shell直接登录的问题
- (Caffe)LSTM层分析
- SpringMVC简单整合Angular2
- Codeforces 525E Anya and Cubes【折半枚举法+剪枝Dfs】好题~
- 经典排序算法:快速排序(Quick Sort)
- Swift教程-类与结构体
- C语言的几种循环
- Java - JFrame 设置背景图片
- 跑马灯实验-寄存器版
- Android Dev 进程与线程 学习
- 180中国跳棋(12)186(13)191(14)196(15)201(16)205(17)209.5(18)
- Android 横竖屏切换总结
- 一个极其简陋的C/C++日志工具
- Linux Socket过程详细解释(包括三次握手建立连接,四次握手断开连接)