Swift: Instance method as closure not working - swift

var closureA: (String)->()
class Test {
func instanceMethod(string: String) {
}
}
let a = Test()
closureA = Test.instanceMethod(a)
closureA("hello")
Xcode10 Playground show error:
error: cannot assign value of type '(String) -> ()' to type '(String) -> ()'
closureA = Test.instanceMethod(a)
I already read: https://oleb.net/blog/2014/07/swift-instance-methods-curried-functions/

I think you are missing the point of closure, you can't store the function in the closure, but you can store the piece of code that call to the function with the variable you are passing into the closure, and the function still need a class instance to call it, so it should be like this:
var closureA: ((String)->())?
class Test {
func instanceMethod(string: String) {
print(string)
}
}
let a = Test()
//Assume you have a variable `str: String` before hand that will execute the code inside closure
closureA = { str in
a.instanceMethod(string: str)
}
//Actual call to the closure to execute it
closureA?("hello")

I think is the bug of the playground. If we create a temp closure B, and assign it to closure A. Then it works.
var closureA: (String)->()
class Test {
func instanceMethod(string: String) {
print("Test")
}
}
let a = Test()
let closureB = Test.instanceMethod(a)
closureA = closureB
closureA("hello") // works

Related

Getting a reference to an instance variable with a default parameter in Swift

So given the following code, how does one get a reference to a function that takes a parameter with a default value and invoke the reference with the default value?
class Test {
func doIt() { print("Done") }
func doIt(_ adjective: String = "better") {
print("Done \(adjective)")
}
}
let t = Test()
let fn1 = t.doIt as () -> Void
let fn2 = t.doIt as (String) -> Void
fn1() // Works
fn2() // Does not work; requires parameter
I also tried the following
let fn2 = t.doIt as (String?) -> Void
But that also does not work. Any ideas? I'd like to invoke fn2() and get the printed result "Done better"
I think you already got what you wanted, a reference to that function.
Since the type is (String) -> Void, it expects String as an input. So you have to pass it to call.

Complex Swift closure with generic argument

