Skip to content

Instantly share code, notes, and snippets.

View yutailang0119's full-sized avatar

Yutaro Muta yutailang0119

View GitHub Profile
struct Hoge {
let id: Int
let title: String
let fuga: Fuga? //更にstructを内包
//メンバワイズイニシャライザ
init(id: Int, title: String,fuga: Fuga?) {
self.id = id
self.title = title
self.fuga = fuga
@yutailang0119
yutailang0119 / DispatchQueueSwift3.swift
Last active January 5, 2017 10:45
Swift3.0で `dispatch_async` が使えなくなると聞いて
// Swift2.x
dispatch_async(dispatch_get_main_queue()) { [weak self]() in
if let weakSelf = self {
weakSelf.tableView.reloadData()
}
}
//Swift3.0
DispatchQueue.main.async { [weak self] in
if weakSelf = self {
//例えばUITableViewの更新
@IBOutlet weak var tableView: UITableView!
let str = "hoge"
// 1
let complete1 = { [weak self]() in
if let weakSelf = self {
print(weakSelf.str) // プロパティにアクセスする => str
weakSelf.tableView.reloadData() // メソッドを使う
import UIKit
// Genericsだけでreturnの型を決めたい
struct GenerucTest1<ViewType: UIView> {
func loadView() -> ViewType {
return UIView() // error return UIView() as! ViewType
}
}
@yutailang0119
yutailang0119 / CodableForArrayJson.swift
Last active July 13, 2017 00:55
JSONの中身が配列の時はdecodeのTypeに[T].selfを渡せばいいっぽい
let data = """
[
{"id": 1, "name": "yutaro"},
{"id": 2, "name": "yutailang"}
]
""".data(using: .utf8)!
struct User: Codable, CustomStringConvertible {
let id: Int
let name: String
@yutailang0119
yutailang0119 / .gitconfig
Created September 23, 2017 10:51
Sample .gitconfig
# This is Git's per-user configuration file.
[user]
name =
email =
[core]
excludesfile =
[color]
ui = auto
[difftool "sourcetree"]
cmd = opendiff \"$LOCAL\" \"$REMOTE\"
let values: [Int?] = [1, 2, nil, 4, 5]
for value in values where value != nil {
print(value!, type(of: value!))
}
for case let value? in values {
print(value, type(of: value))
}
import Foundation
enum Phrase: Int, CustomStringConvertible {
case zun = 0
case doko = 1
var description: String {
switch self {
case .zun:
return "ずん"
@yutailang0119
yutailang0119 / xcode.fish
Created October 11, 2018 10:03
Fish shell function that open file by Xcode
# ~/.config/fish/functions/xcode.fish
function xcode -d "Open file by Xcode"
set -l file $argv[1]
set -l a_option $argv[2]
set -l application $argv[3]
if test -z $a_option
set a_option -a
set application Xcode
@yutailang0119
yutailang0119 / studio.fish
Created October 11, 2018 10:03
Fish shell function that open file by Android Studio
~/.config/fish/functions/studio.fish
function studio -d "Open file by Android Studio"
set -l file $argv[1]
set -l a_option $argv[2]
set -l application $argv[3]
if test -z $a_option
set a_option -a
set application 'Android Studio'