Skip to content

Instantly share code, notes, and snippets.

@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:
@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',
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 / 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 {
@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 / swiftc_help.txt
Created June 18, 2018 20:37
$ swiftc -help
OVERVIEW: Swift compiler
USAGE: swiftc [options] <inputs>
MODES:
-dump-ast Parse and type-check input file(s) and dump AST(s)
-dump-parse Parse input file(s) and dump AST(s)
-dump-scope-maps <expanded-or-list-of-line:column>
Parse and type-check input file(s) and dump the scope map(s)
-dump-type-refinement-contexts
@enomoto
enomoto / AppVersionComparison.swift
Created May 17, 2018 01:05
samples of NSComparisonResult
import Foundation
// https://developer.apple.com/documentation/foundation/nscomparisonresult
let currentVersion = "0.0.2"
let latestVersion1 = "0.0.3"
let latestVersion2 = "0.0.21"
let latestVersion3 = "0.1.0"
let latestVersion4 = "1.0.0"
let latestVersion5 = "0.0.2"
@enomoto
enomoto / sqrt.rkt
Created May 11, 2018 04:05
SICP 1.1.7
#lang racket
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
@enomoto
enomoto / PromiseKitExample.swift
Last active May 10, 2018 01:07
PromiseKit 6 example with Xcode 9.2 and Swift 4.1
import Foundation
import PromiseKit
struct YoRepository {
func fetchDataWithPromise() -> Promise<String> {
return Promise { seal in
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .milliseconds(500)) {
guard true else {
// failure
seal.reject(MyError.unknown)
@enomoto
enomoto / 1.3.rkt
Created April 27, 2018 13:29
SICP excercise 1.3
#lang racket
(define (sum_of_square a b c)
(cond ((< a b) (< a c) (+ (* b b) (* c c)))
((< b c) (< b a) (+ (* a a) (* c c)))
(else (+ (* a a) (* b b)))
)
)
(sum_of_square 3 4 5) ;41
(sum_of_square 8 4 5) ;89