iOS Swift Development: Protocols

iOS Swift Development: Protocols

memorize.aimemorize.ai (lvl 286)
Section 1

Preview this deck

Instead of having the write ".floatValue" after each non-float type. Create a function that can simply add an Int to a float

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (8)

Section 1

(8 cards)

Instead of having the write ".floatValue" after each non-float type. Create a function that can simply add an Int to a float

Front

protocol Number { var floatValue: Float { get } } extension Float: Number { var floatValue: Float { return self } extension Int: Number { var floatValue: Float { return Float(self) } } func +(valueA: Number, valueB: Number) { valueA.floatValue + valueB.floatValue } three + four

Back

Watch this video

Front

https://www.youtube.com/watch?v=KzypIO_QoMw

Back

Create a protocol called someProtocol

Front

protocol SomeProtocol { // protocol definition goes here }

Back

What is a protocol?

Front

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements.

Back

Make a background color picker app using Protocols

Front

Back

Create a class and attach a class called SomeSuperClass, and two protocols called FirstProtocol and AnotherProtocol You must list the class before calling the protocols

Front

class SomeClass: SomeSuperclass, FirstProtocol, AnotherProtocol { // class definition goes here }

Back

Property Requirements:

Front

protocol SomeProtocol { var mustBeSettable: Int { get set } var doesNotNeedToBeSettable: Int { get } } protocol AnotherProtocol { static var someTypeProperty: Int { get set } }

Back

Create a protocol that takes a Int and adds it to a Float

Front

protocol Number { var floatValue: Float { get } } extension Float: Number { var floatValue: Float { return self } extension Int: Number { var floatValue: Float { return Float(self) } } var three: Int = 3 var four: Float = 4 three.floatValue + four

Back