Skip to content

Instantly share code, notes, and snippets.

View sarunw's full-sized avatar
🍎
iOS, Swift, SwiftUI

Sarun Wongpatcharapakorn sarunw

🍎
iOS, Swift, SwiftUI
View GitHub Profile
class Cart {
var items: [Product]
var paymentMethod: PaymentMethod
// Old implementation
var deliveryAddress: String
var shippingOption: ShippingOption
}
class OrderSummaryViewController: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "checkout" {
let vc = segue.destination as! ShippingOptionViewController
vc.cart = cart // pass along cart information to next view controller
}
}
}
class OrderSummaryViewController: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "checkout" {
let vc = segue.destination as! DeliveryAddressViewController
vc.cart = cart // pass along cart information to next view controller
}
}
}
extension KeyedEncodingContainer {
mutating func encode(_ value: UIImage,
forKey key: KeyedEncodingContainer.Key) throws {
let imageData = value.jpegData(compressionQuality: 1)
let prefix = "data:image/jpeg;base64,"
guard let base64String = imageData?.base64EncodedString(options: .lineLength64Characters) else {
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: codingPath, debugDescription: "Can't encode image to base64 string."))
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let object = fetchedResultsController.object(at: indexPath)
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
enum ImageEncodingQuality: CGFloat {
case png = 0
case jpegLow = 0.2
case jpegMid = 0.5
case jpegHigh = 0.75
}
extension KeyedEncodingContainer {
mutating func encode(_ value: UIImage,
struct Base64ImageWrapper: Codable {
let image: UIImage
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let base64String = try container.decode(String.self)
let components = base64String.split(separator: ",")
if components.count != 2 {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Wrong data format")
}
@sarunw
sarunw / money.swift
Created October 7, 2018 15:08
Float and Double error
var float: Float = 0.01
var floatSum: Float = 0
for i in 0..<100 {
floatSum += float
}
floatSum
floatSum == 1
var double: Double = 0.01
@sarunw
sarunw / rails-deploy-to-digitalocean.md
Created September 7, 2018 08:43 — forked from kylefdoherty/rails-deploy-to-digitalocean.md
Notes for myself on How to Deploy a Rails App to DigitalOcean with Ubuntu 14.04, Phusion Passenger & Nginx, Postgres, and Capistrano

How to Deploy a Rails App to DigitalOcean with Ubuntu 14.04, Phusion Passenger & Nginx, Postgres, and Capistrano

1. Setup DigitalOcean Droplet

  • Choose Server Image

choose server image

  • Choose Size (if this is a hobby app you can probably get away with the smallest)

choose server size

@sarunw
sarunw / ruby_readline_issue.md
Created April 21, 2018 06:19 — forked from soultech67/ruby_readline_issue.md
ruby bundler: Sorry, you can't use byebug without Readline

Preamble

On OS/X Sierra, after recently running a brew update I started receiving the error message Sorry, you can't use byebug without Readline when trying to run some rake tasks in my ruby project folder. I observed this in projects and gems that include byebug or pry in their Gemfile or gem.spec. I've found in my googling that many begin encountering this error message after running a brew update but there are other triggering conditions as well.

>> rake aws:show_config
WARN: Unresolved specs during Gem::Specification.reset:
      mime-types (>= 0)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
 Sorry, you can't use byebug without Readline. To solve this, you need to