swift与结构体

前端之家收集整理的这篇文章主要介绍了swift与结构体前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


struct Resolution {

var width = 0

var height = 0

func test(){

println("test")

}

}


//所有结构体都有一个自动生成的成员逐一构造器,用于初始化新结构体实例中成员的属性

let vga = Resolution(width:640,height: 480)

let someResolution = Resolution()

println(vga.width)

println(someResolution.width)

someResolution.test()


//这样写错误

//someResolution.width = 23



class VideoMode {

var resolution = Resolution()

var interlaced = false

var frameRate = 0.0

var name: String?

}


let someVideoMode = VideoMode()

//Swift 允许直接设置结构体属性的子属性

someVideoMode.resolution.width = 12880

println(someVideoMode.resolution.width)


//类是引用类型,引用的是已存在的实例本身而不是其拷贝。

let hd = Resolution(width: 1920,height: 1080)

let tenEighty = VideoMode()

tenEighty.resolution = hd

tenEighty.interlaced = true

tenEighty.name = "1080i"

tenEighty.frameRate = 25.0



let alsoTenEighty = tenEighty

alsoTenEighty.frameRate = 30.0


if tenEighty === alsoTenEighty {

println("tenEighty alsoTenEighty 是等价的")

}

猜你在找的Swift相关文章