Skip to content

Instantly share code, notes, and snippets.

View novinfard's full-sized avatar

Soheil Novinfard novinfard

View GitHub Profile
@novinfard
novinfard / animateBorderWithConformingToProtocol.swift
Created January 31, 2019 17:11
[Animate border of UIView by protocol implementation]
protocol PLAnimatableBorder {
func animateBorderWidth(toValue: CGFloat, duration: Double)
}
extension PLAnimatableBorder where Self: UIView {
/// animate the border width
func animateBorderWidth(toValue: CGFloat, duration: Double) {
let animation: CABasicAnimation = CABasicAnimation(keyPath: "borderWidth")
animation.fromValue = layer.borderWidth
animation.toValue = toValue
@novinfard
novinfard / DifferentLevelOfErrorAssertingInSwift.txt
Created February 1, 2019 13:44
[Different level of error asserting in swift and corresponding result]
debug release release
function -Onone -O -Ounchecked
assert() YES NO NO
assertionFailure() YES NO NO**
precondition() YES YES NO
preconditionFailure() YES YES YES**
fatalError()* YES YES YES
– assert: checking your own code for internal errors
– precondition: for checking that your clients have given you valid arguments.
@novinfard
novinfard / gradientLayerToView.swift
Last active February 4, 2019 16:12
[Add gradient layer to view]
private let gradientLayer = CAGradientLayer()
override func layoutSubviews() {
super.layoutSubviews()
super.layoutIfNeeded()
if let viewBounds = self.gradientView?.bounds {
self.gradientLayer.frame = viewBounds
}
}
@novinfard
novinfard / spaceImageInUIButton.swift
Last active June 17, 2019 10:11
[Set space between image and title in UIButton]
let spacing: CGFloat = 10 // the amount of spacing to appear between image and title
self.button?.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: spacing)
self.button?.titleEdgeInsets = UIEdgeInsets(top: 0, left: spacing, bottom: 0, right: 0)
@novinfard
novinfard / [CheckDeepLinksiOS.sh
Created February 7, 2019 17:42
[Check deep links in iOS simulator]
xcrun simctl openurl booted "PROTOCOL://URLPATH/"
@novinfard
novinfard / navigationFontColor.swift
Last active February 13, 2019 09:41
[Setup navigation bar's color and font]
guard let navBar = self.navigationController else { return }
if #available(iOS 11.0, *) {
navBar.navigationBar.prefersLargeTitles = true
navBar.navigationItem.largeTitleDisplayMode = .always
navBar.navigationBar.largeTitleTextAttributes = [
NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 36)]
@novinfard
novinfard / CoreDataPersistentContainerUniteTest.swift
Created February 27, 2019 16:07
[differences between loading persistent container within the app or unit test
// Inside the app (SQLite)
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "CoreDataUnitTesting")
container.loadPersistentStores { (description, error) in
if let error = error {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
}
return container
@novinfard
novinfard / performNetworkRequest.swift
Last active March 4, 2019 23:25
[Syncing multiple async tasks in Swift - code snippets 1]
func performNetworkRequest(url: String,
completion: @escaping (Data?, Error?) -> Void) {
// create a url
let requestUrl = URL(string: url)
// create a data task
let task = URLSession.shared.dataTask(with: requestUrl!) { (data, response, error) in
completion(data, error)
}
task.resume()
@novinfard
novinfard / initialise.swift
Created March 4, 2019 23:26
[Syncing multiple async tasks in Swift - code snippets 2]
let baseUrl = "https://jsonplaceholder.typicode.com/"
let endpoints = ["posts", "comments", "albums", "photos", "todos", "users"]
@novinfard
novinfard / dispatchGroup.swift
Created March 4, 2019 23:27
[Syncing multiple async tasks in Swift - code snippets 3]
let group = DispatchGroup()
endpoints.forEach { endpoint in
group.enter()
performNetworkRequest(url: baseUrl + endpoint) { data, error in
print("Task \(endpoint) is done")
group.leave()
}
}
// notify the main thread when all task are completed