Guys, tell me how to supplement my program so that the user, for example, User1 matches the added asset1, and, for example, User2 matches the asset2. Naturally with preservation in Core Data of the program.
Archive with the program attached at the link below. Thanks in advance for the answer!
Download the archive with the project
User class file:
import Foundation import CoreData class User: NSManagedObject { @NSManaged var name: String? @NSManaged var asset: NSSet? }
AddUser class file:
import UIKit import CoreData class AddUser: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! var users = [User]() override func viewWillAppear(animated: Bool) { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let manageContext = appDelegate.managedObjectContext let fetchRequest = NSFetchRequest(entityName: "User") do { let fetchResult = try manageContext.executeFetchRequest(fetchRequest) users = fetchResult as! [User] } catch { print(error) return } } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return users.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("userCell", forIndexPath: indexPath) cell.textLabel?.text = users[indexPath.row].name return cell } func addFunc(name: String) { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let manageContext = appDelegate.managedObjectContext let entity = NSEntityDescription.entityForName("User", inManagedObjectContext: manageContext) let user = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: manageContext) as! User user.setValue(name, forKey: "name") do { try manageContext.save() } catch { print(error) return } users.append(user) tableView.reloadData() } @IBAction func addUser(sender: AnyObject) { let alert = UIAlertController(title: "Новый сотрудник", message: "Введите ммя сотрудника", preferredStyle: .Alert) let okButton = UIAlertAction(title: "Сохранить", style: .Default, handler: {(action: UIAlertAction) -> Void in let textField = alert.textFields![0] self.addFunc(textField.text!) self.tableView.reloadData() }) alert.addTextFieldWithConfigurationHandler ({(textField: UITextField) -> Void in }) let cancelButton = UIAlertAction(title: "Сохранить", style: .Default, handler: {(action: UIAlertAction) -> Void in }) alert.addAction(okButton) alert.addAction(cancelButton) presentViewController(alert, animated: true, completion: nil) } }
Asset class file:
import Foundation import CoreData class Asset: NSManagedObject { @NSManaged var name: String? @NSManaged var user: User? }
Class file AddAsset:
import UIKit import CoreData class AddAssets: UIViewController { @IBOutlet weak var tableView: UITableView! var assets = [Asset]() override func viewWillAppear(animated: Bool) { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let manageContext = appDelegate.managedObjectContext let fetchRequest = NSFetchRequest(entityName: "Asset") do { let fetchResult = try manageContext.executeFetchRequest(fetchRequest) assets = fetchResult as! [Asset] } catch { print(error) return } } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return assets.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("assetCell", forIndexPath: indexPath) cell.textLabel?.text = assets[indexPath.row].name return cell } func addFunc(name: String) { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let manageContext = appDelegate.managedObjectContext let entity = NSEntityDescription.entityForName("Asset", inManagedObjectContext: manageContext) let asset = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: manageContext) as! Asset asset.setValue(name, forKey: "name") do { try manageContext.save() } catch { print(error) return } assets.append(asset) tableView.reloadData() } @IBAction func addUser(sender: AnyObject) { let alert = UIAlertController(title: "Новое оборудование", message: "Введите имя оборудования", preferredStyle: .Alert) let okButton = UIAlertAction(title: "Сохранить", style: .Default, handler: {(action: UIAlertAction) -> Void in let textField = alert.textFields![0] self.addFunc(textField.text!) self.tableView.reloadData() }) alert.addTextFieldWithConfigurationHandler ({(textField: UITextField) -> Void in }) let cancelButton = UIAlertAction(title: "Сохранить", style: .Default, handler: {(action: UIAlertAction) -> Void in }) alert.addAction(okButton) alert.addAction(cancelButton) presentViewController(alert, animated: true, completion: nil) } }