Skip to content

Instantly share code, notes, and snippets.

@enomoto
enomoto / UserDefaultsAndStructArray.swift
Created July 17, 2018 07:03
save array of struct to UserDefaults
import Foundation
struct Book: Codable {
let title: String
let author: String
}
let KeyForUserDefaults = "myKey"
func save(_ books: [Book]) {
@enomoto
enomoto / LocaleExtension.swift
Last active August 12, 2024 07:03
Change locale in XCTestCase
private extension MyTests {
private func setLocaleAsJP() {
let original = class_getClassMethod(NSLocale.self, #selector(getter: NSLocale.current))!
let swizzled = class_getClassMethod(NSLocale.self, #selector(NSLocale.myCurrentLocale))!
method_exchangeImplementations(original, swizzled)
}
}
// https://stackoverflow.com/questions/31065859/how-can-i-change-the-locale-on-the-xcode-playground
fileprivate extension NSLocale {
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i, num in enumerate(nums):
for j, num2 in enumerate(nums):
if i == j:
@enomoto
enomoto / bump_version_of_app.py
Created January 24, 2019 08:24
This script bumps iOS app version numbers. (Python 3.7.1)
print("start")
# update version numbers
current_ver_str = '1.0.0' # 現在のバージョン番号
new_ver_str = '1.1.0' # 新しいバージョン番号
# plist file names
file_names = [
'Foo/SupportingFiles/Info.plist',
'Foo/SupportingFiles/InfoStaging.plist',
@enomoto
enomoto / XCTestCase+Stub.swift
Last active January 30, 2019 03:47
Xcode 10.1, Swift 4.1, install [Mockingjay](https://github.com/kylef/Mockingjay) with Cocoapods
// XCTestCase+Stub.swift
import Foundation
import XCTest
import Mockingjay
extension XCTestCase {
/// Web API のリクエストをテストプロジェクトの json でスタブする
///
/// - Parameters:
import Foundation
@testable import Foobar
import Quick
import Nimble
import Mockingjay
import APIKit
final class FooRequestSpec: QuickSpec {
override func spec() {
describe("FooRequest") {
@enomoto
enomoto / stride.swift
Last active April 7, 2019 12:35
replacement of C style for loop in Swift 5(Xcode 10.2)
// for var i = 0; i < 5; i += 1
for i in stride(from: 0, to: 5, by: 1) {
print(i) // 0, 1, 2, 3, 4
}
@enomoto
enomoto / RoundedCornersLabel.swift
Last active May 9, 2019 03:12
UILabel with rounded corners (Swift 5, Xcode 10.2)
@IBDesignable
public final class RoundedCornersLabel: UILabel {
public override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
configure()
@enomoto
enomoto / PlaygroundDrivenDevelopment1.swift
Last active May 9, 2019 08:23
Playground Driven Development (Xcode 10.2.1, Swift 5)
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
@enomoto
enomoto / StackViewInitialize.swift
Last active July 20, 2022 04:38
UIStackView initialization idiom
import UIKit
let stackView = UIStackView()
contentView.addSubview(stackView)
stackView.axis = .horizontal // axis's default value is horizontal.
// 以下2行はセット
stackView.isLayoutMarginsRelativeArrangement = true
stackView.directionalLayoutMargins = .init(top: 0, leading: 16, bottom: 0, trailing: 16)
stackView.spacing = 6