How passing a protocol as parameter in Swift - protocols

In Objective-C, I know how passing a protocol as parameter:
- (void)MyMethod:(Protocol *)myparameter
But in Swift there is no more Protocol type.
How can I pass a protocol as parameter without knowing which is ?

In one of your comments you say:
"I want create a method which return an array of type of class which implements a desired protocol."
Have you tried something like the following:
//notice the use of #objc here
#objc protocol AlertProtocol
{
func getMyName()->String
}
class Class1 : AlertProtocol
{
let name = "Object 1"
func getMyName() -> String
{
return name
}
}
class Class2 : AlertProtocol
{
let name = "Object 2"
func getMyName() -> String
{
return name
}
}
//borrowing from and refactoring siLo's answer
func classesConformingToProtocol(proto:Protocol) -> [AnyClass]
{
let availableClasses : [AnyClass] = [ Class1.self, Class2.self ]
var conformingClasses = Array<AnyClass>()
for myClass : AnyClass in availableClasses
{
if myClass.conforms(to: proto)
{
conformingClasses.append(myClass)
}
}
return conformingClasses
}
Then use the above structure like this:
let classes = classesConformingToProtocol(AlertProtocol.self)
The tricky part that does the work is the "#objc" that exposes the protocol to the objective c runtime and allows us to pass any "Protocol Type" as a parameter.
Probably at some point in the future we will be able to do this in a "pure" Swift way.

Here is what I have tried:
#objc protocol Walker
{
func walk()
}
#objc protocol Runner
{
func run()
}
#objc class Zombie : Walker
{
func walk () { println("Brains...") }
}
#objc class Survivor : Runner
{
func run() { println("Aaaah, zombies!") }
}
func classesConformingToProtocol(proto:Protocol) -> AnyClass[]
{
let availableClasses : AnyClass[] = [ Zombie.self, Survivor.self ]
var conformingClasses = Array<AnyClass>()
for myClass : AnyClass in availableClasses
{
if myClass.conformsToProtocol(proto)
{
conformingClasses.append(myClass)
}
}
return conformingClasses
}
// This does not work
let walkers = classesConformingToProtocol(Walker.self)
let runners = classesConformingToProtocol(Runner.self)
I have been unable to convert Swift's Metatype information into a Protocol object.

In swift 2.0, I use it like this before:
classA.conformsToProtocol(XXXProtocol.self as! Protocol)
It doesn't works fine...
Look the definition of Protocol:
// All methods of class Protocol are unavailable.
// Use the functions in objc/runtime.h instead.
#available(iOS 2.0, *)
public class Protocol {
}
All are unavailable...and I don't know which to use instead in objc/runtime.h
So I have to use this method:
if ClassA is protocol<XXXProtocol> {
// do something
}
Currently, it works...

If you don't allow use #objc (because yours protocols have property, for example), the only solution that I found is with closure. Then, you need use a closure to use a protocol and return a value.
protocol Proto { }
protocol Proto2 { }
class Foo: Proto { }
class Bar: Proto, Proto2 { }
class Baz: Proto2 { }
class Qux { }
func printConforms(classList: [AnyClass], protoCond: (AnyClass) -> Any?) {
for i in classList {
print(i, terminator: " -> ")
if protoCond(i) != nil {
print("is subscriber")
} else {
print("NOT IS subscriber")
}
}
}
let myClasses: [AnyClass] = [Foo.self, Bar.self, Baz.self, Qux.self]
printConforms(classList: myClasses, protoCond: { $0 as? Proto.Type })
More complete example: https://gist.github.com/brunomacabeusbr/eea343bb9119b96eed3393e41dcda0c9
Edit
Another better solution is using generics, for example:
protocol Proto { }
class Foo: Proto { }
class Bar: Proto { }
class Baz { }
func filter<T>(classes: [AnyClass], byConformanceTo: T.Type) -> [AnyClass] {
return classes.filter { $0 is T }
}
filter(classes: [Foo.self, Bar.self, Baz.self], byConformanceTo: Proto.Type.self)
// return [Foo.self, Bar.self]

Worked out a way today (Xcode 6.1):
Firstly, the protocol must be marked as #objc for any checking to work.
Then use an "if let" cast to check for conformance.
#objc protocol MyProtocol {
var protocolValue: Int { get set }
}
if let conformingObject = someObject as? MyProtocol {
// conformingObject is now someObject cast to MyProtocol
conformingObject.protocolValue = 3
}

Related

How to use a function put in using a constructor

So I have this class:
class Sigil {
constructor(name = "", type = "", func = (oppositeCard, oppositeCardLane) => { }) {
this.name = name;
this.type = type;
this.function = func
}
Activate(oppositeCard, oppositeCardLane) {
};
}
var fly = new Sigil("Fly", "OnAttack", (oppositeCard, oppositeCardLane) => {
oppositeCard = cardLib.blank;
return oppositeCard;
})
What I wan to do is use that function that I put in using the constructor. So like i want to call the "func" function using the Activate method
All you do is call this.function.
class Sigil {
...
Activate(){
this.function();
}
...
}

Is it possible to mock accessors by Mockito in Kotlin?

Is it possible to mock getter and setter of the property by Mockito? Something like this:
#Test
fun three() {
val m = mock<Ddd>() {
// on { getQq() }.doReturn("mocked!")
}
assertEquals("mocked!", m.qq)
}
open class Ddd {
var qq : String = "start"
set(value) {
field = value + " by setter"
}
get() {
return field + " by getter"
}
}
To mock getter just write:
val m = mock<Ddd>()
`when`(m.qq).thenReturn("42")
also i suggest to use mockito-kotlin, to use useful extensions and functions like whenever:
val m = mock<Ddd>()
whenever(m.qq).thenReturn("42")
Complementing IRus' answer, you could also use the following syntax:
val mockedObj = mock<SomeClass> {
on { funA() } doReturn "valA"
on { funB() } doReturn "valB"
}
or
val mockedObj = mock<SomeClass> {
on(it.funA()).thenReturn("valA")
on(it.funB()).thenReturn("valB")
}

SWIFT2 : EXC_BAD_ACCESS down casting a class conforming to ErrorType

I have a simple Object hierarchy : RESTError with 2 attributes (httpCode and message), and 4 subclasses of it. One of the subclasses, RESTBusinessError has two additional fields.
I've simplified my code here below, but a RESTError variable (which is actually a RESTBusinessError) is used within a switch statement. Whenever I try to access the fields of the subclass, I've a EXC_BAD_ACCESS. Any idea ? this looks obvious, and in the debug area, I can see that all my variables have the expected values.
I have this issue ONLY in case RESTError conforms to the protocol ErrorType.
Any Idea ?
var tmpError:RESTError;
tmpError=RESTBusinessError(httpCode: 422, message: "error", businessCode: "003", businessMessage: "error");
switch tmpError {
case is RESTAuthorisationError:print("AUTH Error");
case let bError as RESTBusinessError:
let s1=bError.httpCode;
let s2=bError.message;
let s3=bError.businessCode; // <- This systematically fails.
let s4=bError.businessMessage;
print("OK \(s1) \(s2) \(s3) \(s4)");
default: print("default");
}
Object Hierarchy is here below :
public class RESTError : ErrorType{
var httpCode:Int
var message:String
init(httpCode:Int,message:String) {
self.httpCode=httpCode;
self.message=message;
}
}
class RESTAuthorisationError : RESTError {}
class RESTServerError : RESTError {}
class RESTOtherError : RESTError {}
public class RESTBusinessError : RESTError {
var businessCode:String
var businessMessage:String
init(httpCode:Int,message:String, businessCode:String, businessMessage:String) {
self.businessCode=businessCode;
self.businessMessage=businessMessage;
super.init(httpCode: httpCode, message: message);
}
}
check this example
import Foundation
func f() {
let queue = dispatch_queue_create("a", DISPATCH_QUEUE_CONCURRENT)
var i: Int! = 0
dispatch_async(queue) {
i = nil
}
usleep(200000)
print(i) // this is wrong
}
f() // debugger will stop here !!!!

Type 'className -> () -> className!' does not conform to protocol

I'm messing around with Swift. I have a protocol defined as
protocol timerProtocol {
func timerFired()
}
A class who holds a reference to the delegate
class Stopwatch: NSObject {
var delegate: protocol <timerProtocol>
init(delegate: protocol <timerProtocol> ) {
self.delegate = delegate
}
...
}
and a class that implements the protocol
class StopwatchesTableViewController: UITableViewController, timerProtocol {
func timerFired() {
println("timer");
}
let stopwatch = Stopwatch(delegate: self) // Error here
...
}
I get the error when declaring the stopwatch - "Type 'StopwatchesTableViewController -> () -> StopwatchesTableViewController!' does not conform to protocol 'timerProtocol'"
How do I fix this issue?
Change var delegate: protocol <timerProtocol>
To var delegate: timerProtocol?
syntactically and logically that works for me like a charm:
protocol TimerProtocol {
func timerFired()
}
class Stopwatch {
var delegate: protocol <TimerProtocol>? = nil
init() { }
convenience init(delegate: protocol <TimerProtocol> ) {
self.init()
self.delegate = delegate
}
}
class StopwatchesTableViewController: UITableViewController, TimerProtocol {
#lazy var stopwatch: Stopwatch = Stopwatch()
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
stopwatch.delegate = self
}
func timerFired() {
println("timer");
}
}
NOTE: the protocols' names should start with capital letter.
or
the StopwatchesTableViewController class would look like e.g. this:
class StopwatchesTableViewController: UITableViewController, TimerProtocol {
var stopwatch: Stopwatch? = nil
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
stopwatch = Stopwatch(delegate: self)
}
func timerFired() {
println("timer");
}
}
Try change,
let stopwatch = Stopwatch(delegate: self) // Error here
to
#lazy var stopwatch: Stopwatch = Stopwatch(delegate: self)
Your code
let stopwatch = Stopwatch(delegate: self)
is in the scope of the class (not inside a func) and hence self refers to the class (not an instance). The class does not conform to the protocol, only its instances.
You need to do
let stopwatch: Stopwatch
func init() {
stopwatch = Stopwatch(delegate: self)
}

Groovy closures, def vs typed return value

In the Groovy console, version 2.2.1:
Why does this work?
class C {
def foo = { "foo" }
def bar = { foo() }
}
new C().bar()
but this fails?
class C {
String foo = { "foo" }
String bar = { foo() }
}
new C().bar()
The above was answered by tim_yates but I have something somewhat related that doesn't seem like it's worth creating a new question for (not sure of the etiquette). When I make them static it also fails when I call bar(). Why does the bar closure not capture foo?
class C {
static foo = { "foo" }
static bar = { foo() }
}
C.foo() //works
C.bar() //fails
Because neither { "foo" } or { foo() } are Strings?
They are Closure<String>
Try:
class C {
Closure<String> foo = { "foo" }
Closure<String> bar = { foo() }
}
new C().bar()

Resources