Skip to content

Instantly share code, notes, and snippets.

View hmhmsh's full-sized avatar

shunkun hmhmsh

View GitHub Profile
@hmhmsh
hmhmsh / UICollectionViewCompositionalLayoutGenerator.swift
Last active August 12, 2021 01:10
UICollectionViewCompositionalLayout + Carousel
struct LayoutGenerator {
static func generate(handler: @escaping (Int) -> NSCollectionLayoutSection?) -> UICollectionViewCompositionalLayout {
return .init { section, environment in
handler(section)
}
}
}
struct SectionLayoutGenerator {
static func carousel(width: CGFloat, height: CGFloat, spacing: CGFloat = 8) -> NSCollectionLayoutSection {
@hmhmsh
hmhmsh / DateFormatter+Template.swift
Last active January 29, 2021 07:50
Template enum for DateFormatter
extension DateFormatter {
/**
SeeAlso:
[Date Field Symbol Table](http://www.unicode.org/reports/tr35/tr35-25.html#Date_Field_Symbol_Table)
*/
enum Template: String, CaseIterable {
/**
Era - Replaced with the Era string for the current date. One to three letters for the abbreviated form, four letters for the long form, five for the narrow form.
*/
case G
@hmhmsh
hmhmsh / ApiUrl.swift
Created January 14, 2021 13:45
Example struct to generate api url
protocol Api {
var path: String { get }
}
struct QiitaApi: Api {
var path: String {
protocolName + domain + "/" + version + "/" + directory
}
private let protocolName = "https://"
@hmhmsh
hmhmsh / hit.swift
Created May 22, 2020 08:50
input: 確率 output: 当たりかどうか
func hit(percent: Float) -> Bool {
let max = Int(100 / fabsf(percent))
let value = Int.random(in: 1 ... max)
return value == 1
}
// MARK: - get
extension UserDefaults {
/**
-objectForKey: will search the receiver's search list for a default with the key 'defaultName' and return it. If another process has changed defaults in the search list, NSUserDefaults will automatically update to the latest values. If the key in question has been marked as ubiquitous via a Defaults Configuration File, the latest value may not be immediately available, and the registered value will be returned instead.
*/
public func object<T: RawRepresentable>(forKey defaultName: T) -> Any? where T.RawValue == String {
UserDefaults.standard.object(forKey: defaultName.rawValue)
}
/// -stringForKey: is equivalent to -objectForKey:, except that it will convert NSNumber values to their NSString representation. If a non-string non-number value is found, nil will be returned.
@hmhmsh
hmhmsh / UITableView+Row.swift
Created January 30, 2020 06:56
UITableViewCellで自作enumを使う
extension UITableView {
func dequeueReusableCell<ROW: Identifiable>(with row: ROW, for indexPath: IndexPath) -> UITableViewCell where ROW.ID == String {
return self.dequeueReusableCell(withIdentifier: row.id, for: indexPath)
}
}
@hmhmsh
hmhmsh / DataProvider.swift
Last active January 30, 2020 06:34
非同期処理でcompletionをメソッドチェーンで書く
struct DataProvider<T: Decodable> {
let function: (@escaping (Result<T, Error>) -> Void) -> Void
func completion(_ completion: @escaping (Result<T, Error>) -> Void) {
function(completion)
}
}
@hmhmsh
hmhmsh / SaizeriyaCategory.swift
Last active May 17, 2019 10:19
Saizeriya Menu List (2019.05.17時点) // jsonのsaltの桁数あとで修正予定
enum SaizeriyaMenuCategory: String, Codable {
case saladSoup = "サラダ・スープ"
case appetizer = "前菜・おつまみ"
case fromItalyWine = "イタリアの味・ワイン"
case pizza = "ピザ"
case pasta = "パスタ"
case doriaRice = "ドリア・米料理"
case hamburgSteak = "肉料理"
case dessert = "デザート"
@hmhmsh
hmhmsh / TextAlertController.swift
Created October 9, 2018 10:34
UIAlertController with TextField
struct TextAlertController {
func create(title: String, message: String, preferredStyle: UIAlertController.Style = .alert, defaultText: String? = nil, completion: @escaping (String?) -> Void) -> UIAlertController {
let actionController = UIAlertController(title: title, message: message, preferredStyle: preferredStyle)
let ok = UIAlertAction(title: "OK", style: .default, handler: { [weak actionController] (_) -> Void in
guard let textField = actionController?.textFields?.first else {
completion(nil)
return
}
struct Navigator {
static func toMainViewController(from parent: UIViewController) {
self.present(from: parent, to: StoryboardBuilder.mainViewController)
}
// 遷移先のViewControllerに値を渡す場合
static func toDetailViewController(from parent: UIViewController, id: String) {
let detail = StoryboardBuilder.detailViewController
detail.id = id
self.present(from: parent, to: detail)