how to implement a tableview.notification in Swift

Hi, I used this notification in objective-c with success :

{
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(rowSelectionDidChange:)
                   name:NSTableViewSelectionDidChangeNotification object:nil];
  // ...
}

With Swift I got 5 errors:

        selector: #selector: ( NSTableView.rowSelectionDidChange(_:)),
        name:"NSTableViewSelectionDidChangeNotification",
        object: nil)

I hope you can teach me. Uwe

Edit:

        selector: #selector: ( NSTableView.rowSelectionDidChange(_:)),
        name:"NSTableViewSelectionDidChangeNotification",
        object: nil)

What are the 5 errors ? Please also show more code.

I could not find this notification in doc, only selectionDidChangeNotification. Are you using some framework ?

No framework, the projects I mentioned arose on Xcode 4.2, maybe a difference .

There is an error here, extra : Replace

selector: selector: selector: selector: #selector: (NSTableView.rowSelectionDidChange(_:)), name:"NSTableViewSelectionDidChangeNotification", object: nil)

with

selector: #selector(NSTableView.rowSelectionDidChange(_:)), name:"NSTableViewSelectionDidChangeNotification", object: nil)

And there are other errors:

  • Type 'NSTableView' has no member 'rowSelectionDidChange'
  • Cannot convert value of type 'String' to expected argument type 'NSNotification.Name?'

Change

name:"NSTableViewSelectionDidChangeNotification"

to

name: Notification.Name("NSTableViewSelectionDidChangeNotification")

I corrected the notification and got error :

    [center.addObserver: self,
        selector:#selector(rowSelectionDidChange(_:)),
        name:Notification.Name( selectionDidChangeNotification), object:nil]
    // error: Expected declaration

rowSelectionDidChange(_:) do not belong to tableView, it is the function I want to use

        if(arrayController != nil){
            var selected:[] as Any
            var arranged:[] as Any
            /*
             error :
             Consecutive statements on a line must be separated by ';'
             Insert ';'
             Expected element type
             Insert ' <#type#>'
             Expected expression
             */
       selected: NSArray = [arrayController.selectedObjects];
       arranged = [arrayController.arrangedObjects];
        }
    }
`
p.s. please give me a link of your communication rules .


p.s. please give me a link of your communication rules

What do you need ?

    [center.addObserver: self,
        selector:#selector(rowSelectionDidChange(_:)),
        name:Notification.Name( selectionDidChangeNotification), object:nil]
    // error: Expected declaration
Is it the Swift code ?

.

error :Consecutive statements on a line must be separated by ';'

Where exactly does this error show ?

Please show more complete code. It is very hard to understand with just a few lines out of context.

Hi Claude, it is Swift code, this project shall be the twin of my objective-c what must close because of deprecation of OpenGL. The first what I want is getting access to managedObjects, I send you the AppDelegate :

import Cocoa
import CoreData

@main
class AppDelegate: NSObject, NSApplicationDelegate {
    
    @IBOutlet weak var arrayController: NSArrayController!
    @IBOutlet weak var tableView: NSTableView!
    
   @objc var currentManagedObject:  [NSManagedObject]; // error:
    
    func myPrint() {
        if(arrayController != nil){
            print(arrayController!)
        }else {
            print("arrayController is NIL")
        }
        if(tableView != nil){
            print(tableView!)
        }else {
            print("tableView is NIL")
        }
    }
    var center = [NotificationCenter.default]
    [center.addObserver: self,
        selector:#selector(rowSelectionDidChange(_:)),
        name:Notification.Name( selectionDidChangeNotification), object:nil]
    // error: Expected declaration
    
    @objc func rowSelectionDidChange(_ notification: Notification){
        if(arrayController != nil){
            var selected:[] as Any
            var arranged:[] as Any
            // error :
            /*
             Consecutive statements on a line must be separated by ';'
             Insert ';'
             Expected element type
             Insert ' <#type#>'
             Expected expression
             */
    selected: NSArray = [arrayController.selectedObjects];
    arranged = [arrayController.arrangedObjects];
    if((selected.count) >= 0){
    // ...
    }
    }
}
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {}
    func applicationWillTerminate(_ aNotification: Notification) {}
    func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool { return true}
}

I hope it helps you to help me

how to implement a tableview.notification in Swift
 
 
Q