TableView help for hobbyist…

Sorry for vague title.
Recent retiree, new to Mac (for photo purposes) and Swift. I have long history of coding for personal use, very comfortable with C#.Net.

I have a personal project that began as an Excel VBA workbook and grew into a standalone C# application. I changed to a Macbook and started to try to recreate it in Swift macOS. Then I quickly realized the shortage of current macOS info/tutorials compared to all the iOS stuff. Enough backstory.

I recreated the ‘macOS NSTableView Tutorial’ on this site with no trouble (nice explanations, btw) as well as from another site. My problem is I have a 2D 3 column array from a CSV file that I want to manipulate the data and display it in a table. I have everything fine, just trying to get the array as a data source.

@IBAction func openText(_ sender: Any) {

    var fileOpened: String = ""
    var fileOpened2: String = ""
    
    let openPanel = NSOpenPanel()
    openPanel.allowsMultipleSelection = true
    openPanel.canChooseDirectories = false
    openPanel.canChooseFiles = true
    
    if (openPanel.runModal() ==  NSApplication.ModalResponse.OK) {

        // Results contains an array with all the selected paths
        let results = openPanel.urls
        
        let myFile_a: String = results[0].path
        let myFile_b: String = results[1].path

    do {
        fileOpened = try String(contentsOfFile: String("\(results[0].path)"))
        fileOpened2 = try String(contentsOfFile: String("\(results[1].path)"))

    } catch {
        print("Error file couldn't be found")
    }
        
        var wholeArr = fileToArr(name:fileOpened)
        var wholeArr2 = fileToArr(name:fileOpened2)

        print("wholeArr: ",wholeArr[0][1])
        print("wholeArr: ",wholeArr[1][1])

        print("wholeArr2: ",wholeArr2[0][1])
        print("wholeArr2: ",wholeArr2[1][1])
    } else {
        // "Canceled"
        return    

}

I have these in ViewDidLoad() as they’ve been there in every demo I’ve seen:
tableView.delegate = self
tableView.dataSource = self

This code (it’s a sloppy first draft, recreating the concept) only shows the arrays wholeArr and wholeArr2 that are returned from a function that reduces a 6 column row (with blank items) to 3. There are two because I’ll join two similar arrays. The print is there for debugging.

I tried to figure out how using the above mentioned tutorial, as well as several online docs. Apple makes great hardware, documentation isn’t their strong suit, unless you have a background in OS-X code.

Thanks in advance. Dan

Hi Dan (@djcolom),
Hope you are enjoying your journey transitioning to a Mac and Swift.

Let’s look at your issues and split them into 2 portions,

  1. Retrieving Data (File Operations)
  2. Displaying Data (UI)

From what I understand from your post above, you have resolved point #2, the UI part.

For 1, Here’s something for you to look at, Apple introduced a new API/Framework called TabularData. You might want to look at that, it abstracts and saves you from a lot of file operations and allows for a lot of fine grained operations with the csv file.

Hope that help and resolves your challenge. I had a post on using it on my personal site, I will confirm if I can post that link here or not.

cheers,

Jayant

Hi, Jayant. Thanks for your reply.
TabularData looks interesting. I believe I could have a use for it coupled with a DataFrame as I begin to introduce feature creep in an otherwise functioning application.
Currently, I’ve dug up some info using Cocoa bindings for my current project. While I’d prefer to spend my time learning technology from this decade… I’m a glutton for punishment. Unfortunately, I’m limited, I have no interested in iOS apps (NOT a knock on the many, many, who do). My coding workflow would totally insult anyone developing for a living. But, at 68, this is only pushups for my brain.

Hi Dan,
It is really impressive and a very good way to keep yourself engaged. The beauty in developement is that there is no 1 way to do things, and that is what leads to innovations.

If this is more so for a hobby, then I would suggest you watch some of the WWDC 2022 videos, Apple has made a lot of things so much easier now that it is not funny, it almost feels like SwiftUI could be what VB was in the 90’s. You can build for iOS, iPadOS, macOS, tvOS, watchOS all from the same codebase or with minimal changes.

and yes you are correct, Apple’s documentation is a rip off the source code, there is little to none in terms of sample code and how a particular API or function works.

Back to your question, If you are stuck at a particular point in code, please do feel free to ask. Would love to see how to progress and what you create at the end of this.

Cheers,

Jayant

Will do, Jayant. I appreciate you taking the time to present more than a one sentence answer, as often seen on another, otherwise helpful site.
I have one book, an intro to SwiftUI that looks interesting. While it’s approach seems not all that much different, I’m going to complete my current project first.
Thanks.
Dan

1 Like