Skip to content

Instantly share code, notes, and snippets.

Thongchai Kolyutsakul hlung

Block or report user

Report or block hlung

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View medium-codable-response-wrapper.swift
let data = """
{
"response": [
{
"name": "Kitty",
},
{
"name": "Doggy",
}
]
View medium-codable-animal.swift
let exampleData = """
[
{
"type": "cat",
"name": "Kitty",
},
{
"type": "dog",
"name": "Doggy",
}
View medium-codable-lossyarray.swift
struct LossyArray<Element: Decodable>: Decodable {
private(set) var elements: [Element]
init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
var elements = [Element]()
if let count = container.count {
elements.reserveCapacity(count)
}
View medium-codable-dynamic-type.swift
struct AnimalContainer: Decodable {
let animal: Animal
enum AnimalType: String, Decodable {
case cat
case dog
}
enum CodingKeys: String, CodingKey {
case type
View medium-codable-strategy.swift
struct UserSettings: Codable {
let someBool: Bool
let someDate: Date
enum CodingKeys: String, CodingKey {
case someBool = "someDifferentKeyBool"
case someDate = "someDifferentKeyDate"
}
}
View medium-codable-urlsession.swift
extension URLSession {
func send<T: Decodable>(_ request: URLRequest,
completion: @escaping ((Result<T, Error>) -> Void)) {
let task = dataTask(with: request) { (data, response, error) in
if let data = data {
do {
let decoded = try JSONDecoder().decode(T.self, from: data)
completion(.success(decoded))
}
catch let error {
View medium-codable-userdefaults.swift
struct UserSettings: Codable {
let someBool: Bool
let someDate: Date
}
var userSettings: UserSettings? {
get {
guard let data = UserDefaults.standard.object(forKey: "userSettings") as? Data else { return nil }
return try? JSONDecoder().decode(UserSettings.self, from: data)
}
@hlung
hlung / git-commit-auto-msg
Last active Nov 6, 2019
Do git add and commit with auto generated message. For laziness reasons 😂.
View git-commit-auto-msg
# Do git add and commit with auto generated message. For laziness reasons 😂.
# It simply reads `git status -s`, converts the beginning M -> Update, A -> Add, and D -> Delete, and use it as a commit message.
# Example:
# git status: M updated.txt
# generated message: Update updated.txt
#
# For debugging sed part:
# echo "M updated.txt\nA added.txt\nD deleted.txt\nR events/BaseEvents.yml -> events/AppEvents.yml" | sed "s/^M /Update/;s/^A /Add/;s/^D /Delete/;s/^R /Rename/;s/->/to/"
alias gaca='git add .; git status -s | sed "s/^M /Update/;s/^A /Add/;s/^D /Delete/;s/^R /Rename/;s/->/to/" | git commit --file -'
View Fastlane podspec version utils.rb
lane :replace_podspec_version do |options|
# Input file (content) => "s.version = '0.1.0'"
# Input new_version => "0.2.0"
# Output file (content) => "s.version = '0.2.0'"
# Note: For debugging, you can remove -i to make it print output to console without editing file in place.
# Somehow backup file *.bak has to be created for edit in-place (-i) option, otherwise sed command will fail.
sh("cd ..; sed -i bk \"s/\\(s.version.*['\\\"]\\).*\\(['\\\"]\\)/\\1#{options[:new_version]}\\2/g\" #{options[:file]}")
end
lane :get_podspec_version do |options|
You can’t perform that action at this time.