Classes and Structures
来源:互联网 发布:rcpp软件 编辑:程序博客网 时间:2024/05/17 00:07
一.定义语法
class SomeClass {
// class definition goes here
}
struct SomeStructure {
// structure definition goes here
}
注意:一般类名和结构名首字母都大学,方法和属性都小写。
struct Resolution {
var width = 0
var height = 0
}
class VideoMode {
var resolution = Resolution()
var interlaced = false
var frameRate = 0.0
var name: String?
}
类和结构体实例:
let someResolution = Resolution()
let someVideoMode =VideoMode()
访问属性:
我们可以用点语法来访问一个属性值或者改变一个属性值。
print("The width of someVideoMode is \(someVideoMode.resolution.width)")
someVideoMode.resolution.width = 1280
print("The width of someVideoMode is now \(someVideoMode.resolution.width)")
每个 structures 都有一个默认的成员变量初始化函数。你能通过属性名来传值。
letvga =Resolution(width:640,height: 480)
二.结构体和枚举是值类型。
一个值类型是当被赋值给另一个变量或者常量时发生深拷贝。swift 中的全部基础类型是值类如:integers, floating-point numbers, Booleans, strings, arrays and dictionaries
三.类是引用类型
当一个引用类型的变量赋值给一个 let或者var,或者传入一个方法时,不会发生深拷贝。他们仍然引用一个相同的实例对象。
要注意的是当 let 或者 var 引用一个对象的时候,let 和 var 并不是指针。
四.选择结构体还是类
在下面的情况下,考虑用结构体:
1)数据结构最主要的目的是封装一些简单的数据
2)如果你是希望当你传送一个数据结构到一个方法或者赋值给另一个相同类型,发生深拷贝,而不仅仅是引用的传递。
3)如果一个数据结构里存放了值类型的属性,我们应该用结构体。
4)数据结构不需要继承其他的属性和方法。
五.Strings,Arrays,Dictionary 的分配和复制表现。
在 swift 许多基础类型比如 String,Array,Dictionary 是用结构体实现的。
这和 Foundation 框架里的 NSString,NSArray 和 NSDictionary 不同,他们都是用类实现的。
- Classes and Structures
- Classes and Structures
- Part 2: (Enumerations, Classes and Structures, Properties)
- Swift类和结构体(Classes and Structures)
- Swift 类和结构体(Classes and Structures)
- 浅学Swift 3.0.1中的Classes and structures
- Swift学习笔记9——类和结构体(Classes and Structures)
- 对照Java学习Swift--类和结构体(Classes and Structures)
- swift3.1 (8)Enumerations,Classes,Structures,Properties and methods(部分精华)
- Basic Structures and Algorithms
- Data Structures and Algorithms
- Data Structures and Algorithms
- Objects and Data Structures
- Structures and Unions
- JNI Types and Data Structures
- Memory relative structures and variables
- JNI Types and Data Structures
- URL functions and classes
- 需求:使用SwipeRefreshLayout时,在界面顶部才执行刷新
- 人工智能
- Openstack_SQLAlchemy_一对多关系表的多表插入实现
- Linux文件系统操作命令
- Java 项目优化实战
- Classes and Structures
- UVa - 514 - Rails(栈-stack)
- C语言 1~100 所有整数中出现多少次数字 9
- 第4周项目3-并联电阻
- AndroidStudio导入一个AndroidStudio工程作为一个Library Module
- 实验二任务1打招呼
- TCP发送源码学习(3)--tcp_transmit_skb
- 广义表的基本实现
- 设计模式