eranh
eranh

Reputation: 556

Anonymous class in swift

Is there an equivalent syntax or technique for Anonymous class in Swift? Just for clarification Anonymous class in Java example here - http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

Thanks!

Upvotes: 50

Views: 29527

Answers (7)

yeoman
yeoman

Reputation: 1681

Simply use a struct for defining the interface via function values and then anonymously implement it from a function, as is a very common way to write objects in JavaScript.

The function is only required for creating a private scope for the object returned.

import Foundation

struct Logger {
    let info: (String) -> ()
    let error: (String) -> ()
}

func createSimpleLogger() -> Logger {
    var count = 0
    
    func number() -> Int {
        count += 1
        return count
    }
    
    return Logger(
        info: { message in
            print("INFO - #\(number()) - \(message)")
        },
        error: { message in
            print("ERROR - #\(number()) - \(message)")
        }
    )
}

let logger = createSimpleLogger()

logger.info("Example info log message")
logger.error("Example error log message")

Output:

INFO - #1 - Example info log message
ERROR - #2 - Example error log message

Upvotes: 0

Nusatad
Nusatad

Reputation: 3551

If you want to inline a click handler in Java style fashion, first define your closure as a variable in your button class:

  var onButtonClick: () -> Void = {}

Then add a method to accept the closure as parameter and store it in the variable for later use:

func onClick(label: String, buttonClickHandler: @escaping () -> Void) {
   button.label = label 
   onButtonClick = buttonClickHandler
}

Whenever the closure should be executed, call it in your button class:

onButtonClick()

And this is how to set the action that should occur on click:

newButton.onClick(label: "My button") { () in
    print("button clicked")
}

You can also accept multiple parameters. For example, a toggle button may be handled like this:

var buttonClicked: (_ isOn: Bool) -> Void { set get }

Upvotes: 0

This is what I ended up doing (Observer pattern). You can use closures in a similar way you would use anonymous classes in Java. With obvious limitations of course.

class Subject {
   // array of closures
   var observers: [() -> Void] = []

   // @escaping says the closure will be called after the method returns
   func register(observer: @escaping () -> Void) {
       observers.append(observer)
   }

   func triggerEvent() {
       observers.forEach { observer in
            observer()
       }
   }
}

var subj = Subject()
// you can use a trailing closure
subj.register() {
    print("observerd")
}

// or you can assign a closure to a variable so you can maybe use the reference to removeObserver() if you choose to implement that method
var namedObserver: () -> Void = {
    print("named observer")
}
subj.register(observer: namedObserver)

subj.triggerEvent()
// output:
// observerd
// named observer

Upvotes: 1

karthikPrabhu Alagu
karthikPrabhu Alagu

Reputation: 3401

No anonymous class syntax in Swift. But, you can create a class inside a class and class methods:

class ViewController: UIViewController {

    class anonymousSwiftClass {
        func add(number1:Int, number2:Int) -> Int {
            return number1+number2;
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        class innerSwiftClass {
            func sub(number1:Int, number2:Int) -> Int {
                return number1-number2;
            }
        }

        var inner = innerSwiftClass();
        println(inner.sub(2, number2: 3));

        var anonymous = anonymousSwiftClass();
        println(anonymous.add(2, number2: 3));
    }
}

Upvotes: 7

Aaron Rasmussen
Aaron Rasmussen

Reputation: 13316

You can also create a basic empty class that acts like a bare protocol, and pass a closure to the init function that overrides anything you want, like this:

class EmptyClass {

    var someFunc: () -> () = { }

    init(overrides: EmptyClass -> EmptyClass) {
        overrides(self)
    }
}

// Now you initialize 'EmptyClass' with a closure that sets
// whatever variable properties you want to override:

let workingClass = EmptyClass { ec in
    ec.someFunc = { println("It worked!") }
    return ec
}

workingClass.someFunc()  // Outputs: "It worked!"

It is not technically 'anonymous' but it works the same way. You are given an empty shell of a class, and then you fill it in or override whatever parameters you want when you initialize it with a closure.

It's basically the same, except instead of fulfilling the expectations of a protocol, it is overriding the properties of a class.

Upvotes: 14

hpique
hpique

Reputation: 120344

There is no equivalent syntax, as far as I know.

Regarding equivalent techniques, theoretically you could use closures and define structs and classes inside them. Sadly, I can't get this to work in a playground or project without making it crash. Most likely this isn't ready to be used in the current beta.

Something like...

protocol SomeProtocol {
    func hello()
}

let closure : () -> () = {
    class NotSoAnonymousClass : SomeProtocol {
        func hello() {
            println("Hello")
        }
    }
    let object = NotSoAnonymousClass()
    object.hello()
}

...currently outputs this error:

invalid linkage type for global declaration
%swift.full_heapmetadata* @_TMdCFIv4Test7closureFT_T_iU_FT_T_L_19NotSoAnonymousClass
LLVM ERROR: Broken module found, compilation aborted!
Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 1

Upvotes: 23

Sulthan
Sulthan

Reputation: 130122

For example, Java listener/adapter pattern would be translated to Swift like this:

protocol EventListener {
    func handleEvent(event: Int) -> ()
}

class Adapter : EventListener {
    func handleEvent(event: Int) -> () {
    }
}

var instance: EventListener = {
    class NotSoAnonymous : Adapter {
        override func handleEvent(event: Int) {
            println("Event: \(event)")
        }
    }

    return NotSoAnonymous()
}()

instance.handleEvent(10)

(Crashing the compiler on Beta 2)

The problem is, you always have to specify a name. I don't think Apple will ever introduce anonymous classes (and structs etc.) because it would be pretty difficult to come with a syntax that doesn't collide with the trailing closures.

Also in programming anonymous things are bad. Naming things help readers to understand the code.

Upvotes: 10

Related Questions