Generic class of generic class with one argument/parameter - ios

I have this syntactical issue with my data structure. What makes it worse is that I can not talk about it in detail. The company I am employed at operates in the public transportation sector and I am under NDA and boss would kill me if I posted anything too specific. I hope you understand!
I have this perfect example though. There are no inconsistencies at all ;)
Well, okay, there are. However I am convinced that most of you out there are smart enough to get what is of importance here.
Basic structure:
class Propulsion {
var horsePower: Double
init(horsePower: Double) {
self.horsePower = horsePower
}
static let pedes = Propulsion(horsePower: 0.2)
}
class Motor: Propulsion {
var range: Double
init(range: Double, horsePower: Double) {
self.range = range
super.init(horsePower: horsePower)
}
static let otto = Motor(range: 1000, horsePower: 100)
static let electric = Motor(range: 400, horsePower: 200)
}
class Vehicle<P: Propulsion> {
var propulsion: P
init(propulsion: P) {
self.propulsion = propulsion
}
}
class Bicycle<P: Propulsion>: Vehicle<P> {
var hasFrontSuspension: Bool
init(hasFrontSuspension: Bool, propulsion: P) {
self.hasFrontSuspension = hasFrontSuspension
super.init(propulsion: propulsion)
}
}
class Car<P: Propulsion>: Vehicle<P> {
func rangePerHorsePower() -> Double where P: Motor {
propulsion.range / propulsion.horsePower
}
}
Now I would like to declare a parking spot for a car. Like so:
var carParkingSpot: ParkingSpot<Car<Motor>>
For the class ParkingSpot I have some class like this in mind:
class ParkingSpot<V: Vehicle<P>> where P: Propulsion {
var vehicle: Vehicle<P>
init(vehicle: Vehicle<P>) {
self.vehicle = vehicle
}
func taxForRange() -> Double where P: Motor {
vehicle.propulsion.range * 50
}
}
From the last bit I get back a bunch of
Cannot find type 'P' in scope
This one doesn’t work either:
class ParkingSpot<V: Vehicle<P: Propulsion>>
Expected '>' to complete generic argument list
This implementation works though:
class ParkingSpot<V: Vehicle<P>, P: Propulsion> {
var vehicle: Vehicle<P>
init(vehicle: Vehicle<P>) {
self.vehicle = vehicle
}
func taxForRange() -> Double where P: Motor {
vehicle.propulsion.range * 50
}
}
However I don’t want to duplicate the Motor bit:
var carParkingSpot: ParkingSpot<Car<Motor>, Motor>
How can I accomplish this with just one generic parameter?

You may use the "Protocol oriented" approach:
protocol PropulsionP {
var horsePower: Double { get }
}
protocol MotorP: PropulsionP {
var range: Double { get }
}
struct MotorS: MotorP {
var range: Double
var horsePower: Double
init(range: Double, horsePower: Double) {
self.range = range
self.horsePower = horsePower
}
}
protocol VehicleP {
associatedtype P: PropulsionP
var propulsion: P { get }
}
struct BicycleS<Prop: PropulsionP>: VehicleP {
let hasFrontSuspension: Bool
var propulsion: Prop
init(
hasFrontSuspension: Bool,
propulsion: Prop
) {
self.hasFrontSuspension = hasFrontSuspension
self.propulsion = propulsion
}
}
struct CarS<Prop: PropulsionP>: VehicleP {
var propulsion: Prop
func rangePerHorsePower() -> Double where P: MotorP {
propulsion.range / propulsion.horsePower
}
}
struct ParkingSpotS<V: VehicleP> {
var vehicle: V
init(vehicle: V) {
self.vehicle = vehicle
}
func taxForRange() -> Double where V.P: MotorP {
vehicle.propulsion.range * 50
}
}
var carParkingSpot: ParkingSpotS<CarS<MotorS>>
No double MotorS bit.
Quod erat demonstrandum.
I used the somewhat unusual naming to emphasize the point.
(needed to make some edit, erronously typed Motor where I actually need MotorP)
Update
I was on the road with my preferred car and tried it out:
var carParkingSpot: ParkingSpotS<CarS<MotorS>> = .init(
vehicle: .init(
propulsion: .init(
range: 760,
horsePower: 240
)
)
)
print(carParkingSpot.taxForRange())
38000.0
Alternatively you can use this initialiser:
var carParkingSpot: ParkingSpotS = .init(
vehicle: CarS(
propulsion: MotorS(
range: 760,
horsePower: 240
)
)
)
Update
Now suppose you are utilising a third party library which already provides a nice implementation of a motor.
What you need to do is to implement an extension for their given class or struct TheirMotor which conforms to your protocol MotorP:
import FancyMotors
extension TheirMotor: MotorP {
let range: Double {
// calculate `range` in terms of the
// given API of `TheirMotor`:
...
return range
}
}
Then, you can use it like below:
var carParkingSpot: ParkingSpotS = .init(
vehicle: CarS(
propulsion: TheirMotor(
distance: 760,
power: 240
)
)
)
Note, that you use TheirMotor and need to use the appropriate initialiser to create it.

This seems to work:
class ParkingSpot<V: Vehicle<Propulsion>>
{
var vehicle: V
init(vehicle: V)
{
self.vehicle = vehicle
}
func taxForEngineeNoise() -> Double
{
switch vehicle.propulsion
{
case is Motor:
return vehicle.propulsion.horsePower * 50
default:
...
}
}
func taxForRange() -> Double
{
if let motor = vehicle.propulsion as? Motor
{
return motor.range * 50
}
else
{
...
}
}
}
Alternatively, perhaps hide the duplication where you can?
typealias ParkingSpotX = ParkingSpot<Car<Motor>, Motor>
var parkingSpot: ParkingSpotX

Related

Does swift type inference not work with function return types?

Does swift type inference not work with function return types?
protocol Vehicle {
func numberOfWheels() -> Int
}
struct Car: Vehicle {
func numberOfWheels() -> Int {
return 4
}
}
struct Bike: Vehicle {
func numberOfWheels() -> Int {
return 2
}
}
struct Truck: Vehicle {
func numberOfWheels() -> Int {
return 8
}
}
struct VehicleFactory {
static func getVehicle<T: Vehicle>(_ vehicleType: T.Type = T.self) -> T? {
let id = identifier(for: T.self)
switch id {
case "Car":
return Car() as? T
case "Bike":
return Bike() as? T
default:
return nil
}
}
private static func identifier(for type: Any.Type) -> String {
String(describing: type)
}
}
let v: Bike = VehicleFactory.getVehicle() // ERROR HERE: Cannot convert value of type 'T?' to specified type 'Bike'
print(v.numberOfWheels())
I am trying this in playground. Why is there an error in above line?
Shouldnt the compiler infer the type to be Bike from the let v: Bike declaration?
The problem is that getVehicle returns an optional, you have to declare
let v: Bike? = VehicleFactory.getVehicle()
Further you have to unwrap v in the print line
Not a direct answer to your question. Vadian has already answered but a few notes on your implementation:
(_ vehicleType: T.Type = T.self) is pointless. You can just omit it.
Second I would simply add init() to your protocol requirements, get rid of the identifier method, change number of wheels to a computed property:
protocol Vehicle {
init()
var numberOfWheels: Int { get }
}
struct Car: Vehicle {
let numberOfWheels = 4
}
struct Bike: Vehicle {
let numberOfWheels = 2
}
struct Truck: Vehicle {
let numberOfWheels = 8
}
struct VehicleFactory {
static func getVehicle<T: Vehicle>() -> T { .init() }
}
let v: Bike = VehicleFactory.getVehicle()
print(v.numberOfWheels) // "2\n"

How can I write a simple example on how to use delegates in Swift 5

I want to write a simple example on how delegates work in Swift. I've set up a class, a protocol and another class which adheres to the protocol. However I'm getting on two different places the same error. I've created a simple swift command line tool in xcode and all my code is inside main.swift. The code has no real functionality except learning how delegates work.
Here are the error messages:
Consecutive declarations on a line must be separated by ';'
Insert ';'
Expected '(' in argument list of function declaration
Expected '{' in body of function declaration
Expected 'func' keyword in instance method declaration
Insert 'func '
Expected declaration
Invalid redeclaration of 'delegate()' //or anything() in the other error
Here is the code:
class MainClass {
var delegate: MyProtocol? = nil
delegate.doAnything() //getting 1st error here
}
protocol MyProtocol {
func doAnything()
}
class OtherClass: MyProtocol {
let anything = MainClass()
anything?.delegate = self //getting 2nd error here
func doAnything() {
print("text")
}
}
So the first error is because you are trying to call the delegate function, outside of anywhere it can be executed. You need to create a function that calls that function, or call it in init. When making examples, try using real world concepts to model your example. You could do something like a Conductor class and a Train Class. The conductor could implement some control protocol that controls the speed of the train.
Anyway, your second error is because self has not yet been initialized. to assign a variable to self, you must initialize the class first, so you could do
init() {
anything?.delegate = self
}
Feel free to DM to understand this concept more, i'll post a full example here in a bit.
EDIT: FULL EXAMPLE, feel free to ask questions
import Foundation
enum Direction {
case north
case east
case south
case west
}
protocol VehicleControls {
var speed: Float {get set}
var direction: Direction {get set}
var numPassengers: Int {get}
func change(newSpeed: Float)
func change(newDirection: Direction)
func createNoise()
}
class Conductor {
var vehicle: VehicleControls
init() {
vehicle = Train(s: 1.5, d: .west, nP: 50)
}
func controlVehicle() {
vehicle.change(newSpeed: 2.5)
vehicle.change(newDirection: .east)
vehicle.createNoise()
print("\n")
}
}
class Train: VehicleControls {
var speed: Float
var direction: Direction
var numPassengers: Int
init() {
self.speed = 0
self.direction = .north
self.numPassengers = 0
}
init(s: Float, d: Direction, nP: Int) {
self.speed = s
self.direction = d
self.numPassengers = nP
}
func change(newSpeed: Float) {
print("changing speed from \(speed), to \(newSpeed)")
self.speed = newSpeed
}
func change(newDirection: Direction) {
print("changing direction from \(direction) to \(newDirection)")
self.direction = newDirection
}
func createNoise() {
print("Chugga, Chugga... Chugga, Chugga... CHOO CHOO")
}
}
class Car: VehicleControls {
var speed: Float
var direction: Direction
var numPassengers: Int
init() {
self.speed = 0
self.direction = .north
self.numPassengers = 0
}
init(s: Float, d: Direction, nP: Int) {
self.speed = s
self.direction = d
self.numPassengers = nP
}
func change(newSpeed: Float) {
print("changing speed from \(speed), to \(newSpeed)")
self.speed = newSpeed
}
func change(newDirection: Direction) {
print("changing direction from \(direction) to \(newDirection)")
self.direction = newDirection
}
func createNoise() {
print("HONK HONK, BEEP BEEP")
}
}
let newConductor = Conductor()
newConductor.controlVehicle()
newConductor.vehicle = Car(s: 60.56, d: .north, nP: 2)
newConductor.controlVehicle()
edit your code in playground file:
protocol MyProtocol {
func doAnything()
}
class MainClass {
var delegate: MyProtocol? = nil
func callMeForGetCallBackWithDelegate() {
// send call back inside any function after call or inside in init
if let delegate = self.delegate {
delegate.doAnything()
}
}
}
class OtherClass: MyProtocol {
let anything = MainClass()
init() {
self.anything.delegate = self // set delegate in init or set inside any function
self.anything.callMeForGetCallBackWithDelegate()
}
func doAnything() {
print("text")
}
}
let otherClass = OtherClass()

Protocol Oriented Programming Example not working in swift 3.0