I have a scenario where I need to call a method with an optional closure argument where the closure receives a generic argument. Here's my simplified code:
class FooModel
{
}
class FooSubClass1 : FooModel
{
}
class FooSubClass2 : FooModel
{
}
class Client
{
func httpGet<T:FooModel>(closure:((T) -> Void)? = nil)
{
// Doing some network request stuff here and call onHTTPRequestComplete() when done!
onHTTPRequestComplete(data: result, closure: closure)
}
func onHTTPRequestComplete<T:FooModel>(data:[String:Any], closure:((T) -> Void)? = nil)
{
if let c = closure
{
switch(T)
{
case is FooSubClass1:
var foo = FooSubClass1()
// Process data into new FooSubClass1 object here!
c(foo)
case is FooSubClass2:
var foo = FooSubClass2()
// Process data into new FooSubClass2 object here!
c(foo)
}
}
}
}
class App
{
func someFunc()
{
client.httpGet()
{
response in
print("\(response)")
}
}
}
What I'm trying to do here:
I have a data model with a super class and several subclasses. In Client I do http requests to retrieve data and want to populate model objects with the data retrieved. The calling method in App should be able to provide a closure that is called after the asynchronous network request is done and in which I get back the correct data model object.
There are several issues with my code:
switch(T): Error: expected member name or constructor call after type name
c(foo): Error: '(FooSubClass1) -> Void' is not convertible to '(T) -> Void'
Not sure my closure definition makes sense for what I'm trying to do.
Updated code:
protocol FooModel
{
init()
}
class FooModelBase : FooModel
{
}
class FooSubClass1 : FooModelBase
{
}
class FooSubClass2 : FooModelBase
{
}
class Client
{
func httpGet<T:FooModel>(closure:((T) -> Void)? = nil)
{
// Doing some network request stuff here and call onHTTPRequestComplete() when done!
onHTTPRequestComplete(data: result, closure: closure)
}
func onHTTPRequestComplete<T:FooModel>(data:[String:Any], closure:((T) -> Void)? = nil)
{
if let c = closure
{
switch T.self
{
case is FooSubClass1.Type:
var foo = FooSubClass1()
// Assign retrieved values to model!
closure(foo)
case is FooSubClass2.Type:
var foo = FooSubClass2()
// Assign retrieved values to model!
closure(foo)
default:
break
}
}
}
}
class App
{
func someFunc()
{
client.httpGet()
{
response in
print("\(response)") // Should output FooSubClass1 instance with assigned values!
}
}
}
Almost works but I get the following compile error:
'closure(foo): Error: '(#lvalue FooSubClass1) -> Void' is not convertible to '(T) -> Void'
for the switch(T) error of course you get this error. Because you're trying to switch a type but you should be switching a value. For your second error it makes perfect sense because then what would closure be expecting then because T is generic.
For more info on generics in closures have a look at this stackoverflow thread
generics as parameters to a closure in swift
All in all what you're trying to do there just isn't possible and at first glance I believe this isn't the correct direction to go in (Although I could be wrong without seeing the whole context). One thing you can do instead maybe is have your closure be ((Any) -> Void)? but then unfortunately the type would be of Any and you would have to do a switch inside of the closure.
The value of T in the closure will be passed to the closure itself. You can't switch on it. But what I think you want is to switch on the type. You could do something like this:
let type = String(reflecting: T.self)
switch (type) {
case "Module.FooSubClass1":
var foo = FooSubClass1()
}
Or you could say:
if FooSubClass1.self == T.self {
var foo = FooSubClass1();
} else if FooSubClass2.self == T.self {
var foo = FooSubClass2();
}
However, this code smells. You seem to want to use generics but then have a hardcoded set of classes inside your generic method, which half-defeats the entire purpose of generics. Why not just define FooModel as
protocol FooModel {
init()
}
Then instead of your switch statement, just create one and pass it to your closure.
func onHTTPRequestComplete<T: FooModel>(data: [String: Any], closure: ((T) -> Void)? = nil) {
guard let closure = closure else { return }
closure(T())
}
Or why not create T outside of onHTTPRequestComplete and pass it?
onHTTPRequestComplete(data: data: model: FooSubClass1()) { model in
// Do something awesome with `model` right here.
}
However, what I'd probably do is this:
func httpGet(callback: (([String: Any]) -> Void)? = nil) {
// Do some network stuff and get the data
callback?(data)
}
Then inside of callback, just create whatever type you want:
httpGet { data in
let foo = FooSubClass1(data: data)
// foo on you
}

How to call a static method on a base class from a generic class<T>?

I'm trying to call a static method in a base class from a generic subclass. See simplified playground code below.
Calling the non-static 'dump' function works.
The similar static call fails. Various attempts to typecast the array also fail.
In the full production code, "ClassA" is in a 3rd-party module and can't be changed. It can be further subclassed and extended.
Does Swift offer some magical typecast to have ClassT call ClassA.dump() directly?
class ClassT<T> {
var dict=[String:T]()
func add(key:String, obj:T) {
dict[key]=obj
let arr=Array(dict.values)
dump(arr) // works -> but not as expected, see comment below !!!
ClassA.dump(arr) // error: cannot convert value of type 'Array<T>' to expected argument type '[ClassA]'
ClassA.dump(arr as! [ClassA]) // error: cannot convert value of type 'Array<T>' to type '[ClassA]' in coercion
ClassA.dump(arr as! [AnyObject]) // error: 'AnyObject' is not a subtype of 'T'
ClassA.dump(arr as! [Any]) // error: 'Any' is not a subtype of 'T'
}
}
class ClassA {
func dump(arr:[ClassA]) {
ClassA.dump(arr)
}
static func dump(arr:[ClassA]) {
print(arr)
}
}
class ClassB:ClassA {
static let o=ClassT<ClassA>()
func test() {
ClassB.o.add("Elem1", obj:self)
}
}
You have to add a constraint to specify that T derives from ClassA.
class ClassT<T: ClassA> {
var dict = [String : T]()
func add(key: String, obj: T) {
dict[key] = obj
let arr = Array(dict.values) //probably unecessary
dump(arr) // works
ClassA.dump(arr)
}
//...
Without it, the compiler has no way to enforce that all conforming types T will be castable to ClassA.

Using a Type Variable in a Generic

I have this question except for Swift. How do I use a Type variable in a generic?
I tried this:
func intType() -> Int.Type {
return Int.self
}
func test() {
var t = self.intType()
var arr = Array<t>() // Error: "'t' is not a type". Uh... yeah, it is.
}
This didn't work either:
var arr = Array<t.Type>() // Error: "'t' is not a type"
var arr = Array<t.self>() // Swift doesn't seem to even understand this syntax at all.
Is there a way to do this? I get the feeling that Swift just doesn't support it and is giving me somewhat ambiguous error messages.
Edit: Here's a more complex example where the problem can't be circumvented using a generic function header. Of course it doesn't make sense, but I have a sensible use for this kind of functionality somewhere in my code and would rather post a clean example instead of my actual code:
func someTypes() -> [Any.Type] {
var ret = [Any.Type]()
for (var i = 0; i<rand()%10; i++) {
if (rand()%2 == 0){ ret.append(Int.self) }
else {ret.append(String.self) }
}
return ret
}
func test() {
var ts = self.someTypes()
for t in ts {
var arr = Array<t>()
}
}
Swift's static typing means the type of a variable must be known at compile time.
In the context of a generic function func foo<T>() { ... }, T looks like a variable, but its type is actually known at compile time based on where the function is called from. The behavior of Array<T>() depends on T, but this information is known at compile time.
When using protocols, Swift employs dynamic dispatch, so you can write Array<MyProtocol>(), and the array simply stores references to things which implement MyProtocol — so when you get something out of the array, you have access to all functions/variables/typealiases required by MyProtocol.
But if t is actually a variable of kind Any.Type, Array<t>() is meaningless since its type is actually not known at compile time. (Since Array is a generic struct, the compiler needs know which type to use as the generic parameter, but this is not possible.)
I would recommend watching some videos from WWDC this year:
Protocol-Oriented Programming in Swift
Building Better Apps with Value Types in Swift
I found this slide particularly helpful for understanding protocols and dynamic dispatch:
There is a way and it's called generics. You could do something like that.
class func foo() {
test(Int.self)
}
class func test<T>(t: T.Type) {
var arr = Array<T>()
}
You will need to hint the compiler at the type you want to specialize the function with, one way or another. Another way is with return param (discarded in that case):
class func foo() {
let _:Int = test()
}
class func test<T>() -> T {
var arr = Array<T>()
}
And using generics on a class (or struct) you don't need the extra param:
class Whatever<T> {
var array = [T]() // another way to init the array.
}
let we = Whatever<Int>()
jtbandes' answer - that you can't use your current approach because Swift is statically typed - is correct.
However, if you're willing to create a whitelist of allowable types in your array, for example in an enum, you can dynamically initialize different types at runtime.
First, create an enum of allowable types:
enum Types {
case Int
case String
}
Create an Example class. Implement your someTypes() function to use these enum values. (You could easily transform a JSON array of strings into an array of this enum.)
class Example {
func someTypes() -> [Types] {
var ret = [Types]()
for _ in 1...rand()%10 {
if (rand()%2 == 0){ ret.append(.Int) }
else {ret.append(.String) }
}
return ret
}
Now implement your test function, using switch to scope arr for each allowable type:
func test() {
let types = self.someTypes()
for type in types {
switch type {
case .Int:
var arr = [Int]()
arr += [4]
case .String:
var arr = [String]()
arr += ["hi"]
}
}
}
}
As you may know, you could alternatively declare arr as [Any] to mix types (the "heterogenous" case in jtbandes' answer):
var arr = [Any]()
for type in types {
switch type {
case .Int:
arr += [4]
case .String:
arr += ["hi"]
}
}
print(arr)
I would break it down with the things you already learned from the first answer. I took the liberty to refactor some code. Here it is:
func someTypes<T>(t: T.Type) -> [Any.Type] {
var ret = [Any.Type]()
for _ in 0..<rand()%10 {
if (rand()%2 == 0){ ret.append(T.self) }
else {
ret.append(String.self)
}
}
return ret
}
func makeArray<T>(t: T) -> [T] {
return [T]()
}
func test() {
let ts = someTypes(Int.self)
for t in ts {
print(t)
}
}
This is somewhat working but I believe the way of doing this is very unorthodox. Could you use reflection (mirroring) instead?
Its possible so long as you can provide "a hint" to the compiler about the type of... T. So in the example below one must use : String?.
func cast<T>(_ value: Any) -> T? {
return value as? T
}
let inputValue: Any = "this is a test"
let casted: String? = cast(inputValue)
print(casted) // Optional("this is a test")
print(type(of: casted)) // Optional<String>
Why Swift doesn't just allow us to let casted = cast<String>(inputValue) I'll never know.
One annoying scenerio is when your func has no return value. Then its not always straightford to provide the necessary "hint". Lets look at this example...
func asyncCast<T>(_ value: Any, completion: (T?) -> Void) {
completion(value as? T)
}
The following client code DOES NOT COMPILE. It gives a "Generic parameter 'T' could not be inferred" error.
let inputValue: Any = "this is a test"
asyncCast(inputValue) { casted in
print(casted)
print(type(of: casted))
}
But you can solve this by providing a "hint" to compiler as follows:
asyncCast(inputValue) { (casted: String?) in
print(casted) // Optional("this is a test")
print(type(of: casted)) // Optional<String>
}

Assigning to a var defined in Protocol says that it can't assign to it

Here is a simple use case:
protocol Debuggable {
var _d: String -> () {
get set
}
}
class Debugger {
class func attach(instance: Debuggable) {
let d = Debugger()
instance._d = d.d // This doesn't work
}
func d(message: String) {
println(message)
}
}
Error message at compile time: Cannot assign to _d in instance, although my protocol defines the var with a getter and setter.
This is a good case of the compiler catching an error for you :)
instance may be a value type and therefore be copied in to this method. In that case, you will modify the copy and just throw it out. The quickest fix is to define it as an inout parameter:
class func attach(inout instance: Debuggable) {
let d = Debugger()
instance._d = d.d
}
I think it would be better to return the instance though:
class func attach(instance: Debuggable) -> Debuggable {
let d = Debugger()
var ret = instance
ret._d = d.d
return ret
}
I feel that inout is mostly in the language for backwards compatibility to Objective-C and C. Let the type determine if it should be copied instead of forcing it to be passed by reference. If something is defined as a value type, there is a good reason for it.