Is it possible to convert instance of a class to an instance of a subclass? - swift

I've recently came across a case where it would be very convenient to convert an instance of a class to a subclass, while the instance has been created within the parent class. But I've never seen such thing. So is there a way to do something like:
class Foo {
var name: String
}
class Bar: Foo {
var friendName: String
}
let foo = Foo(name: "Alice")
foo.toBar(friendName: "Bob")
// foo now of type Bar, as if I'd done
// foo = Bar(name: "Alice", friendName: "Bob")
If that's not possible, is there some reasons this would be impossible from a design perspective?
===edit=== description of a use case where it could make sense
Let say there's two views representing what correspond to the same database record for a book, on is a just a preview of the book and another is a more complex view. Models could be:
protocol BookMetaDelegate {
func onReadStatusUpdate()
}
/// describe a book
class BookMeta {
var delegate: BookMetaDelegate?
private var _hasBeenRead: Bool
var hasBeenRead: Bool {
get {
return _hasBeenRead
}
set {
guard newValue != _hasBeenRead else { return }
_hasBeenRead = newValue
delegate?.onReadStatusUpdate()
}
}
var title: String
}
/// contains all the content of a book
class Book: BookMeta {
var content: BookContent
var lastPageRead: Int
/// some logic that only makes sense in a Book instance
func getLastPageRead() {
return content.getPage(lastPageRead)
}
}
and views could look like:
class BookPreview: UIView, BookMetaDelegate {
var book: BookMeta
init(book: BookMeta) {
book.delegate = self
}
func onReadStatusUpdate() {
print("read status has changed! UI should update")
}
}
class BookView: UIView {
var book: Book
init(book: Book) {
book.hasBeenRead = true
}
}
Then things could happen like
fetch(bookMetaWithId: 123).then { bookMeta in // bookMeta is of type BookMeta
let preview = BookPreview(book: bookMeta)
...
fetch(contentOf: bookMeta).then { content, lastPageRead in
bookMeta.asBook(content: content, lastPageRead: lastPageRead)
let bookView = BookView(book: bookMeta) // doing so will change the hasBeenRead flag and message the instance's delegate, ie the preview
...
}
}

Thinking more about it, it sounds like that if such thing was possible, it'd break things like:
class Foo {
var name: String
}
class Bar: Foo {
var friendName: String
}
class Bla: Foo {
var surname: String
}
func something(foo: Foo) {
foo.toBla(surname: "Will")
}
let bar = Bar(name: "Alice", friendName: "Bob")
something(foo: bar) // what does that do ??? is bar a Bla now ?
so that'd be a good reason for making such casting impossible.

Related

How to access inner object's properties from superclass without boilerplate

Is there a way to create accessors of inner object properties without having boilerplate?
Here's an example of a class with the boilerplate
public class Foo {
internal let bar: Bar
internal init(bar: Bar) {
self.bar = bar
}
private struct Bar: Codable {
let id: Int
let name: String
}
// MARK: - Boilerplate
public var id: Int {
return self.bar.id
}
public var name: String {
return self.bar.name
}
}
// Usage
do {
let bar: Bar = try data.decoded()
let foo = Foo(bar: bar)
print(foo.id)
print(foo.name)
} catch {
...
}
Is there a way to do this without having to write the boilerplate? For larger objects with more properties this could be really helpful
Take Note: The Access Control Modifiers are important
New in Swift 5.1, you can use dynamic member lookup with a key path. This works brilliantly for this sort of "has-a" situation, because key paths maintain full type checking. Here's a simplified example (with no attempt to mirror your actual situation):
struct Dog {
let name : String
}
#dynamicMemberLookup
struct Kennel {
let dog : Dog
subscript(dynamicMember kp:KeyPath<Dog,String>) -> String {
self.dog[keyPath:kp]
}
}
The result is that, given a Kennel k, we can get k.name as a way of getting k.dog.name.
But saying k.xxx would be illegal, because a Dog has no String xxx property; that's what I mean when I say that full type checking is maintained.
The old way would be to use protocols. That way you can use protocol extension to inject the boilerplate.
protocol HasNameAndId {
var id: Int {get}
var name: String {get}
}
protocol WrapperOfHasNameAndId {
associatedtype T : HasNameAndId
var bar: T {get}
}
extension WrapperOfHasNameAndId { // boilerplate
var id: Int {
return self.bar.id
}
var name: String {
return self.bar.name
}
}
// ==============
struct Bar: HasNameAndId {
let id: Int
let name: String
}
class Foo : WrapperOfHasNameAndId {
let bar: Bar
init(bar: Bar) {
self.bar = bar
}
}
// =======
let f = Foo(bar: Bar(id: 1, name: "howdy"))
print(f.id) // 1
print(f.name) // "howdy"

Using an overriden static property during initialization

I would like to create a class with a static property that subclasses can override, which would be used to initialize instances. So far, I've tried to accomplish this like this:
import Cocoa
class A: NSObject {
class var staticProperty: String {
return "A"
}
var property: String = A.staticProperty
}
class B: A {
override class var staticProperty: String {
return "B"
}
}
This does not work, since B().property still returns "A". How could I change this code so that property contains the value specified by the subclass? Any help would be appreciated!
Edit
I would like to initialize property with the value of staticProperty, so this could also look like this:
var property: SomeClass = SomeClass(A.staticProperty)
But then, this initialization should still use "A" for class A, and "B" for class B.
Edit 2 (After #RakeshaShastri's comment)
For my specific use-case, I need property to be stored (so not computed) and non-lazy.
Edit 3
In short, I'm trying to build a Realm model class which has a few to-many relationships to other models. For these models (which are quite similar), I'm trying to create a superclass which contains the shared functionality, amongst which is also the inverse relationship. Therefore, I want to have a static property which contains the key in the first model to either of the other models, and then initialize a LinkingObjects property using this key name. Since Realm does not allow this to be lazy or computed, I cannot use these functionalities here.
If you inherit from NSObject why not using it ?
import Cocoa
class A: NSObject {
var property: String
public override init() {
let str = type(of: self).perform(#selector(getter: type(of: self).staticProperty))?.takeUnretainedValue() as! String
property = str
}
#objc class var staticProperty: String {
return "A"
}
}
class B: A {
override class var staticProperty: String {
return "B"
}
}
You can do this with this aproach
class A {
var prop: String{
return "A"
}
}
class B: A {
override var prop: String{
return "B"
}
}
print(A().prop) // "PRINTS A"
print(B().prop) // "PRINTS B"
A.staticProperty will use static dispatch and will always point to A's class property. You need dynamic dispatch here, aka type(of: self).
However, self needs an instance to work with, thus var property: String = type(of: self.staticProperty won't compile.
However, lazy properties can work around this limitation, so you could declare property as a lazy one:
class A: NSObject {
class var staticProperty: String {
return "A"
}
private(set) lazy var property: String = { type(of: self).staticProperty }()
}
class B: A {
override class var staticProperty: String {
return "B"
}
}
print(B().property) // B
P.S. the private(set) part is just something I usually do, I rarely allow extrinsic factors to change my object.
Update As #MartinR has pointed out, lazy is not a choice for the OP. An alternative solution that doesn't use a lazy var is to use a "shadowing" property:
class A: NSObject {
class var staticProperty: String {
return "A"
}
private var _property: String?
var property: String {
get {
return _property ?? type(of: self).staticProperty
}
set {
_property = newValue
}
}
}
class B: A {
override class var staticProperty: String {
return "B"
}
}
let b = B()
print(b.property) // B
b.property = "B'"
print(b.property) // B'

Get rid off extra output in playground

I am currently learning swift, so I'm working in the swift playground with xcode.
I am working with classes but I get some extra output that it's just kind of distracting for me.
I don't know if I have modify xcode preference or there is something wrong with my code.
//: Playground - noun: a place where people can play
import UIKit
class Person {
var name = ""
}
class BlogPost {
var title:String?
var body = ""
var author:Person!
var numberOfComments = 0
}
let post = BlogPost()
if let actualTitle = post.title {
}
I just want to get rid off the __lldb_expr_114.
Use a protocol oriented approach:
import Foundation
import Swift
protocol PlaygroundFriendlyClass: CustomStringConvertible
{
}
extension PlaygroundFriendlyClass
{
var description: String
{
return String(describing: type(of: self)).components(separatedBy: ".").last!
}
}
class Foo: PlaygroundFriendlyClass
{
init()
{
}
}
class Bar: PlaygroundFriendlyClass
{
init()
{
}
}
Foo() // "Foo"
Bar() // "Bar"
Add the description property:
var description : String {
return "BlogPost \(author) - \(title)"
}

Command Pattern example confusion

I am going through the Head-First Design Pattern book when I came across the "Command Pattern" chapter. I have recreated the example in Playground:
protocol RemoteActions {
func turnOn()
}
protocol Product {
var description: String { get set }
}
struct Light: Product {
var description: String
// some other properties
}
struct Heater: Product {
var description: String
// some other properties
}
class LightOn: RemoteActions {
var light: Light
init(light: Light) {
self.light = light
}
func turnOn() {
print("\(light.description) on")
}
}
class HeaterOn: RemoteActions {
var heater: Heater
init(heater: Heater) {
self.heater = heater
}
func turnOn() {
print("\(heater.description) on")
}
}
class Remote {
func doAction(action: RemoteActions) {
action.turnOn()
}
}
let r = Remote()
let l = Light(description: "light1")
let h = Heater(description: "heater1")
let lo = LightOn(light: l)
let ho = HeaterOn(heater: h)
r.doAction(action: lo)
r.doAction(action: ho)
I mean what are the benefits of this pattern?? Yes, I can see that the remote will only know about its actions, but what if I wanted to create a new Product to be turned on and off? I will undoubtfully have to create a new "Command Class" right? Which makes this part in the book really silly:
Wouldn't it be better if we conformed the action into said Product? Like this:
protocol RemoteActions {
func turnOn()
}
protocol Product: RemoteActions {
var description: String { get set }
func turnOn()
}
struct Light: Product {
var description: String
// some other properties
func turnOn() {
print("\(description) on")
}
}
struct Heater: Product {
var description: String
// some other properties
func turnOn() {
print("\(description) on")
}
}
class Remote {
func doAction(product: Product) {
product.turnOn()
}
}
let r = Remote()
let l = Light(description: "light1")
let h = Heater(description: "heater1")
r.doAction(product: l)
r.doAction(product: h)
gof says: 'The Command pattern lets toolkit objects make requests of unspecified application objects by turning the request itself into an object. This object can be stored and passed around like other objects. The key to this pattern is an abstract Command class, which declares an interface for executing operations. In the simplest form this interface includes an abstract Execute operation. Concrete Command subclasses specify a receiver-action pair by storing the receiver as an instance variable and by implementing Execute to invoke the request. The receiver has the knowledge required to carry out the request.'

Accessing Class Variables by enum

I have following code which basically defines enum for different companies and different methods for each company.
protocol Fetcher {
func getDetail()
static var idRegex:String {get}
}
class FooFetcher:Fetcher {
static var idRegex = "(^\\d{5}$)"
var company = Company.Foo
init (name:String?, id:String) {
println("Name: \(name) id: \(id)")
}
func getDetail() {
println("Foo Get detail")
}
}
class BarFetcher:Fetcher {
static var idRegex = "(^\\d{11}$)"
var company = Company.Bar
init (name:String?, id:String) {
println("Name: \(name) id: \(id)")
}
func getDetail() {
println("Bar Get detail")
}
}
enum Company:Int {
case Foo
case Bar
func fetcher(name:String?, id:String) -> AnyObject {
switch self {
case Foo:
return FooFetcher(name: name, id: id)
case .Bar:
return BarFetcher(name: name, id: id)
}
}
}
var compList = [Company.Foo , Company.Bar]
How can I get idRegex by using compList array only? I don't want to create another array like [FooFetcher.idRegex,BarFetcher.idRegex] because it will bloat the file. I want to use enum because it is easy to store their rawValue with Core Data.
I also wonder how to write better fetcher function for Company enum. Currently I use fetcher function like below
var company = Company.Foo
var strategy = company.fetcher("Some name", id: "Some id") as! Fetcher
strategy.getDetail()
AnyObject does not support Fetcher, in the company.fetcher function instead of returning AnyObject is -> Fetcher
enum Company:Int {
case Foo
case Bar
func fetcher(name:String?, id:String) -> Fetcher {
switch self {
case .Foo:
return FooFetcher(name: name, id: id)
case .Bar:
return BarFetcher(name: name, id: id)
}
}
}
FOO also there corresponds almost .foo
Along with eliasRuizHz's answer, add a new regex method to the Company enum.
func regex() -> String {
switch self {
case .Foo:
return FooFetcher.idRegex
case .Bar:
return BarFetcher.idRegex
}
}
The two responses both provide good contributions. For example, returning a Fetcher rather than AnyObject makes complete sense, as does a regex() function. The thing that I query, however, is whether you could use enums with associated values rather than this enum + class approach, but putting that to one side and answering the question in hand if you really want to directly access the .idRegex from an instance then it needs to be a regular instance variable not a class variable. One way to have it both ways is to return the type variable through an instance variable like so:
protocol Fetcher {
func getDetail()
var idRegex:String {get}
static var regex:String {get}
}
class FooFetcher:Fetcher {
static var regex = "(^\\d{5}$)"
var idRegex:String { return FooFetcher.regex }
var company = Company.Foo
init (name:String?, id:String) {
println("Name: \(name) id: \(id)")
}
func getDetail() {
println("Foo Get detail")
}
}
class BarFetcher:Fetcher {
static var regex = "(^\\d{11}$)"
var idRegex:String { return BarFetcher.regex }
var company = Company.Bar
init (name:String?, id:String) {
println("Name: \(name) id: \(id)")
}
func getDetail() {
println("Bar Get detail")
}
}
enum Company:Int {
case Foo
case Bar
func fetcher(name:String?, id:String) -> Fetcher {
switch self {
case Foo:
return FooFetcher(name: name, id: id)
case .Bar:
return BarFetcher(name: name, id: id)
}
}
}
var compList = [Company.Foo, Company.Bar]
var company = Company.Foo
var strategy = company.fetcher("Some name", id: "Some id")
strategy.getDetail()
strategy.idRegex
Edit Responding to Query
If you want the fetcher to not necessarily initialize an instance then set whatever requirement there is to determine whether a value or nil is returned.
enum Company:Int {
case Foo
case Bar
func fetcher(name:String?, id:String) -> Fetcher? {
switch self {
case Foo:
if FooFetcher.regex == "(^\\d{11}$)" {
return FooFetcher(name: name, id: id)}
else {return nil}
case .Bar:
if BarFetcher.regex == "(^\\d{11}$)" {
return BarFetcher(name: name, id: id)
}
else {return nil}
}
}
}
var compList = [Company.Foo, Company.Bar]
var company = Company.Foo
var strategy = company.fetcher("Some name", id: "Some id")
strategy?.getDetail()
strategy?.idRegex