Skip to content

Instantly share code, notes, and snippets.

@sho-ito-1027
sho-ito-1027 / CustomNestedJSONDecoder.swift
Last active January 22, 2018 08:38
JSONと異なる形でDecodeしたい場合、`init(from decoder: Decoder) throws`を実装
import Foundation
struct Response: Decodable {
let main: Main
struct Main: Decodable {
let sub: Sub
struct Sub: Decodable {
let a: UInt
@sho-ito-1027
sho-ito-1027 / StringExtensions.swift
Created February 8, 2018 07:58
Swift4 StringのアクセスをsubscriptでIntによって可能にした
extension String {
/// String[index]
///
/// - Parameter i: Index of String
public subscript(i: Int) -> String {
let index = self.index(startIndex, offsetBy: i)
return String(self[index])
}
@sho-ito-1027
sho-ito-1027 / ascii.swift
Last active February 19, 2018 17:14
error: unprintable ASCII character found in source file
// playground
var total: String = ""
for i: UInt8 in 0...255 {
let data1: [UInt8] = [i]
let value = Data.init(bytes: data1)
let char = String.init(data: value, encoding: .utf8)
if let char = char {
total += char
}
@sho-ito-1027
sho-ito-1027 / Routable.swift
Last active March 6, 2018 03:24
画面遷移のProtocol(たぶんあまりよくない例)
import UIKit
enum PresentDestination {
case viewControllerA
case viewControllerB(content: Content)
var viewController: UIViewController {
switch self {
case .viewControllerA:
return ViewControllerA.initiateWithNavigation()
@sho-ito-1027
sho-ito-1027 / ViewAndPresenterProtocols.swift
Last active March 28, 2018 09:01
DESERIALIZATION FAILURE
// 1ファイルで両方書いてある場合、Build成功
// 1ファイル1Protocolで書いてある場合、Build失敗
protocol ViewProtocol: class {
associatedtype Presenter: PresenterProtocol
var presenter: Presenter! { get }
}
protocol PresenterProtocol: class {
associatedtype View: ViewProtocol
weak var view: View? { get }
@sho-ito-1027
sho-ito-1027 / buildResult.log
Created May 27, 2018 08:41
Simulatorsの実行結果
// Xcode 9.3.1のプロジェクト
// build時はiPhone 8 Plusを選択していた
//
Akari% simulators --project SimTest9.3.xcodeproj --scheme SimTest9.3 --beforeClean true --devices iPhone SE,iPhone X,iPhone 9 Plus,iPhone 4s --osVersion 9.3,11.3
=== start ===
Build settings from command line:
SDKROOT = iphonesimulator11.3
=== BUILD TARGET SimTest9.3 OF PROJECT SimTest9.3 WITH CONFIGURATION Debug ===
@sho-ito-1027
sho-ito-1027 / Simulators.findDevice.log
Created May 28, 2018 03:07
Simulators.findDevice(for:withOSVersion:andDevices)の変数をprintした内容
999277% simulators --project HogeApp.xcodeproj --scheme HogeApp --beforeClean true --devices "iPhone X" --osVersion 11.2 --showBuildLog true
Build settings from command line:
SDKROOT = iphonesimulator11.3
Build Preparation
Build task concurrency set to 2 via user default IDEBuildOperationMaxNumberOfConcurrentCompileTasks
=== BUILD TARGET HogeApp OF PROJECT HogeApp WITH CONFIGURATION Debug ===
Check dependencies
import UIKit
final class CircleLineGraphView: UIView {
private enum CircleType {
case background
case main(percentage: CGFloat)
}
private enum CircleAlignment {
case outside
case center
@sho-ito-1027
sho-ito-1027 / youtube_api.playground
Last active February 15, 2019 03:27
youtube api for playground (Swift 4)
import UIKit
let youtubeURLs: [String] = [
"https://www.youtube.com/get_video_info?video_id=e0okrDbEsXM",
"https://www.youtube.com/get_video_info?video_id=Dwh9hpBWtYQ",
"https://www.youtube.com/get_video_info?video_id=aNyNLEoPx4k"//,
// "https://www.youtube.com/get_video_info?video_id=mM5_T-F1Yn4",
// "https://www.youtube.com/get_video_info?video_id=XYra2ovcWxE"
]
@sho-ito-1027
sho-ito-1027 / StoryboardDependencySample.swift
Created July 10, 2019 15:01
Storyboardを使ったDIもどき
final class ViewController: UIViewController {
// MARK: - Private Property
private var dependency: Int!
// MARK: - Initilizer
static func initiate(dependency: Int) -> ViewController {
let viewController = UIStoryboard.instantiateInitialViewController(from: self)
viewController.dependency = dependency
return viewController
}