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`操作符实现。这两个操作符提供了一种简单达意的方式去检查值的类型或者转换它的类型。
你也可以用来检查一个类是否实现了某个协议,就像在 [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>
## 定义一个类层次作为例子

View File

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

View File

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

View File

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