13. class Hoge: NSObject {
var name: String?
var note: String?
override func description() -> String {
return "Name = (self.name),
Note = (self.note)"
}
}
ERROR Method does not override
any method from its superclass
14. class Hoge: NSObject {
var name: String?
var note: String?
override var description: String! {
get {
return "Name = (self.name),
Note = (self.note)"
}
}
}
プロパティのオーバーライドする
22. let i: Int = Int(1.0)
let n: NSInteger = NSInteger(1.0)
!
let f: Float = Float(100)
let d: Double = Double(100)
let g: CGFloat = CGFloat(100)
Swiftではスカラー型のキャストはない
代わりにコンバージョンを利用
23. let index: NSInteger = 1
let width: CGFloat = 100
let r = width * (CGFloat)index
!
// => ERROR Consecutive statements on
a line must be separated by ';'
ObjCライクなキャストはできない
24. let someObjects: [AnyObject] = [
Movie(name: "2001: A Space", director: "Stan"),
Movie(name: "Moon", director: "Duncan Jones"),
Movie(name: "Alien", director: "Ridley Scott")
]
!
for object in someObjects {
let movie = object as Movie // Type Casting !
println("Movie: '(movie.name)',
dir. (movie.director)")
}
補足
オブジェクト型はキャスト可能
25. for movie in someObjects as [Movie] {
println("Movie: '(movie.name)',
dir. (movie.director)")
}
配列をキャストするとコンパクトに書ける
28. let index: NSInteger = 1
let width: CGFloat = 100
let r1 = width * Float(index)
!
// => ERROR Could not find an overload
for '*' that accepts the supplied
arguments
CGFloatとFloatの演算
一見正しく動作しそうだがエラーになる
34. typealias NSInteger = Int
!
!
let i: Int = 1
let n: NSInteger = 1
let x: CGFloat = 3
let y: Float = 3
let w = i + Int(x) + Int(y) // => OK
let q = n + Int(x) + Int(y) // => OK
!
if n == i {
println("HERE!") // => OK
}
35. let u: NSUInteger = 1
!
// ERROR => Use of undeclared type
'NSUInteger'; did you mean to use
'Int'?
NSUInteger は利用できません