Skip to content

Instantly share code, notes, and snippets.

View kitasuke's full-sized avatar

Yusuke Kita kitasuke

View GitHub Profile
@kitasuke
kitasuke / SwiftPM-Function-Builders.swift
Last active September 10, 2019 02:11
Experimental code for SwiftPM with Function Builders. Declarative package description for SwiftPM.
import Foundation
@_functionBuilder
struct ContentListBuilder {
static func buildBlock() -> ContentList {
return EmptyContentList()
}
static func buildBlock(_ list: ContentList) -> ContentList {
@kitasuke
kitasuke / StringLiteralExpr-jp.md
Created August 3, 2019 12:33
StringLiteralExpr-jp

Swift 5.1へのStringLiteralExprの改善

こんにちは。iOSエンジニアの@kitasukeです。

今回は、Swift Compilerのパーサー内部でlibSyntaxが作成する、StringLiteralExprに関する改善を行ったので、その内容を簡単に紹介します。
ちなみに、この変更はlibSyntax内での変更でありSwiftのString APIへの変更ではありません。

概要

libSyntaxには、Stringリテラル用にStringLiteralExprStringInterpolationExprの2種類のシンタックスが定義されています。

@kitasuke
kitasuke / defer.sil
Last active March 1, 2018 14:18
Swift -> SIL
sil_stage canonical
import Builtin
import Swift
import SwiftShims
// main
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
%2 = integer_literal $Builtin.Int32, 0 // user: %3
@kitasuke
kitasuke / defer.sil
Created March 1, 2018 14:03
Swift -> SIL
sil_stage canonical
import Builtin
import Swift
import SwiftShims
// main
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
%2 = integer_literal $Builtin.Int32, 0 // user: %3
import Foundation
import RxSwift
import FBSDKCoreKit
import FBSDKLoginKit
enum FacebookSDKError: Error {
case tokenNotFound
}
extension Reactive where Base: FBSDKLoginManager {
import UIKit
import RxSwift
import RxCocoa
extension Reactive where Base: UITableView {
func items<S: Sequence, Cell: UITableViewCell, O: ObservableType>
(cellType: Cell.Type)
-> (_ source: O)
-> (_ configureCell: @escaping (Int, S.Iterator.Element, Cell) -> Void)
-> Disposable

Type-safe API call with Protocol Buffers in Swift

Apple recently open sourced swift-protobuf which is a plugin of Protocol Buffers for swift language. Protocol Buffers in Swift enables us to have type safety, make API faster and unify schema documentation of structured data. I had a chance to use swift-protobuf in my project and thought that there are many benefits for us, so I would like to share my thoughts.

I also created a repository which has sample server/client app with Protocol Buffers. Please take a look here if you're interested in what implementation looks like.

What's Protocol Buffers?

import Foundation
import APIKit
protocol ProtobufRequest: Request {}
extension ProtobufRequest {
var dataParser: DataParser {
return ProtobufDataParser()
}
}
import UIKit
extension UICollectionView {
func registerNib<T: UICollectionViewCell>(forCellType type: T.Type) {
let name = String(describing: type)
let nib = UINib(nibName: name, bundle: nil)
register(nib, forCellWithReuseIdentifier: name)
}
func registerNib<T: UIView>(forSupplementaryViewOfKind elementKind: String, withType type: T.Type) {
import UIKit
extension UITableView {
func registerNib<T: UITableViewCell>(forCellType type: T.Type) {
let name = String(describing: type)
let nib = UINib(nibName: name, bundle: nil)
register(nib, forCellReuseIdentifier: name)
}
func registerNib<T: UIView>(forHeaderFooterType type: T.Type) {