FREE TRIAL: Accelerate your app development career with 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?

   

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
        }

   

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

   

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for iOS devs who want to become complete senior developers — from October 18th to 24th. Learn how to apply iOS app architecture patterns through a series of lectures and practical coding sessions.

Learn more

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

Reply to this topic…

You need to create an account or log in to reply.

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.