24
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Objective-C 使って、Swiftにかわった時にハマりそうところまとめ

Last updated at Posted at 2014-08-01

自分でswift本よんで、実際にプロジェクト使う時ハマるかもしれないところをまとめてみました(追加していく予定)。

Selector

swiftには @selectorがない、stringはそのままにSelectorになる(自動変換)
performSelector: も使えない、代わりに"?"を使う

let mySelector: Selector = "tappedButton:"

myButton.addTarget(self, action: "tappedButton:", forControlEvents: .TouchUpInside)

NSError point

NSError ** swiftでは使えない。代わりにNSErrorPointerを使う

var writeError: NSError?
let written = myString.writeToFile(path, atomically: false,
    encoding: NSUTF8StringEncoding,
    error: &writeError)
if !written {
    if let error = writeError {
        println("write failure: \(error.localizedDescription)")
    }
}

Closures _block

objective-c block使ってるとき block内でblock外の変数値変えるできるにするためには 変数に _blockにつける必要ある。
それがswiftの Closuresではデフォルトの仕様になってます。つまり変数に_blockつけなくても変えるようになった.

if ([obj isKindOfClass:[Foo class]]) { … })

var someValue : Any?
 
someValue = "String"
 
if someValue is String {
    println("someValue is a String")
} else {
    println("someValue is something else")
}
24
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?