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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.