From: AceVest Date: Wed, 22 Jun 2016 15:42:37 +0000 (+0800) Subject: Classes & Properties X-Git-Url: http://zhaoyanbai.com/repos/?a=commitdiff_plain;h=d21b77af5cbb21ffcb908efb787f88b300e52d8f;p=acecode.git Classes & Properties --- diff --git a/learn/AcePlay/AcePlay.playground/Pages/Classes.xcplaygroundpage/Contents.swift b/learn/AcePlay/AcePlay.playground/Pages/Classes.xcplaygroundpage/Contents.swift new file mode 100644 index 0000000..7a81ad8 --- /dev/null +++ b/learn/AcePlay/AcePlay.playground/Pages/Classes.xcplaygroundpage/Contents.swift @@ -0,0 +1,61 @@ +//: [Previous](@previous) + +import UIKit + +printLine("Classes & Structures") + +struct ResolutionA { + var width = 0 + var height: Int +} + +// 如果成员未初始化,在实例化的时候要初始化所有成员 +var resolutionA = ResolutionA(width: 2, height: 3) + + +struct Resolution { + var width = 0 + var height = 0 +} +var resolutionB = Resolution() // 成员初始化完了则不用 + +let vga = Resolution(width: 640, height: 480) +let hd = Resolution(width: 1920, height: 1080) +var cinema = hd +cinema.width = 2048 + +// Struct 是值类型 +print("HD: \(hd.width)x\(hd.height)") +print("Cinema: \(cinema.width)x\(cinema.height)") + + +class VideoMode { + var resolution = Resolution() + var interlaced = false + var frameRate = 0.0 + var name: String? +} + +let someVideoMode = VideoMode() +someVideoMode.resolution.width = 1680 // Class是引用类型,所以纵然someVideoMode是常量,但其指向的是变量 +someVideoMode.resolution.height = 1050 // 属性访问是可以级联进行的 + +let tenEighty = VideoMode() +tenEighty.resolution = hd +tenEighty.interlaced = true +tenEighty.frameRate = 25.0 +tenEighty.name = "1080i" + +let anotherTenEighty = tenEighty // 引用赋值 +anotherTenEighty.frameRate = 30.0 +print("Now TenEight Frame Rate: \(tenEighty.frameRate)") + + +printLine("Identity Operators") +if anotherTenEighty === tenEighty { + print("tenEighty & anotherTenEighty refer to the same VideoMode instance") +} + +if someVideoMode !== tenEighty { + print("someVideoMode & tenEighty refer to different VideoMode instance") +} \ No newline at end of file diff --git a/learn/AcePlay/AcePlay.playground/Pages/Properties.xcplaygroundpage/Contents.swift b/learn/AcePlay/AcePlay.playground/Pages/Properties.xcplaygroundpage/Contents.swift new file mode 100644 index 0000000..e636094 --- /dev/null +++ b/learn/AcePlay/AcePlay.playground/Pages/Properties.xcplaygroundpage/Contents.swift @@ -0,0 +1,78 @@ +//: [Previous](@previous) + +import UIKit + +printLine("Properies") + +printLine("Stored Properties") + +//Instances of FixedLengthRange have a variable stored property called firstValue and a constant stored property called length. +struct FixedLengthRange { + var firstValue: Int + let length: Int +} + +var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length: 3) +rangeOfThreeItems.firstValue = 7 + + +printLine("Stored Properties of Constant Structure Instances") + +let rangeOfFourItems = FixedLengthRange(firstValue: 0, length: 4) +//以下这句是不行的,因为rangeOfFourItems是常量FixedLengthRange的struct类型,是值类型,其值不能修改。而类因为是引用类型则可以。 +//rangeOfFourItems.firstValue = 9 + +printLine("Lazy Stored Properties") +// lazy property must always be variable +// lazy 属性在声明前加上 lazy 关键字 +// 如果一个被标记为 lazy 的属性在没有初始化时就同时被多个线程访问,则无法保证该属性只会被初始化一次 + + +printLine("Computed Properties") + +struct Point { + var x = 0.0, y = 0.0 +} + +struct Size { + var width = 0.0 + var height = 0.0 +} + +struct Rect { + var origin = Point() + var size = Size() + + var center: Point { + get { + var p = Point() + p.x = origin.x + size.width/2 + p.y = origin.y + size.width/2 + return p + } + + set(p) { + origin.x = p.x - size.width/2 + origin.y = p.y - size.height/2 + } + + /* + //set 有更简洁的写法,set重命名参数的话默认参数名为newValue + + set { + origin.x = newValue.x - size.width/2 + origin.y = newValue.y - size.height/2 + } + */ + } +} + + + +var square = Rect(origin: Point(x:0.0, y:0.0), size: Size(width: 10.0, height: 10.0)) +let squareCenter = square.center +print("square center is (\(squareCenter.x), \(squareCenter.y))") + +square.center = Point(x: 15.0, y: 15.0) +print("square new origin is (\(square.origin.x), \(square.origin.y))") + diff --git a/learn/AcePlay/AcePlay.playground/contents.xcplayground b/learn/AcePlay/AcePlay.playground/contents.xcplayground index 04644e5..d6d3e1a 100644 --- a/learn/AcePlay/AcePlay.playground/contents.xcplayground +++ b/learn/AcePlay/AcePlay.playground/contents.xcplayground @@ -5,5 +5,7 @@ + + \ No newline at end of file diff --git a/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate b/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate index f12efa0..1e2cf8b 100644 Binary files a/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate and b/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate differ