Last active
August 29, 2015 14:25
-
-
Save syou007/310bfa0232a6776dc4bc to your computer and use it in GitHub Desktop.
僕がSwiftで使ってる構文のメモ ref: http://qiita.com/syou007/items/d2add74903a5e6a626be
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var str = "初期化コード" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var str:String? | |
var human:Human? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function("引数", { () in | |
// コールバックとしての処理 | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function("引数", { | |
// コールバックとしての処理 | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function("引数") { | |
// コールバックとしての処理 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func function(str:String, callback:(human:Human)->()) { | |
// なんらかの処理 | |
callback(Human()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function("引数", { (human) in | |
// コールバックとしての処理 | |
human.run() | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function("引数") { (human:Human) in | |
// コールバックとしての処理 | |
human.run() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function("引数") { (human) in | |
// コールバックとしての処理 | |
human.run() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function("引数") { | |
// コールバックとしての処理 | |
$0.run() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function("引数") { | |
// コールバックとしての処理 | |
print("コンパイルエラー") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 定義 | |
var function:()->() | |
// 処理の設定 | |
function = { () in | |
// 呼び出したい処理 | |
} | |
function() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
str = "Optionalデータ" | |
if let s = str { | |
print("\(s)") | |
} | |
human = Human() | |
human?.run() | |
human!.run() // 値が入っていることがわかっている場合は`!`で呼び出すこともある。 | |
human?.foot?.kick() // チェーンしても問題ない。途中でnilがある場合はそれ以降の行われずnilと評価される。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 定義 | |
var function:(human:Human)->() | |
// 処理の設定 | |
function = { | |
// 呼び出したい処理 | |
$0.run | |
} | |
function(Human()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBAction func onClick(sender:AnyObject) { | |
// APIを呼び出す処理 | |
API.call() { | |
// APIの結果を画面に描画する。 | |
self.write($0) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBAction func onClick(sender:AnyObject) { | |
// APIを呼び出す処理 | |
API.call() { [weak self] in | |
// APIの結果を画面に描画する。 | |
self?.write($0) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBAction func onClick(sender:AnyObject) { | |
// APIを呼び出す処理 | |
API.call() { [weak self] in | |
// APIの結果を画面に描画する。 | |
if let _self = self { | |
_self.write($0) | |
// 長い処理・・・ | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBAction func onClick(sender:AnyObject) { | |
// APIを呼び出す処理 | |
API.call() { [weak self, myButton, myObjyect] in | |
// APIの結果を画面に描画する。 | |
self?.write($0) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@object ||= Object.new |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.object = self.object ?? Object() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.object = self.object != nil ? self.object : Object() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let floatObj = 0.5 | |
let intObj = Int32(floatObj) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ObjectParent {} | |
class objectChild : ObjectParent {} | |
let objectParent = ObjectParent() | |
let objectChild = objectChild as? ObjectChild |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if let s = str, let h = human { | |
print("\(s)") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let objectParent = ObjectParent() | |
let objectChild = objectChild as! ObjectChild // 実行時エラー! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let objectParent = ObjectChild() | |
let objectChild = objectChild as! ObjectChild |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let objectParent = ObjectChild() | |
let objectChild = objectChild as? ObjectChild |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let objectParent = ObjectChild() | |
if let objectChild = objectChild as? ObjectChild { | |
// objectChildの場合の処理を記載 | |
} else { | |
// objectParentの場合の処理を記載 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Type { | |
case A | |
case B | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum TypeString:String { | |
case A = "a" | |
case B = "b" | |
} | |
// 文字列からEnumを生成。Optional型。 | |
let typeString = TypeString(rawValue: "a") | |
print("type = |(typeString!.rawValue())" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum TestEnum { | |
case TestA | |
case TestB | |
} | |
func test(testEnum:TestEnum) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test(TestEnum.TestA) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test(.TestA) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func text() -> String { | |
retrun "test" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var callback:(()->())? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 空で初期化 | |
let array:[String] = [] | |
let array:[String] = Array() | |
let array:[String] = [String]() | |
let array = [String]() | |
// データを入れて初期化 | |
var array:[String] = ["データ1", "データ2"] | |
var array = ["データ1", "データ2"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 空で初期化 | |
let array:[String: String] = Dictionary() | |
let array:[String: String] = [:] | |
let array:[String: String] = [String: String]() | |
let array = [String: String]() | |
// データを入れて初期化 | |
let array:[String: String] = ["Key1": "データ1", "Key2": "データ2"] | |
let array = ["Key1": "データ1", "Key2": "データ2"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = [ObjectClass.AnyEnum]() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array:[ObjectClass.AnyEnum] = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array = ["データ1", "データ2"] | |
array.each_with_index { |str, index| | |
p "#{index}:#{str}" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contains(["Test1", "Test2"], "Test1") // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = ["データ1", "データ2"] | |
for str in array { | |
print("\(str)") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array = ["データ1", "データ2"] | |
array.each_with_index { |str, index| | |
p "#{index}:#{str}" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = ["データ1", "データ2"] | |
for (index, str) in enumerate(array) { | |
print("\(index):\(str)") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array = ["データ1", "データ2"] | |
array.each_with_index { |str, index| | |
p "#{index}:#{str}" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = ["データ1", "データ2"] | |
for (index, str) in enumerate(array) { | |
print("\(index):\(str)") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
callback = { () in | |
... | |
// コールバックの処理 | |
} | |
// 呼び出し処理 | |
callback?() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var str:String! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBOutlet weak var text:UITextField! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AutoSync { | |
let object : AnyObject | |
init(_ obj : AnyObject) { | |
object = obj | |
objc_sync_enter(object) | |
} | |
deinit { | |
objc_sync_exit(object) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func function(str:String, callback:()->()) { | |
// なんらかの処理 | |
callback() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment