Skip to content

Instantly share code, notes, and snippets.

@nrlnishan
nrlnishan / property-wrapper-3.swift
Last active May 20, 2020 12:44
Property Wrapper in Swift
// We make our Property Wrapper Generic.
@propertyWrapper
struct Store<T> {
// This is the key that user can provide
var key: String
// The default value for the property
var defaultValue: T
@nrlnishan
nrlnishan / property-wrapper-2.swift
Last active May 20, 2020 12:42
Property Wrapper in Swift - Article
@propertyWrapper
struct Store {
// Key for UserDefaults
var key: String
// This is the special variable required in Property Wrapper.
var wrappedValue: Bool {
set { UserDefaults.standard.set(newValue, forKey: key) }
get { UserDefaults.standard.value(forKey: key) as? Bool ?? false }
@nrlnishan
nrlnishan / property-wrapper-1.swift
Created May 19, 2020 14:47
Property Wrapper in Swift - 1
struct AppSettings {
// Update the respective value in UserDefault
static var isDarkModeEnabled = false {
didSet {
UserDefaults.standard.setValue(isDarkModeEnabled, forKey: "is_dark_mode_enabled")
}
}
}
@nrlnishan
nrlnishan / NoRetainCycle.swift
Last active March 9, 2020 04:59
Example showing no retain cycle
class SecondViewController: UIViewController {
var someText = ""
var controller: UIAlertController!
var label = UILabel()
override func loadView() {
view = UIView()
view.backgroundColor = .white
@nrlnishan
nrlnishan / shakeit.swift
Created February 14, 2019 18:39
Shake Animation
extension UIView {
func shake() {
let animation = CAKeyframeAnimation()
animation.keyPath = "position.x"
animation.values = [0, 10, -10, 10, -5, 5, -5, 0 ]
animation.keyTimes = [0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1]
animation.duration = 0.4
animation.isAdditive = true
@nrlnishan
nrlnishan / Adaptive-Scrollview1.swift
Created May 19, 2018 14:59
Scrollview - Adaptive View
// DRAFT:-
1. Give scrollview with top, leading, trailing & bottom constraint
2. Give the container view inside scrollview with
- Center x to scrollview
- Center y to scrollview
- trailing to scrollview
- bottom to scrollview
- Equal Height to scrollview
- Equal Width to scrollview
@nrlnishan
nrlnishan / ConstraintPriority.txt
Created February 16, 2018 05:03
Content Hugging & Compression Resistance
1. Content Hugging Property:
- This priority defines whether the content size should increase beyond its intrinsic size.
- Content Hugging :: Content doesnot want to grow.
Horizontal Hugging: Higher priority means that content width doesnot want to grow beyond its intrinsic width.
Vertical Hugging: Higher priority means that content height doesnot want to grow beyond its intrinsic height.
2. Content Compression Resistance
- This priority defines whether the content size should decrease beyond its intrinsic size.
- Content Compression Resistance :: Content doesnot want to shrink
@nrlnishan
nrlnishan / MultipleEnvironments-iOS.txt
Last active January 17, 2018 16:27
Managing multiple environments in Xcode
1. Duplicate Build Configurations from Project Settings
- Example: Duplicate debug & name it as debugDev. Duplicate release & name it as releaseDev.
2. Change Project's bundle identifier from Target Build Settings
- Change it to reflect build configurations
3. Create User Defined Settings
- There is + sign on navigation bar in Build Settings. Click on it & create User Defined Settings as needed
- Example: If you need different names on different builds, add setting name APP_DISPLAY_NAME & so on.
- You need to use this created settings on info.plist file. Eg, Change Bundle Display Name value to $(APP_DISPLAY_NAME).
@nrlnishan
nrlnishan / CustomUIView
Created November 1, 2017 15:28
Creating a custom UIView using Xib
Steps for creating custom UIView.
1. Create a xib file, eg: MyCustomView.xib
2. Do necessary layout in xib file
3. Create a UIView file. eg: MyCustomView.swift.
4. Set this MyCustomView.swift file as the file owner of that xib
5. Create necessary outlets from the .xib file to .swift file. Important: Create one outlet for the root parent UIView to .swift file.
Name this outlet as as containerView
6. Override both the init method i.e using decoder & frame
7. Set the code as follows
@nrlnishan
nrlnishan / UIColorExtension.swift
Created October 7, 2017 08:04
UIColor Extensions
class func from(r:CGFloat,g:CGFloat,b:CGFloat,a:CGFloat) -> UIColor {
return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
}
// Hex should be in format #bcbcbc
class func from (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {