Questions tagged [protocol-oriented]
46 questions
75
votes
4 answers
Swift - Protocol extensions - Property default values
Let's say that I have the following protocol:
protocol Identifiable {
var id: Int {get}
var name: String {get}
}
And that I have the following structs:
struct A: Identifiable {
var id: Int
var name: String
}
struct B: Identifiable…

Axort
- 2,034
- 2
- 23
- 24
5
votes
2 answers
Why does the protocol default value passed to the function not change, even though the function does when subclassing?
I have a protocol, to which I have assigned some default values:
protocol HigherProtocol {
var level: Int { get }
func doSomething()
}
extension HigherProtocol {
var level: Int { 10 }
func doSomething() {
…

Kramer
- 338
- 2
- 15
4
votes
3 answers
Does protocol array elements passed by value or reference?
I know Structs are passed by value and classes are passed by reference in Swift.
I wonder if I create an array store elements which providing a protocol. These elements passed by value or reference?
Is it based on the definition of model a class or…

excE
- 70
- 9
4
votes
4 answers
Difference between adopting protocol+extension VS using instance of a class
I have been trying to grasp protocol-oriented-programming but i don't understand the difference between the 2 following scenarios...
Scenario 1
I have two classes that are UIViewControllers. Both of these classes need to use some common…

MikeG
- 3,745
- 1
- 29
- 51
3
votes
1 answer
Swift Protocol Oriented Mixed Scope
I have a Protocol Oriented Programming conceptual question. Let's say I am creating a protocol Foo, and I want to extend Foo with a function action() in a protocol extension. action() will always be mostly the same, no matter who is implementing, so…

Luke Street
- 441
- 1
- 4
- 8
3
votes
1 answer
Swift 3 Protocol Oriented Programming results in random SIGBUS crashes
I am responsible of a complete Swift 3 application and one of the crashes that occurs regularly is a SIGBUS signal that I can't understand at all:
Thread 0 Crashed:
0 libswiftCore.dylib 0x00000001009b4ac8 0x1007b8000 +2083528
1 LeadingBoards …

Dean
- 1,512
- 13
- 28
3
votes
1 answer
Why should not directly extend UIView or UIViewController?
I saw this question, with this code:
protocol Flashable {}
extension Flashable where Self: UIView
{
func flash() {
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
self.alpha = 1.0 //Object…

mfaani
- 33,269
- 19
- 164
- 293
3
votes
1 answer
Is There a Way to Hide a Default Initializer for Swift Struct?
I have a protocol called Parameter:
protocol Parameter {
var name: String { get }
var unit: Unit? { get }
var value: Double { get }
init(name: String, unit: Unit?, value: Double)
}
I also have 16 structs that conform to…

Nick Kohrn
- 5,779
- 3
- 29
- 49
2
votes
1 answer
Swift: Error during protocol oriented approach. Error: cannot convert return expression of type 'I.Job' to return type 'ActualJob'
I have two interfaces Controllerable and Interactorable which should work together to achieve something. Following are the protocols:
protocol Controllerable {
associatedtype Job: Decodable
func getJob() -> Job
func control(job:…

justintime
- 352
- 3
- 11
2
votes
1 answer
Cannot use mutating member on immutable value: 'self' is immutable
I don't understand why I'm getting this error. SomeController is a class, not a struct, and it is not immutable. Is there a work around to this?
class SomeController: APIFetchable {
let baseUrl = "https://jsonplaceholder.typicode.com"
let…

MikeG
- 3,745
- 1
- 29
- 51
2
votes
3 answers
How to use protocol oriented programming to improve my Swift code?
I have a rather large project structured in this format:
class One : FirstThree {
fileprivate var integers: [Int] {
return [1, 2, 3, 101, 102]
}
override func allIntegers() -> [Int] {
return integers
}
func…

Frankie
- 11,508
- 5
- 53
- 60
2
votes
1 answer
Protocol Oriented Programming, implicitly calling extension method
Having my first crack at POP. In this case I want to decorate some UIViewControllers so that any that they automatically raise a 'Page viewed' analytics event.
So I created a protocol, and and extension for that protocol:
protocol…

Si-N
- 1,495
- 1
- 13
- 27
1
vote
1 answer
How to make Base class property generic in swift so can use with multiple model type?
I want the property "cellViewModel" as generic so I can reuse BaseCustomCell with a different types of models.
Ex.
struct CELLVIEWMODEL {
var name: String
var address: String
}
class BaseCustomCell: UITableViewCell {
var…

Avijit Nagare
- 8,482
- 7
- 39
- 68
1
vote
0 answers
Why cannot conform to protocol with associated type?
I have following simple protocol:
protocol JSONParser {
associatedtype JSONResult
func parse(response: Response, type: T) -> JSONResult
}
Function parse takes simple Response which is struct and takes T which is Codable. And…

neo
- 1,314
- 2
- 14
- 34
1
vote
0 answers
How to instantiate two generics classes which depends on each other?
I have two generic classes which need each other to instantiate, is there an elegant way of doing that?
Check out the following example:
protocol Presentable {
/// Protocol for view
}
protocol Action {
/// Protocol for Presenter
}
class…

XcodeNOOB
- 2,135
- 4
- 23
- 29