Swift有更新,语法有些不太一样,在学习的时候做了一些更改

This commit is contained in:
Kevin Jiang
2014-09-27 23:24:31 +08:00
parent 94b6c46c54
commit 425fae6ddf
4 changed files with 18 additions and 17 deletions

View File

@ -16,7 +16,7 @@ _类型转换_可以判断实例的类型也可以将实例看做是其父类
类型转换在 Swift 中使用`is``as`操作符实现。这两个操作符提供了一种简单达意的方式去检查值的类型或者转换它的类型。 类型转换在 Swift 中使用`is``as`操作符实现。这两个操作符提供了一种简单达意的方式去检查值的类型或者转换它的类型。
你也可以用来检查一个类是否实现了某个协议,就像在 [Checking for Protocol Conformance](21_Protocols.html#checking_for_protocol_conformance)部分讲述的一样。 你也可以用来检查一个类是否实现了某个协议,就像在 [Checking for Protocol Conformance](Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-XID_363)部分讲述的一样。
<a name="defining_a_class_hierarchy_for_type_casting"></a> <a name="defining_a_class_hierarchy_for_type_casting"></a>
## 定义一个类层次作为例子 ## 定义一个类层次作为例子

View File

@ -156,7 +156,7 @@ let centerRect = Rect(center: Point(x: 4.0, y: 4.0),
```swift ```swift
extension Int { extension Int {
func repetitions(task: () -> ()) { func repetitions(task: () -> ()) {
for i in 0..self { for i in 0..<self {
task() task()
} }
} }
@ -217,12 +217,13 @@ someInt.square()
```swift ```swift
extension Int { extension Int {
subscript(digitIndex: Int) -> Int { subscript(var digitIndex: Int) -> Int {
var decimalBase = 1 var decimalBase = 1
for _ in 1...digitIndex { while digitIndex > 0 {
decimalBase *= 10 decimalBase *= 10
--digitIndex
} }
return (self / decimalBase) % 10 return (self / decimalBase) % 10
} }
} }
746381295[0] 746381295[0]

View File

@ -112,7 +112,7 @@ class Starship: FullyNamed {
self.prefix = prefix self.prefix = prefix
} }
var fullName: String { var fullName: String {
return (prefix ? prefix! + " " : " ") + name return (prefix != nil ? prefix! + " " : " ") + name
} }
} }
var ncc1701 = Starship(name: "Enterprise", prefix: "USS") var ncc1701 = Starship(name: "Enterprise", prefix: "USS")
@ -349,9 +349,9 @@ class SnakesAndLadders: DiceGame {
let finalSquare = 25 let finalSquare = 25
let dice = Dice(sides: 6, generator: LinearCongruentialGenerator()) let dice = Dice(sides: 6, generator: LinearCongruentialGenerator())
var square = 0 var square = 0
var board: Int[] var board: [Int]
init() { init() {
board = Int[](count: finalSquare + 1, repeatedValue: 0) board = [Int](count: finalSquare + 1, repeatedValue: 0)
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02 board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08 board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
} }
@ -508,7 +508,7 @@ println(somethingTextRepresentable.asText())
协议类型可以被集合使用,表示集合中的元素均为协议类型: 协议类型可以被集合使用,表示集合中的元素均为协议类型:
```swift ```swift
let things: TextRepresentable[] = [game,d12,simonTheHamster] let things: [TextRepresentable] = [game,d12,simonTheHamster]
``` ```
如下所示,`things`数组可以被直接遍历,并调用其中元素的`asText()`函数: 如下所示,`things`数组可以被直接遍历,并调用其中元素的`asText()`函数:
@ -675,7 +675,7 @@ class Animal {
`CircleCountryAnimal`并没有一个相同的基类,因而采用`AnyObject`类型的数组来装载在他们的实例,如下所示: `CircleCountryAnimal`并没有一个相同的基类,因而采用`AnyObject`类型的数组来装载在他们的实例,如下所示:
```swift ```swift
let objects: AnyObject[] = [ let objects: [AnyObject] = [
Circle(radius: 2.0), Circle(radius: 2.0),
Country(area: 243_610), Country(area: 243_610),
Animal(legs: 4) Animal(legs: 4)
@ -718,8 +718,8 @@ for object in objects {
```swift ```swift
@objc protocol CounterDataSource { @objc protocol CounterDataSource {
@optional func incrementForCount(count: Int) -> Int optional func incrementForCount(count: Int) -> Int
@optional var fixedIncrement: Int { get } optional var fixedIncrement: Int { get }
} }
``` ```

View File

@ -180,7 +180,7 @@ struct IntStack {
```swift ```swift
struct Stack<T> { struct Stack<T> {
var items = T[]() var items = [T]()
mutating func push(item: T) { mutating func push(item: T) {
items.append(item) items.append(item)
} }
@ -254,7 +254,7 @@ func someFunction<T: SomeClass, U: SomeProtocol>(someT: T, someU: U) {
这里有个名为`findStringIndex`的非泛型函数,该函数功能是去查找包含一给定`String`值的数组。若查找到匹配的字符串,`findStringIndex`函数返回该字符串在数组中的索引值(`Int`),反之则返回`nil` 这里有个名为`findStringIndex`的非泛型函数,该函数功能是去查找包含一给定`String`值的数组。若查找到匹配的字符串,`findStringIndex`函数返回该字符串在数组中的索引值(`Int`),反之则返回`nil`
```swift ```swift
func findStringIndex(array: String[], valueToFind: String) -> Int? { func findStringIndex(array: [String], valueToFind: String) -> Int? {
for (index, value) in enumerate(array) { for (index, value) in enumerate(array) {
if value == valueToFind { if value == valueToFind {
return index return index
@ -356,7 +356,7 @@ protocol Container {
```swift ```swift
struct IntStack: Container { struct IntStack: Container {
// IntStack的原始实现 // IntStack的原始实现
var items = Int[]() var items = [Int]()
mutating func push(item: Int) { mutating func push(item: Int) {
items.append(item) items.append(item)
} }
@ -389,7 +389,7 @@ struct IntStack: Container {
```swift ```swift
struct Stack<T>: Container { struct Stack<T>: Container {
// original Stack<T> implementation // original Stack<T> implementation
var items = T[]() var items = [T]()
mutating func push(item: T) { mutating func push(item: T) {
items.append(item) items.append(item)
} }
@ -447,7 +447,7 @@ func allItemsMatch<
} }
// 检查两个Container相应位置的元素彼此是否相等 // 检查两个Container相应位置的元素彼此是否相等
for i in 0..someContainer.count { for i in 0..<someContainer.count {
if someContainer[i] != anotherContainer[i] { if someContainer[i] != anotherContainer[i] {
return false return false
} }