structResolution{ var width =0 var height =0 } classVideoMode{ var resolution =Resolution() var interlaced =false var frameRate =0.0 var name: String? }
类和结构体的实例:
1 2
let someResolution =Resolution() let someVideoMode =VideoMode()
属性访问:
1 2 3
someVideoMode.resolution.width =1280 println("The width of someVideoMode is now \(someVideoMode.resolution.width)") // prints "The width of someVideoMode is now 1280"
结构体类型的成员构造器(类实例没有):
1
let vga =Resolution(width: 640, height: 480)
结构体和枚举是值类型
结构体:
1 2 3 4 5 6 7 8 9 10
let hd =Resolution(width: 1920, height: 1080) var cinema = hd
cinema.width =2048
println("cinema is now \(cinema.width) pixels wide") // prints "cinema is now 2048 pixels wide"
println("hd is still \(hd.width) pixels wide") // prints "hd is still 1920 pixels wide"
枚举:
1 2 3 4 5 6 7 8 9 10 11 12
enumCompassPoint{ caseNorth, South, East, West }
var currentDirection =CompassPoint.West let rememberedDirection = currentDirection currentDirection = .East
if rememberedDirection == .West { println("The remembered direction is still .West") } // prints "The remembered direction is still .West"
类是引用类型
1 2 3 4 5 6 7 8 9 10 11
let tenEighty =VideoMode() tenEighty.resolution = hd tenEighty.interlaced =true tenEighty.name ="1080i" tenEighty.frameRate =25.0
let alsoTenEighty = tenEighty alsoTenEighty.frameRate =30.0
println("The frameRate property of tenEighty is now \(tenEighty.frameRate)") // prints "The frameRate property of tenEighty is now 30.0"
标识恒等运算符(===和!==):
1 2 3 4
if tenEighty === alsoTenEighty { println("tenEighty and alsoTenEighty refer to the same VideoMode instance.") } // prints "tenEighty and alsoTenEighty refer to the same VideoMode instance."