Skip to content

Instantly share code, notes, and snippets.

@chanonly123
chanonly123 / build_xcframework.sh
Last active June 24, 2024 15:44
Build XCFramework and documentation script
# put this file inside the root directory of the project
# update the framework name
frameworkName="MyFramework"
echo "⬛️ cleaning"
xcodebuild clean
rm -rf output
rm -rf build
@chanonly123
chanonly123 / StoredPropertiesExtenstion.swift
Last active December 17, 2023 19:47
Stored properties in Swift Extensions in Swifty way
import Foundation
import UIKit
extension CustomView {
// can make private
static let storedProperties = WeakDictionary<UIView, Properties>()
struct Properties {
var url: String = ""
@chanonly123
chanonly123 / UIViewXib.swift
Last active May 27, 2020 08:59
Designable view from xib, with optimisation
@IBDesignable
class UIViewXib: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
loadViewFromNib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
@chanonly123
chanonly123 / UIViewXib.swift
Last active May 27, 2020 09:00
Designable view from xib, without optimisation
@IBDesignable
class UIViewXib: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
loadViewFromNib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
@chanonly123
chanonly123 / UITableViewDequeueCell.swift
Created May 2, 2020 07:15
Dequeue UITableView cell using class name
import UIKit
extension UITableView {
func dequeueReusableCell<T: UITableViewCell>(type: T.Type) -> T {
let identifier = String(describing: type)
if let reusableCell = self.dequeueReusableCell(withIdentifier: identifier) {
if let cell = reusableCell as? T {
return cell
} else {
assertionFailure("tableview cell cannot be casted to \(identifier)")
@chanonly123
chanonly123 / UIViewInitFromXib.swift
Last active May 2, 2020 07:13
Instantiating custom UIView from xib
import UIKit
extension UIView {
static func initViewFromNib() -> Self {
let nibName = String(describing: self)
let index = 0
let view = Bundle.main.loadNibNamed(nibName, owner: nil, options: nil)![index]
if let castedView = view as? Self {
return castedView
} else {
@chanonly123
chanonly123 / UIViewControllerInitFromStoryboard.swift
Last active May 2, 2020 07:12
Wrapping up boilerplate code - Interface builder
import UIKit
extension UIViewController {
// must override to provide storyboard
@objc class var fromStoryboard: UIStoryboard? { return nil }
// optional override to provide storyboardId
@objc class var storyboardId: String { return String(describing: self) }
static func instantiate() -> Self {
@chanonly123
chanonly123 / UserDefaultsExtended.swift
Last active April 12, 2020 12:45
Simple propertyWrapper for UserDefaults
@propertyWrapper
class UserDefaultsExtended<Type: Codable> {
let key: String
let loc: UserDefaults
var actualValue: Type?
init(key: String, loc: UserDefaults = UserDefaults.standard) {
self.key = key
self.loc = loc
}