Skip to content

Instantly share code, notes, and snippets.

View nurtugan's full-sized avatar
🏠
Working from home

Nurtugan Nuraly nurtugan

🏠
Working from home
View GitHub Profile
@nurtugan
nurtugan / Auto Layout by Tutorials.swift
Last active August 7, 2020 15:22
Useful code snippets from Auto Layout by Tutorials book
// Chapter 1: Introducing Auto Layout
// Chapter 2: Construct Auto Layout with the Interface Builder
// 1
override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
view.addSubview(contactPreviewView)
contactPreviewView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([ // this line of code more performant than activating each constraint individually
contactPreviewView.widthAnchor.constraint(equalToConstant: 150),
contactPreviewView.heightAnchor.constraint(equalToConstant: 150),
@nurtugan
nurtugan / ObjectMapper.swift
Created June 19, 2020 08:29
Example of mocking json with ObjectMapper
guard let path = Bundle.main.path(forResource: "ScheduleMock", ofType: "json") else {
return
}
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
let schedule = try Mapper<TakeawayScheduleModel>().map(JSONString: data.string)
print(schedule.title)
} catch {
print(error.localizedDescription)
assertionFailure()
@nurtugan
nurtugan / PrettyPrint.swift
Created May 29, 2020 05:34
Pretty Print Swift Dictionary
print(String(data: try! JSONSerialization.data(withJSONObject: json, options: .prettyPrinted), encoding: .utf8 )!)
@nurtugan
nurtugan / DiffableDataSource-DeleteItem.swift
Last active July 17, 2024 20:28
Example of Deleting Item from Diffable Data Source
@objc
private func handleGet(_ sender: UIButton) {
var superview = sender.superview
while superview != nil {
if let cell = superview as? UICollectionViewCell {
guard let indexPath = collectionView.indexPath(for: cell),
let objectIClickedOnto = diffableDataSource.itemIdentifier(for: indexPath) else { return }
var snapshot = diffableDataSource.snapshot()
snapshot.deleteItems([objectIClickedOnto])
diffableDataSource.apply(snapshot)
@nurtugan
nurtugan / UIView+Layout.swift
Created May 8, 2020 20:20
Making Programmatic Auto Layout Easy through Extensions
//
// UIView+Layout.swift
// Project X
//
// Created by Brian Voong on 2/10/19.
// Copyright © 2019 Brian Voong. All rights reserved.
//
import UIKit
@nurtugan
nurtugan / .swiftlint.yml
Created April 18, 2020 11:50
My SwiftLint Rules
disabled_rules:
excluded:
- Pods
- ProjectX/Supporting Files/R.generated.swift # R.swift
opt_in_rules:
- anyobject_protocol
- array_init
- attributes
@nurtugan
nurtugan / DismissKeyboard.swift
Created March 29, 2020 18:55
Dismiss Keyboard by touching anywhere
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap = UITapGestureRecognizer(
target: self,
action: #selector(dismissKeyboard)
)
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@nurtugan
nurtugan / TextFieldSwitching.swift
Last active April 18, 2020 10:53
Switching between Text Fields on pressing return key
extension MyViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Try to find next responder
if let nextTextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {
nextTextField.becomeFirstResponder()
} else {
// Not found, so remove keyboard
textField.resignFirstResponder()
}
// Do not add a line break
@nurtugan
nurtugan / AppearanceManager.swift
Last active March 29, 2020 17:46
My Default Appearance Manager
import UIKit
final class AppearanceManager {
static let shared = AppearanceManager()
private init () {}
func setDefaultAppearance() {
// Removes all navigation bar back button title
UIBarButtonItem.appearance().setTitleTextAttributes(
@nurtugan
nurtugan / UIImageView+Kingfisher.swift
Last active April 18, 2020 10:54
Kingfisher configs
import Kingfisher
import UIKit
extension UIImageView {
static func clearImageCache() {
KingfisherManager.shared.cache.clearMemoryCache()
KingfisherManager.shared.cache.clearDiskCache()
}
/// UIImageView+: Method for downloading image