UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Generic functions without parameters?

Forums > Swift

I am new to Swift, coming for a primaryly C++ background (with experience in Python, Java, C#, C, and Haskell), as such I am experimenting with eth Swifty way of doing things, but my approach may be clouded my baked in habits carried over from oter languages.

When working with CoreData I have ended up with the the following code repeated code, where the only change is the TypeX parameter passed to ManagedObjectContextChanges<Type2>(notification: $0)

    private func observeChangeNotification() {
        let cancellables1 = NotificationCenter.default.publisher(
            for: .NSManagedObjectContextObjectsDidChange,
            object: moc)
            .compactMap({
                ManagedObjectContextChanges<Type1>(notification: $0)
            })
            .sink { (changes) in print(changes) }
        cancellables.append(cancellables1)

       let cancellables2 = NotificationCenter.default.publisher(
              for: .NSManagedObjectContextObjectsDidChange,
              object: moc)
              .compactMap({
                  ManagedObjectContextChanges<Type2>(notification: $0)
              })
              .sink { (changes) in print(changes) }

        cancellables.append(cancellables2)

        let cancellables3 = NotificationCenter.default.publisher(
               for: .NSManagedObjectContextObjectsDidChange,
               object: moc)
               .compactMap({
                   ManagedObjectContextChanges<Type3>(notification: $0)
               })
               .sink { (changes) in print(changes) }

         cancellables.append(cancellables3)
    }

I would like to avoid duplicated code and the obvious choice is to use a Generic function. Unfortunatley Swift only support generic functions that use the Type parameter in the function signature. In this case there is no need to pass ot return any objects of the Type which is changing betwen function calls. My solution is to create a simple class that hosts the generic function, as follows:

private func observeChangeNotification() {
    PublisherHelper<Widget>.generate(moc: moc, storage: &cancellables)
    PublisherHelper<Event>.generate(moc: moc, storage: &cancellables)
    PublisherHelper<EventAttribute>.generate(moc: moc, storage: &cancellables)
    PublisherHelper<EventAttributeType>.generate(moc: moc, storage: &cancellables)
}

class PublisherHelper<T: NSManagedObject> {
    static func generate(moc: NSManagedObjectContext,
                         storage: inout [AnyCancellable]) {
        let cancellableVals = NotificationCenter.default.publisher(
              for: .NSManagedObjectContextObjectsDidChange,
              object: moc)
              .compactMap({
                  ManagedObjectContextChanges<T>(notification: $0)
              })
              .sink { (changes) in print(changes) }

        storage.append(cancellableVals)
    }

All appears to be working as expected and I believe this is a neat solution, but I would like some feedback as to whether or not there is a better way.

4      

Creating an extension to NSManagedObject doesn't work?

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.