Reputation: 17
struct StoryBrain {
var storyNumber = 0
let Allstory = [
Story(s: "There is a fork on the road", c1: "Take a left", c2: "Take a right"),
Story(s: "There are two door", c1: "Enter the left door", c2: "Enter the Right door"),
Story(s: "You can have either a dog or a cat", c1: "I will have a dog", c2: "I will have a cat")
]
func getStory() ->String {
return Allstory[storyNumber].mainStory
}
func getchoice1() -> String {
return Allstory[storyNumber].choice1
}
func getchoice2() -> String {
return Allstory[storyNumber].choice2
}
This is Model
in view controller, I want to make updateUI function
func updateUI() {
storyLabel.text = StoryBrain.getStory(self: StoryBrain)
I do not understand why I am getting (self: StoryBrain) instead of just ().
Upvotes: 0
Views: 60
Reputation: 14397
These are not static or class methods ... you need to create object of that class to call its functions sir ... or make them static/class functions if you dont want to create object of that class
Either do
func updateUI() {
let story = StoryBrain()
storyLabel.text = story.getStory()
}
Or you can make them static to use but thats not a good approach
struct StoryBrain {
static var storyNumber = 0
static let Allstory = [
Story(s: "There is a fork on the road", c1: "Take a left", c2: "Take a right"),
Story(s: "There are two door", c1: "Enter the left door", c2: "Enter the Right door"),
Story(s: "You can have either a dog or a cat", c1: "I will have a dog", c2: "I will have a cat")
]
static func getStory() ->String {
return Allstory[storyNumber].mainStory
}
static func getchoice1() -> String {
return Allstory[storyNumber].choice1
}
static func getchoice2() -> String {
return Allstory[storyNumber].choice2
}
Now you can do
func updateUI() {
storyLabel.text = StoryBrain.getStory()
}
Upvotes: 0
Reputation: 54706
You declaring getStory
as an instance method. So you need to call it on an instance of StoryBrain
, that is what the compiler is trying to tell you with self
there.
let storyBrain = StoryBrain() // create an instance
storyLabel.text = storyBrain.getStory() // call the instance method on that instance
Upvotes: 0