Skip to content

Instantly share code, notes, and snippets.

extension Dictionary {
var prettyPrintedJSON: String? {
do {
let data: Data = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return String(data: data, encoding: .utf8)
} catch _ {
return nil
}
}
extension Data {
func toString() -> String? {
let str = String(data: self, encoding: .utf8)
return str
}
func toDictionary() -> [String: Any]? {
do {
return try JSONSerialization.jsonObject(with: self, options: []) as? [String: Any]
} catch {
1. In capabilities need to turn on the iCloud and tick the iCloud Documnet in services. Choose the container and note
down its name. (because while setting up settings inside info.plist we. need to set the NSUbiquitousContainers key value
same as this name).
2. We need to define the iCloud Drive Container that we are going to use inside info.plist
<key>NSUbiquitousContainers</key>
<dict>
<key>this has to be same as selected containers name inside entitlement</key>
<dict>
@rahulsingh1101
rahulsingh1101 / Core Data CRUD operation
Last active August 23, 2020 09:17
Core Data CRUD operation
To Do CRUD operation in Core Data, we need to do the following things:
1. Get reference of NSPersistentContainer.
eg. lazy var persistentContainer: NSPersistentContainer (inside Appdelegate)
2. Get context from persistentContainer
eg. let context = persistentContainer.viewContext (inside Appdelegate)
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext (inside ViewController)
3. Call fetch, save and delete over this context
@rahulsingh1101
rahulsingh1101 / Extensions.swift
Created August 23, 2020 04:19
Useful Extensions
extension UIView {
func addAutolayoutSubview(_ view: UIView){
view.translatesAutoresizingMaskIntoConstraints = false
addSubview(view)
}
func apply<T:UIView>(_ view: T, completion:((T))->Void){
completion(view)
}
}
@rahulsingh1101
rahulsingh1101 / CustomTableView.swift
Last active August 23, 2020 04:14
Autoresizing content size of table view
class RelatedItemsTableView: UITableView, UITableViewDelegate, UITableViewDataSource {
var items = [Items]() {
didSet{
self.reloadData()
}
}
var headerTitles = [String]() {
didSet {
self.reloadSections(IndexSet.init(arrayLiteral: 0), with: .automatic)