UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Hacking with macOS, project 1, SourceViewController.swift errors

Forums > Books

Hi! Got another newbie question. In project 1 of Hacking with macOS, I've added NSTableViewDataSource, NSTableViewDelegate to the class SourceViewController and “func numberOfRows(in tableView: NSTableView) -> Int { return 100 }" to SourceViewController below the existing viewDidLoad():. The book says you can run the program again and not see any errors, but when I do I get the following errors: 2020-11-27 09:02:27.588717-0600 Project1[56452:19787066] *** Illegal NSTableView data source (<Project1.SourceViewController: 0x600000798e00>). Must implement numberOfRowsInTableView: and tableView:objectValueForTableColumn:row:

Also, when I run it I don't see "Table View Cell" in 100 rows. Just a blank gray window.

Here's my complete code:

import Cocoa

class SourceViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
@IBOutlet var tableView: NSTableView!

    override func viewDidLoad() {
        func numberOfRows(in tableView: NSTableView) -> Int {
            return 100
        }
        super.viewDidLoad()
        // Do view setup here.
    }

}

Have I done something wrong?

3      

Your function is nested inside another function (viewDidLoad). You need to move it outside:

override func viewDidLoad() {

        super.viewDidLoad()
        // Do view setup here.
    }

     func numberOfRows(in tableView: NSTableView) -> Int {
            return 100
        }

3      

that did it! Thank you so much, @nemecek-filip

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.