Initializing model data for use in TableViewController

Hi

I think I am having a little problem with how to have data from model ready at run time to use in my TableViewController.

TeamModel

class TeamModel {

// a type of team
typealias Team = (String, Int, Int, Int)

// current list of teams
var teamList : [Team] = [("Manchester", 2, 40, 11)]

// adds a new team to the user's list of teams
func addTeamToDB(newTeam: Team) {
    
   }
}

ViewController

class TeamViewController: UITableViewController {

// reference to team Model
let teamModelPointer = TeamModel()
let teams = teamModelPointer.teamList

Getting the error: Instance member TeamModelPointer cannot be used on type TeamViewController

Eventually this will have data in Core Data, so that’s why I placed it in my model, I just wanted to see it work with arrays of Tuples initially. If I created the Array of Tuples in the controller, code works fine, which is why I think it’s a timing issue??

Any help would be appreciated

I can’t find the problem. I do see there is no “TeamModelPointer” in what you have pasted though - only a “teamModelPointer”. Where exactly does the error occur and have you pasted everything accurately? Is there perhaps a “TeamModelPointer” somewhere in the view controller, maybe you have tried to define an instance variable of that type?

I’m not sure either. I just adjusted it to this:

class TeamViewController: UITableViewController {

// reference to team Model
let teams = TeamModel.teamList

Just so you know my mind set, I’m trying to set the let of teams using the TeamModel class I have and the variable teamList I have defined inside of the TeamModel as noted in the code. IS that the correct way to call a model and property from a controller?

The error I get now is: instance member teamList cannot be used on type 'TeamModel'

I fixed it. It should be let teams = TeamModel().teamList

I didn’t instantiate it.

Thanks for your help :slight_smile:

You can also use the concept of “Singleton” here to access data from the model . Create a “sharedInstance” object of the model class and you get access to methods and variables .
But i am not sure whether its the best approach , but surely that works .