For the past few months, I’ve been learning about Swift coding. I’m relatively new to the coding field so this may seem trivial to some.
Here’s what I’m trying to do: I cannot get the data saved from regular textboxes to appear in the table I've designed on the storyboard. It is clearly saving something to the table rows but I cannot see it.
I've looked all over the web but most examples are from old versions of swift/deprecated versions of Xcode and are not applicable.
Basically, I’m designing an app for my company that allows quick and easy saving of users that call; I would be able to save their company, name, phone, userid, etc, the app saves it into the table And allows me to reference it later.
I'm using core data and I’ve attached all of the code to the appropriate storyboard fields.
any insight or errors someone could point out would be very helpful.
Here’s my code:
import Cocoa
import SwiftData
import SwiftUI
class User: NSManagedObject, Identifiable {
let id = UUID() //compatibility
@NSManaged public var company: String
@NSManaged public var name: String
@NSManaged public var phone: String
@NSManaged public var uid: String
@NSManaged public var cid: String
@NSManaged public var tvid: String
@NSManaged public var tvpwd: String
@NSManaged public var notes: String
}
class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
@IBOutlet weak var companyTextField: NSTextField!
@IBOutlet weak var nameTextField: NSTextField!
@IBOutlet weak var phoneTextField: NSTextField!
@IBOutlet weak var uidTextField: NSTextField!
@IBOutlet weak var cidTextField: NSTextField!
@IBOutlet weak var tvidTextField: NSTextField!
@IBOutlet weak var tvpwdTextField: NSTextField!
@IBOutlet weak var notesTextField: NSTextField!
@IBOutlet weak var tableView: NSTableView!
var users = [User]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
@IBAction func saveButtonClicked(_ sender: NSButton) {
let user = User()
users.append(user)
tableView.reloadData()
}
// MARK: - NSTableViewDataSource
func numberOfRows(in tableView: NSTableView) -> Int {
return users.count
}
// MARK: - NSTableViewDelegate
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let user = users[row]
guard let cell = tableView.makeView(withIdentifier: tableColumn!.identifier, owner: self) as? NSTableCellView else { return nil }
switch tableColumn?.identifier.rawValue {
case "company":
cell.textField?.stringValue = user.company
case "name":
cell.textField?.stringValue = user.name
case "phone":
cell.textField?.stringValue = user.phone
case "uid":
cell.textField?.stringValue = user.uid
case "cid":
cell.textField?.stringValue = user.cid
case "tvid":
cell.textField?.stringValue = user.tvid
case "tvpwd":
cell.textField?.stringValue = user.tvpwd
case "notes":
cell.textField?.stringValue = user.notes
default:
return nil
}
return cell
}
}
![]
Post
Replies
Boosts
Views
Activity
This is my first app and I'm very new to coding. I started out with enthusiasm until becoming overwhelmed with Swift coding language...
The app I'm trying to design is very simple and will go hand in hand with the work I do. I attached a screenshot of the UI of what I'm trying to achieve.
All I need the app to do is to let me input the appropriate information, and save it to the table.
The problem is that:
Swift doesn't import source files like C#, whenever I create a new swift file and try to import the file called User.swift, "no file exists named User"
I've looked all over and the examples I'm seeing don't make sense, I dont understand why Xcode/Playground doesn't see it.
Instead of using the UI, I'm writing out:
TextField(Name, text: ???)
I've tried defining a variable in my User.swift file (I can't import which might be the problem).
Even if I could import the User.swift file, I'm not even sure of what would go after the "text:____" spot - I've tried using the $name or changing it to "self.name" and there's always a problem
I realize this may be trivial to some of you but I appreciate your input.
What I'm writing in my User.swift file:
import SwiftUI
struct User {
var name: String
var phone: String
var company: String
var userid: String
var clientid: String
var errorid: String
var notes: String
init(name: String, phone: String, company: String, userid: String, clientid: String, errorid: String, notes: String) {
self.name = name
self.phone = phone
self.company = company
self.userid = userid
self.clientid = clientid
self.errorid = errorid
self.notes = notes
}
}