Skip to content

Instantly share code, notes, and snippets.

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