Why is self unresolved? - swift

protocol LOL {
var foo: ((_ lol: String) -> Bool)? { get set }
}
class LMAO: LOL {
internal var foo: ((String) -> Bool)? = { (_ lol: String) in
self.rofl()
return true
}
func rofl() {}
}
Why is self unresolved in the internal foo variable?
What am I doing wrong here?

The closure you are assigning to foo is independent of your LMAO class so it has no "self".
if you want to initialize your foo variable with code from the current instance's self, you need to use lazy initialization. You will also need to link to the current instance somehow (a plain closure will not do it ).
the easiest way would probably be to add a defaultFoo function to your class and assign it as the initial value:
class LMAO: LOL
{
lazy var foo: ((String) -> Bool)? = self.defaultFoo
func defaultFoo (_ lol: String) -> Bool
{
self.rofl()
return true
}
func rofl() {}
}
At this point you probably don't need foo to be an optional, but that's up to you.

Related

Can I know if a function is a method? If so, can I know its object?

In Swift, a method is a special type of function, one attached to an object.
If I am given an arbitrary function, I want to know if it is a method. If it is, I want to know the object it is attached to.
Something like this:
func ownerOfFunction(_ f: () -> Void) -> Any? {
return isMethod(f) ? getObject(f) : nil
}
Does anything like isMethod() or getObject() exist?
func ownerOfFunc<T> (_ parameter: AnyObject,_ type: T.Type) -> Bool {
return parameter is T
}
protocol P1 {}
protocol P2: P1 {}
class C1: NSObject, P2 {
#objc func fuc1() {}
func fuc2() {}
}
let o = C1()
o.responds(to: Selector("fuc1")) // true (Works only Objc selectors)
o.responds(to: Selector("fuc2")) // false
ownerOfFunc(o, P1.self) // true
ownerOfFunc(o, P2.self) // true
ownerOfFunc(o, Int.self) // false

How to use a protocol function as block definition?

Here a function b calls a and passes a completion block:
func a(completion: (_ succeeded: Bool)) {
completion(true)
}
func b() {
a() { succeeded in
...
}
}
Can I use a protocol function definition to define the block?
Something like this (which does not work):
protocol P {
func c(succeeded: Bool)
}
func a(completion: P) {
completion.c(succeeded: true)
}
func b() {
a() { c(succeeded) in
...
}
}
Note: I am not looking for the protocol/delegate concept.
The issue with using a protocol here is that functions cannot conform to a protocol, only classes, enums and structs can conform to a protocol. So you either need one of these types or an instance of one of these types, but then the instance seems superfluous. If you really want to do it the protocol way here is an example with static conformance on an enum:
import UIKit
protocol Callable {
static func call(someString: String, someInt: Int)
}
enum MyCallable: Callable {
static func call(someString: String, someInt: Int) {
print(someString, someInt)
}
}
func someFunction<T: Callable>(callable: T.Type) {
callable.call(someString: "a",someInt: 1)
}
someFunction(callable: MyCallable.self)
You can use a labeled tuple to give yourself argument labels if that is what you are after. Here is a playground example:
import UIKit
typealias MyArguements = (someInt: Int, someString: String)
func someFunction( completion: #escaping (MyArguements) -> Void) {
completion((someInt: 1, someString: "a"))
}
someFunction { tuple in
let (someInt, someString) = tuple
print(someInt, someString)
}

Swift generic function saved as varaible

Given a class:
class First<T> {
}
And a method of class First:
func second<U>(closure: (value: T) -> U) {
}
How could I store the closure passed as an argument to second so that I can call it at a later date?
You would need to declare U in the class instead, so that you have a type for the storage:
class First<T,U> {
var f : ((T) -> U)! = nil
func second(closure: #escaping (T) -> U) {
self.f = closure
}
}
If having the function second work for only one kind of type is good enough for you, then Matt's answer is good.
class First<T, U> {
typealias ClosureType = (value: T) -> U
var savedClosure: ClosureType? = nil
func second(closure: ClosureType) {
savedClosure = closure
}
}
That doesn't actually answer your question as stated!
The thing is: You can't store a value of an unknown type.
But! if the type conforms to a known protocol, then you can save it.
protocol P {}
class First<T> {
typealias ClosureType = (value: T) -> P
var savedClosure: ClosureType? = nil
func second<U: P>(closure: (value: T) -> U) {
savedClosure = closure
}
}
The protocol could even be protocol<> "no protocol at all", which is typealiased to the keyword Any.
class First<T> {
typealias ClosureType = (value: T) -> Any
var savedClosure: ClosureType? = nil
func second<U>(closure: (value: T) -> U) {
savedClosure = closure
}
}
But we don't really know what you want to do, so there are multiple answers to your question… for example, maybe you wanted to store a separate closure for each type?
class First<T> {
typealias ClosureType = (value: T) -> Any
var savedClosures = [String: ClosureType]()
func second<U>(closure: (value: T) -> U) {
savedClosures[String(U)] = closure
}
}
Anyway, the real question is: "Do you really need to do this? Is there some simple change you can do that obviates this need?"

