This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
2
Overriding methods with different parameter types in Swift
Post Flair (click to view more posts with a particular flair)
Post Body
Hey. I have only recently started looking at Swift and got to an issue I have previously solved in Objective C, but not in Swift.
The gist is that I want to declare a base class, then have many subclasses override a central method but with their own data types. For example:
class BaseCell {
func setupCell(data: Any) {
// Use subclasses
}
}
Then a subclass that sets the cell up with a string could be
class TextCell: BaseCell {
override func setupCell(text: String) {
label.text = text
}
}
And another could have an integer
class NumberCell: BaseCell {
override func setupCell(number: Int) {
label.text = number as String
}
}
The way I then use them are like this (psuedo code)
// Data setup
// cellType, cellData
cells = NumberCell, 21
NumberCell, 5
TextCell, "text"
NumberCell, 12
// TableView cellForIndexPath
cell = getCellOfType(cellType)
cell.setupCell(cellData)
But right now as I cannot override the setupCell, I have to do this
override func setupCell(data: Any?) {
labelVersion.text = data as? String
}
Any pointers?
Author
Account Strength
100%
Account Age
13 years
Verified Email
Yes
Verified Flair
No
Total Karma
74,719
Link Karma
8,619
Comment Karma
65,534
Profile updated: 3 days ago
Posts updated: 10 months ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 6 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/iOSProgramm...