Hello I have the following piece of code to display data in NSTextView. I want to have a new line after each record. Need help to set the new line character so Swift will not concatenate each time a new record is inserted into the TextView, on line 28.
Any help will be appreciated.
{By the way, I've spent weeks trying to use the NSTableView without success. This method is simple and it gives me what I need).
@IBActionfunc showStudents(_ sender: NSButton) {
guard (NSApplication.shared.delegate as? AppDelegate) != nil else {
return
}
_ = (NSApplication.shared.delegate as!
AppDelegate).persistentContainer.viewContext
_ = NSFetchRequest(entityName: "Registration")
_ = NSSortDescriptor(key: "lastName", ascending: true)
let fetchRequest: NSFetchRequest = Registration.fetchRequest()
do {
let items = try managedObjectContext.fetch(fetchRequest)
for item in items {
if let record0Item = item.value(forKey: "lastName") as? String
{
print ("Record Item is \(record0Item)")
lastNameItem.append(record0Item)
print ("Last Name Item is \(lastNameItem)")
if let record1Item = item.value(forKey: "firstName") as? String
{
print ("Record Item is \(record1Item)")
firstNameItem.append(record1Item)
print ("First Name Item is \(firstNameItem)")
if let record2Item = item.value(forKey: "middleName") as? String
{
print ("Record Item is \(record2Item)")
middleNameItem.append(record2Item)
studentsText?.string = ((lastNameItem) + (" ") + (firstNameItem) + (" ") + (middleNameItem))
print ("Middle Name Item is \(middleNameItem)")
}
}
}
}
}
}
Your title speaks about tableView, but the message is about TextView. What is the point ?
I interpreted as a question about the TextView. Is it ?
If so, what you do now is that each new record overwrites the existing one. Exact ?
You need to change 2 things:
- write record in addition to existing ones
- insert a linefeed.
Could change
studentsText?.string = ((lastNameItem) + (" ") + (firstNameItem) + (" ") + (middleNameItem))
into:
let newLine = ((lastNameItem) + (" ") + (firstNameItem) + (" ") + (middleNameItem))
studentsText?.string = studentsText?.string! + newLine + "\n"
That will add an extra linefeed at the end of text, but that should not be a problem.
If it is, need a bit more complex logic:
var newLine = ((lastNameItem) + (" ") + (firstNameItem) + (" ") + (middleNameItem))
if studentsText?.string! != "" { newLine = "\n" + newLine
studentsText?.string = studentsText?.string! + newLine
Hope I understaood what you want to do.