How to pass closure with argument as argument and execute it?

Initializer of class A takes an optional closure as argument:
class A {
var closure: ()?
init(closure: closure()?) {
self.closure = closure
self.closure()
}
}
I want to pass a function with an argument as the closure:
class B {
let a = A(closure: action(1)) // This throws the error: Cannot convert value of type '()' to expected argument type '(() -> Void)?'
func action(_ i: Int) {
//...
}
}
Class A should execute the closure action with argument i.
I am not sure about how to write this correctly, see error in code comment above. What has to be changed?
Please make your "what-you-have-now" code error free.
Assuming your class A like this:
class A {
typealias ClosureType = ()->Void
var closure: ClosureType?
init(closure: ClosureType?) {
self.closure = closure
//`closure` would be used later.
}
//To use the closure in class A
func someMethod() {
//call the closure
self.closure?()
}
}
With A given above, you need to rewrite your class B as:
class B {
private(set) var a: A!
init() {
//initialize all instance properties till here
a = A(closure: {[weak self] in self?.action(1)})
}
func action(i: Int) {
//...
}
}
The problem is that closure()? is not a type. And ()? is a type, but it is probably not the type you want.
If you want var closure to have as its value a certain kind of function, you need to use the type of that function in the declaration, e.g.
var closure: (Int) -> Void
Similarly, if you want init(closure:) to take as its parameter a certain kind of function, you need to use the type of that function in the declaration, e.g.
init(closure: (Int) -> Void) {
Types as Parameters
In Swift, every object has a type. For example, Int, String, etc. are likely all types you are extremely familiar with.
So when you declare a function, the explicit type (or sometimes protocols) of any parameters should be specified.
func swallowInt(number: Int) {}
Compound Types
Swift also has a concept of compound types. One example of this is Tuples. A Tuple is just a collection of other types.
let httpStatusCode: (Int, String) = (404, "Not Found")
A function could easily take a tuple as its argument:
func swallowStatusCode(statusCode: (Int, String)) {}
Another compound type is the function type. A function type consists of a tuple of parameters and a return type. So the swallowInt function from above would have the following function type: (Int) -> Void. Similarly, a function taking in an Int and a String and returning a Bool would have the following type: (Int, String) -> Bool.
Function Types As Parameters
So we can use these concepts to re-write function A:
class A {
var closure: (() -> Void)?
init(closure: (() -> Void)?) {
self.closure = closure
self.closure()
}
}
Passing an argument would then just be:
func foo(closure: (Int) -> Void) {
// Execute the closure
closure(1)
}
One way to do what I think you're attempting is with the following code:
class ViewController: UIViewController {
override func viewDidLoad() {
let _ = A.init(){Void in self.action(2)}
}
func action(i: Int) {
print(i)
}
}
class A: NSObject {
var closure : ()?
init(closure: (()->Void)? = nil) {
// Notice how this is executed before the closure
print("1")
// Make sure closure isn't nil
self.closure = closure?()
}
}

Can I specify that a class instance can be converted to some type?

Say I have a generic class
class Foo<T> { … }
Can I somehow specify that instances of this class can be converted to T in assignments? Example:
let foo = Foo<Int>()
func useAnInt(a: Int) {}
let getTheInt: Int = foo
useAnInt(foo)
Why not just use the underlying type? (Similar to #MrBeardsley's answer)
class Foo<T> {
var t : T
init(t: T) {
self.t = t
}
}
let foo = Foo(t: 3)
func useAnInt(a: Int) {}
let getTheInt: Int = foo.t
useAnInt(foo.t)
You are not able to do what you are asking. Even though Foo defines a generic type T, instances of Foo are still Foo and cannot be converted to the generic type. The reason you would declare a generic type at the class level is to use it multiple places throughout the class.
class Foo<T> {
var value: T
init(withValue: T) {
self.value = withValue
}
func getSomeValue() -> T {
return self.value
}
}
Generics don't mean the class itself is generic and can be converted.
One way of achieving what you want is to use a dedicated protocol for each of the target types that your class shall be convertible to. Here is very basic example:
protocol BoolAdaptable {
func adaptToBool() -> Bool
}
protocol IntAdaptable {
func adaptToInt() -> Int
}
protocol FloatAdaptable {
func adaptToFloat() -> Float
}
class Foo: BoolAdaptable, IntAdaptable, FloatAdaptable {
var v: NSNumber
init(_ v: NSNumber) {
self.v = v
}
func adaptToBool() -> Bool {
return v.boolValue
}
func adaptToInt() -> Int {
return v.integerValue
}
func adaptToFloat() -> Float {
return v.floatValue
}
}
let foo = Foo(NSNumber(double: 1.23))
let getTheBool = foo.adaptToBool() // true
let getTheInt = foo.adaptToInt() // 1
let getTheFloat = foo.adaptToFloat() // 1.23
This could easily be extended to support more complex conversions.