WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

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?

1      

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
        }

1      

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

1      

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.