Skip to content

Instantly share code, notes, and snippets.

@hisoka0917
hisoka0917 / UIView+frame.swift
Created January 15, 2018 09:48
UIView frame extension
import UIKit
extension UIView {
public var top: CGFloat {
get {
return self.frame.origin.y
}
set {
var frame = self.frame
frame.origin.y = newValue
@hisoka0917
hisoka0917 / DynamicKeys.swift
Last active September 23, 2021 07:33
Make Dictionary<String, Any> Codable
/**
```
// Encode a model with properties of type [String : Any]
var propertiesContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: .properties)
if let properties = properties {
try propertiesContainer.encodeDynamicKeyValues(withDictionary: properties)
}
```
inspired by https://gist.github.com/samwize/a82f29a1fb34091cd61fc06934568f82
*/
@hisoka0917
hisoka0917 / JSONCodingKeys.swift
Created January 17, 2018 09:13
Make Array<Any> and Dictionary<String, Any> Decodable
import Foundation
// Copy from https://gist.github.com/loudmouth/332e8d89d8de2c1eaf81875cfcd22e24
struct JSONCodingKeys: CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
@hisoka0917
hisoka0917 / UIColor+hex.swift
Created January 24, 2018 08:54
UIColor with hex in Swift 4
//
// UIColor+hex.swift
//
// Created by Hisoka0917 on 2018/1/24.
// Copyright © 2018. All rights reserved.
//
import UIKit
import Foundation
@hisoka0917
hisoka0917 / NSObject+ExtensionProps.swift
Last active January 26, 2018 03:29
Extension property in Swift
extension NSObject {
private struct AssociatedKeys {
static var extensionProps: UInt8 = 0
}
private var extensionProps: NSObject? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.extensionProps) as? NSObject
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.extensionProps, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
@hisoka0917
hisoka0917 / UIImage+SolidColor.swift
Last active May 8, 2018 09:31
Get a UIImage with solid color
extension UIImage {
public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height:1)) {
let rect = CGRect(origin: .zero, size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 1.0)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let cgImage = image?.cgImage else {
@hisoka0917
hisoka0917 / Dictionary+UrlQuery.swift
Created February 6, 2018 02:58
Format Dictionary to url query
extension Dictionary {
func urlQueryEncode() -> String {
return self
.map({ "\($0)=\($1)" })
.joined(separator: "&")
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
}
func queryParameters() -> String {
let parameterArray = self.map { (key, value) -> String in
@hisoka0917
hisoka0917 / UIButton+BackgroundColorForState.swift
Created April 8, 2018 08:39
UIButton setBackgroundColor for state in Swift 4
extension UIButton {
func setBackgroundColor(_ color: UIColor, for state: UIControlState) {
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
if let currentGraphicsContext = UIGraphicsGetCurrentContext() {
currentGraphicsContext.setFillColor(color.cgColor)
currentGraphicsContext.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
}
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.setBackgroundImage(colorImage, for: state)
@hisoka0917
hisoka0917 / String+SHA256.swift
Created April 26, 2018 08:39
Swift String SHA256
extentsion String {
func SHA256() -> String {
if let stringData = self.data(using: .utf8) {
return self.hexStringFromData(input: digest(input: stringData as NSData))
} else {
return self
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>items</key>
<array>
<dict>
<key>assets</key>
<array>
<dict>