import Foundation
import CoreData
class CoreDataStack {
var container: NSPersistentContainer {
let container = NSPersistentContainer(name: "todos")
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
container.loadPersistentStores { (description, error) in
guard error == nil else {
print("Error: \(error!)")
return
}
}
return container
}
var managedContext: NSManagedObjectContext {
return container.viewContext
}
}
If embedded in Navigation Controller:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
if let rootViewController = window?.rootViewController as? UINavigationController {
if let viewController = rootViewController.viewControllers.first as? ViewController {
let url = alert["imgurl"] as? NSString
viewController.loadImage(url as String)
}
}
}
If not embedded in Navigation Controller:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
if let rootViewController = window?.rootViewController as? ViewController {
rootViewController.loadImage()
}
}
let alert = UIAlertController(title: "Title", message: nil, preferredStyle: .alert)
alert.addTextField { (textField) in
textField.placeholder = "Name"
}
let action = UIAlertAction(title: "Save", style: .default) { (_) in
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action: UIAlertAction) in
}
alert.addAction(action)
alert.addAction(cancel)
present(alert, animated: true, completion: nil)
In an alert view:
let action = UIAlertAction(title: "Update", style: .default) { (_) in
let name = alert.textFields!.first!.text!
let age = alert.textFields!.last!.text!
selectedItem.setValue(name, forKey: "name")
selectedItem.setValue(Int16(age), forKey: "age")
do{
try persistenceService.context.save()
} catch {
print("Error saving")
}
self.tableView.reloadData()
}
In a tableview:
let itemToDelete = people[indexPath.row]
print(itemToDelete)
if (editingStyle == UITableViewCellEditingStyle.delete) {
// handle delete (by removing the data from your array and updating the tableview)
persistenceService.context.delete(itemToDelete)
self.people.remove(at: indexPath.row)
do {
try persistenceService.context.save()
} catch let error as NSError {
print("Error While Deleting: \(error.userInfo)")
}
}
In an alert view:
let action = UIAlertAction(title: "Save", style: .default) { (_) in
let name = alert.textFields!.first!.text!
let age = alert.textFields!.last!.text!
// fill container
let person = Person(context: persistenceService.context)
person.name = name
person.age = Int16(age)!
// save container
persistenceService.saveContext()
self.people.append(person)
self.tableView.reloadData()
}
Auf dem Git Server:
mkdir repository.git
cd repository.git
git init --bare
In Xcode9:
Rechtsklick -> Add Existing Remote
Source Control -> Push
Pfad nach folgendem Muster: user@192.168.1.2:/home/git/repository.git
Works with a Dictionary like this:
["Key":["value1", "value2"]]
let array = DICTIONARY.values.reduce([], +)
//Remove Empty Keys
for (key, value) in DICTIONARY{
if value == [] {
print("Key: ", key)
self.defaultDevicesSections.removeValue(forKey: key)
}
}
Sollte optimalerweise in der AppDelegate unter "didFinishLaunchingWithOptions" untergebracht werden:
let isFirstLaunch = UserDefaults.isFirstLaunch()
if isFirstLaunch == true {
print("firstStart")
} else {
print("notfirstStart")
}