NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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 RevenueCat

SPONSORED Building and maintaining in-app subscription infrastructure is hard. Luckily there's a better way. With RevenueCat, you can implement subscriptions for your app in hours, not months, so you can get back to building your app.

Try it for free

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.