08-Swift结构体和类

2020-12-14  本文已影响0人  一抹相思泪成雨

结构体

struct Point {
    var x: Int = 1
    var y: Int = 2
//    var x: Int?
//    var y: Int?
}
var p1 = Point(x: 10, y: 10)
var p2 = Point(x: 10)
var p3 = Point(y: 10)
var p4 = Point()

structPoint{
varx: Int?  // 可选项都有个默认值nil
vary: Int?
}
varp1= Point(x: 10, y: 10)
varp2= Point(y: 10)
varp3= Point(x: 10)
varp4= Point()

1.自定义初始化器

struct TestPoint {
    var x: Int = 0
    var y: Int = 0
    var z: Bool = true
    init(x: Int, y: Int) {
        self.x = x
        self.y = y
    }
}

var testP1 = TestPoint(x: 10, y: 10)
//var p2 = TestPoint(y: 10)
//var p3 = TestPoint(x: 10)
//var p4 = TestPoint()

2.结构体内部结构

print(MemoryLayout<TestPoint>.size)         // 具体占用的内存大小
print(MemoryLayout<TestPoint>.stride)       // 系统分配的默认大小
print(MemoryLayout<TestPoint>.alignment)    // 对齐

class PointClass {
    var x: Int = 0
    var y: Int = 0
}

//let pc1 = PointClass()
//let pc1 = PointClass(x: 10, y: 20)
//let pc1 = PointClass(x: 10)
//let pc1 = PointClass(y: 20)

1.类的初始化器

classPoint{
  var x: Int= 10
  var y: Int= 20
}
let p1= Point()

//  等价于
classPoint{
  var x: Int
  var y: Int
   init() {
     x= 10
     y= 20
  }
}
let p1= Point()

2.结构体与类的本质区别

3.值类型

var s1 = "Jack"
var s2 = s1
print(String(format: "%p", s1))
print(String(format: "%p", s2))
s2.append("_Rose")
print(s1)
print(s2)
print(String(format: "%p", s1))
print(String(format: "%p", s2))

在Swift标准库中,为了提升性能,String,Array,Dictionary,Set采取了Copy On Write的技术
1.比如仅当有"写"操作时,才会真正进行拷贝操作
2.对于标准库值类型的赋值操作,Swift能够确保最佳性能,所以没必要为了保证最佳性能来避免赋值
3.建议:不需要修改的,尽量定义为let

4.引用类型

class Size {
    var width: Int
    var height: Int
    init(width: Int, height: Int) {
        self.width = width
        self.height = height
    }
}

func test() {
    let s1 = Size(width: 10, height: 20)
    let s2 = s1
    s2.width = 11
    s2.height = 22
    /// 请问s1.width和s1的height是多少?
}

5.对象的堆空间申请过程

在Swift中,创建类的实例对象,要向堆空间申请内存,大概流程如下
Class.__allocating_init()
libswiftCore.dylib:_swift_allocObject_
libswiftCore.dylib:swift_slowAlloc
libsystem_malloc.dylib:malloc
在Mac、iOS中的malloc函数分配的内存大小总是16的倍数
通过class_getInstanceSize可以得知:类的对象至少需要占用多少内存

classPoint {
    var x= 11
    var test= true
    var y= 22
}
varp= Point()
class_getInstanceSize(type(of: p)) //40
class_getInstanceSize(Point.self) //40

6.值类型、引用类型的let

// 值类型、引用类型的let
struct TestPoint1 {
    var x: Int
    var y: Int
}

class TestSize1 {
    var width: Int
    var height: Int
    init(width: Int, height: Int) {
        self.width = width
        self.height = height
    }
}
//let tp1 = TestPoint1(x: 10, y: 20)
//tp1 = Point(x: 11, y: 22)
//tp1.x = 33
//tp2.y = 44

//let ts1 = TestSize1(width: 10, height: 20)
//ts1 = TestSize1(width: 11, height: 22)
//ts1.width = 33
//ts1.height = 44

7.嵌套类型

struct Poker {
    enum Suit: Int {
        case spades, hearts, diamonds, clubs
    }
  enum  Rank: Int{
      case two= 2, three, four, five, six, seven, eight, nine, ten
      case jack, queen, king, ace
  }
}
print(Poker.Suit.hearts.rawValue)
var suit= Poker.Suit.spades
suit= .diamonds
var rank= Poker.Rank.five
rank= .king

8.枚举、结构体、类都可以定义方法

class Size{
  var width= 10
  var height= 10
  func show() {
  print("width=\(width), height=\(height)")
  }
}
let s= Size()
s.show() // width=10, height=10

struct Point {
  var x= 10
  var y= 10
  func show() {
    print("x=\(x), y=\(y)")
  }
}
let p = Point()
p.show() // x=10, y=10
上一篇下一篇

猜你喜欢

热点阅读