Skip to content

Instantly share code, notes, and snippets.

@iamchiwon
iamchiwon / Rx+Codable.swift
Created November 22, 2019 12:14
RxSwift extension for Codable
import RxSwift
extension ObservableType {
public func mapObject<T: Codable>(type: T.Type) -> Observable<T> {
return flatMap { (data) -> Observable<T> in
if let data = (data as? (HTTPURLResponse, Data))?.1 {
return try self.mapObject(type: type, data: data)
} else if let json = (data as? (HTTPURLResponse, Any))?.1 {
return try self.mapObjectJSON(type: type, json: json)
} else {
// The SwiftUI Lab: https://swiftui-lab.com
// Article: Inspecting the View Tree – Part 3
// https://swiftui-lab.com/communicating-with-the-view-tree-part-3/
import SwiftUI
enum MyViewType: Equatable {
case formContainer // main container
case fieldContainer // contains a text label + text field
case field(Int) // text field (with an associated value that indicates the character count in the field)
let calendar = Calendar.current
let today = calendar.startOfDay(for: Date())
let dayOfMonth = calendar.component(.day, from: today)
let daysOfMonth = calendar.range(of: .day, in: .month, for: today)!
let weekDaysInMonth = (daysOfMonth.lowerBound ..< daysOfMonth.upperBound)
.flatMap { calendar.date(byAdding: .day, value: $0 - dayOfMonth, to: today) }
.filter { !calendar.isDateInWeekend($0) }
@saoudrizwan
saoudrizwan / Storage.swift
Last active October 27, 2021 01:51
Helper class to easily store and retrieve Codable structs from/to disk. https://medium.com/@sdrzn/swift-4-codable-lets-make-things-even-easier-c793b6cf29e1
import Foundation
public class Storage {
fileprivate init() { }
enum Directory {
// Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the <Application_Home>/Documents directory and will be automatically backed up by iCloud.
case documents
@ahcode0919
ahcode0919 / Swift-Codable.swift
Created June 6, 2017 22:53
Swift 4 Codable example (Type -> JSON)
//Simple Type - Person
struct Person: Codable {
let name: String
let age: Int
func getString() -> String {
return "Name: \(name), Age: \(age)"
}
}
@Azoy
Azoy / install-swift-ubuntu.md
Last active December 9, 2022 03:42
Guide on how to install Swift on Ubuntu

Install Swift on Ubuntu

Requirements

  1. Ubuntu 14.04, 16.04, or 16.10

Step 1 - Dependencies

  1. Open up terminal
  2. Install core deps: sudo apt-get install clang libicu-dev git

Step 1.1 - Ubuntu 14.04 Clang

@kharrison
kharrison / CoreDataController.swift
Last active January 31, 2023 22:36
Swift wrapper for NSPersistentContainer - Easy Core Data Setup with iOS 10
//
// CoreDataController.swift
//
// Created by Keith Harrison http://useyourloaf.com
// Copyright (c) 2017 Keith Harrison. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright
@citrusui
citrusui / dropdown.md
Last active July 3, 2024 07:46
"Dropdowns" in Markdown
How do I dropdown?
This is how you dropdown.

<details>
<summary>How do I dropdown?</summary>
<br>
This is how you dropdown.
@wojteklu
wojteklu / clean_code.md
Last active July 3, 2024 08:35
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules

Using Swift Package Manager with iOS

Step 1:

File > New > Project...

Step 2:

Create a Package.swift file in your root project directory, add dependencies, then run swift package fetch on the command line in the same directory. We’re not going to run swift build because it will just complain.