I have the following piece of code where am trying to implement my Zoo example using Structs and Protocols.
In the example, I have a Zoo where there is certain type of animals which have a certain type of characteristics. Details of the question are in the Readme file of repo (https://github.com/harsh62/Melbournce-Zoo/blob/master/README.md)
I want to understand whether what am trying to do is the correct approach or not? The following piece of code compiles without any errors but doesn't work.
Can somebody help, please?
enum Sex {
case Male
case Female
case Other
}
enum Size:Int {
case ExtraSmall = 0
case Small = 1
case Medium = 2
case Large = 3
case ExtraLarge = 4
}
enum Diet {
case Herbivore
case Carnivore
}
protocol Animal {
var sex: Sex { get set }
var size: Size { get set }
var diet: Diet { get set }
var weight: Double { get set }
var age: Int { get set }
}
protocol Swimmable {
var swimSpeed: Double { set get }
}
protocol NeedsEclosure {
func enclosureSize() -> Double
}
protocol Carnivorous {
func meatSize() -> Double
}
protocol Mammal { }
protocol Fish {
var adjustedSpeed: Double { set get }
}
protocol Bird { }
protocol Reptile { }
protocol Invertebrate { }
/////Extensions to protocols
extension Animal {
var weight: Double {
set {
self.weight = newValue
}
get {
return weight
}
}
var age: Int{
set {
self.age = newValue
}
get {
return age
}
}
var size: Size{
set {
self.size = newValue
}
get {
return size
}
}
var diet: Diet{
set {
self.diet = newValue
}
get {
return diet
}
}
var sex: Sex{
set {
self.sex = newValue
}
get {
return sex
}
}
}
extension Swimmable {
var swimSpeed: Double{
set {
self.swimSpeed = newValue
}
get {
return swimSpeed
}
}
}
extension NeedsEclosure where Self: Animal {
func enclosureSize() -> Double {
return Double(size.rawValue) * weight
}
}
extension Carnivorous where Self: Animal {
func meatSize() -> Double {
return Double(size.rawValue) * weight
}
}
extension Swimmable where Self: Animal {
var adjustedSpeed: Double { return (swimSpeed/weight)/Double(age) }
}
/////////////////////////////////
typealias BigCat = Animal & Mammal & Swimmable & Carnivorous & NeedsEclosure
struct Tiger: BigCat { }
var tiger = Tiger()
tiger.enclosureSize()
Your extension of Animal where you define all of those computed properties makes no sense. You need stored properties. It makes no sense for a computed getter, weight for example, to return weight. Likewise, the computed setter, self.weight = weight doesn't make sense.
You need stored properties to store all of these values, and stored properties cannot be implemented in a protocol extension. Consider your protocol for Animal:
protocol Animal {
var sex: Sex { get set }
var size: Size { get set }
var diet: Diet { get set }
var weight: Double { get set }
var age: Int { get set }
}
You really need to hold all these values somewhere, you can't use computed properties defined in a protocol extension. (So get rid of that extension for Animal that defines those invalid computed properties.) You actually want to implement these stored properties inside the type that conforms to this protocol, e.g.:
typealias BigCat = Animal & Mammal & Swimmable & Carnivorous & NeedsEnclosure
struct Tiger: BigCat {
var swimSpeed = 2.0
var size = Size.large
var sex = Sex.male
var diet = Diet.carnivore
var weight = 400.0
var age = 4
}
(By the way, I changed the values of those various enum types to use lowercase letter, as is the convention in Swift 3.)
Your protocol extensions can provide default implementations for methods and computed properties, but not for stored properties.

Swift 2 Correct way to get level of any object game, protocol? struct? extension?

This is my first game, and I'm new on swift and sprite kit.
I must have a level for each class that needs get level. Like car lev1 car lev 2 etc. I have read about protocol extension etc, witch is the best way to approach level management?
I have tried to use LevelTraker as extension of this protocol:
protocol LevelTracker {
typealias TypeUnit: TypeGame
var nameClass: String! {get set}
var currentLevel : Int {get set}
mutating func levelIncreases()
}
but with extension, i must write 3 var each class that needs level.
i try the same extension LevelTraker with struct LevelTraker:
func getClassName (theClass:AnyObject) -> String {
let name = _stdlib_getDemangledTypeName(theClass); return name}
protocol TypeGame {}
enum transportType : TypeGame {
case ground, sea, air
}
struct LevelTracker {
var sender: AnyObject
var TypeUnit: TypeGame
private func getSaveFileWhitName() -> String {
let saveWithName = getClassName(sender) + "." + String(TypeUnit)
return saveWithName
}
var currentLevel : Int {
get {
let stringName = getSaveFileWhitName()
let returnValue : Int = dataBase.read(stringName) as? Int ?? 1 //Check for first run of app, if = nil, set = 1
return returnValue
}
set (newValue) {
let stringName = getSaveFileWhitName()
let level : Int = self.currentLevel
let val = newValue
if (newValue > level) {dataBase.write(val, key: stringName)}
}
}
mutating func levelIncreases() {self.currentLevel++}
///SERVE SOLO PER SVILUPPO
mutating func RESETLEVEL() {dataBase.write(1, key: getSaveFileWhitName())}
}
To use: (thanks #Krzak)
class car {
init () {
let level = LevelTracker(sender: self, TypeUnit: transportType.ground).currentLevel
}
}
But I don't want modify all init object that use level, and the super super class in common, some class don't have propriety level.
The reason why you have compiler error is in your last line. You're missing the .ground
I'm not sure how you're thinking though that this will work, shouldn't it be var?
var level = LevelTracker(sender: self, TypeUnit: transportType.ground).currentLevel
What I am reading it sounds like you are doing this:
class Level : AnyObject
{
private func getSaveFileWhitName() -> String {
let saveWithName = getClassName(sender) + "." + String(TypeUnit)
return saveWithName
}
var currentLevel : Int {
get {
let stringName = getSaveFileWhitName()
let returnValue : Int = dataBase.read(stringName) as? Int ?? 1 //Check for first run of app, if = nil, set = 1
return returnValue
}
set (newValue) {
let stringName = getSaveFileWhitName()
let level : Int = self.currentLevel
let val = newValue
if (newValue > level) {dataBase.write(val, key: stringName)}
}
}
mutating func levelIncreases() {self.currentLevel++}
///SERVE SOLO PER SVILUPPO
mutating func RESETLEVEL() {dataBase.write(1, key: getSaveFileWhitName())}
}
class car : Level
{
init () {
let level = self.currentLevel
}
}
I found a solution, I'm happy to have some comment.
protocol TypeGame {}
enum transportType : TypeGame {
case car, bus, trak
}
protocol LevelTracker {
var nameClass: String! {get}
var currentLevel : Int {get set}
mutating func levelIncreases()
}
extension LevelTracker {
var currentLevel : Int {
get {/*set to DB*/ return 1}
set (newValue) {/*set to DB*/}
}
mutating func levelIncreases() {self.currentLevel++}}
A protocol only for transport object:
protocol Transport : LevelTracker {}
Ok, now my (simplified) class are:
class AllNode {//SKSpriteNode
init(){}
}
class TransportGame:AllNode, Transport {
var nameClass : String! = "Transport"
override init() {
super.init()
self.nameClass = nameClass + "." + getClassName(self)}
}
class Car : TransportGame {}
class miniCar : Car {}
class Bus: TransportGame {}
class Tree: AllNode {}
var carOne = Car()
let levelCar = carOne.currentLevel
var busOne = Bus()
let levelBue = busOne.currentLevel
var treeOne = Tree()
tree.currentLevel //ERROR YUPPI!!!! :)
Now the tree class can't access to level!
What do you think about this solution?

Swift Protocol Conformance

Here is an example of a set of value relationships that I am toying around with.
protocol Configurable {
func configure(data: Any?) -> Void
}
class RelatedObject {
var x: String = ""
var y: String = ""
}
class Example {
var a: String = ""
var b: String = ""
var c: String = ""
}
extension Example: Configurable {
func configure(data: Any?) //I want the parameter to be of type RelatedObject?, not Any?
{
if let d = data as? RelatedObject { //I don't want to have to do this every time i implement Configurable on an object.
//do stuff
a = d.x
b = d.y
c = d.x + d.y
}
}
}
Is there a way for my classes that implement the Configurable protocol to be able to restrict the specific type of object they accept within the function signature?
I feel like Swift would/could/should have a way avoid a situation where I have to check class types for what gets passed into my object that I want configured.
You are looking for typealias in your protocol definition.
protocol Configurable {
typealias InputData
func configure(data: InputData) -> Void
}
In anything that implements your protocol you set the typealias to the type you would like.
class RelatedObject {
var x: String = ""
var y: String = ""
}
class Example {
var a: String = ""
var b: String = ""
var c: String = ""
}
extension Example: Configurable {
typealias InputData = RelatedObject
func configure(data: InputData) {
a = data.x
b = data.y
c = data.x + data.y
}
}

Resources