NSOutlineView with NSTreeController bindings with core data - cocoa

Please send me some links on how to use NSOutlineView with NSTreeController bindings with using core data.
https://www.dropbox.com/s/em7kxjqmxqjto88/outlineViewCoreData%20copie.zip?dl=0 this example is ok
https://www.dropbox.com/s/1asg3kkf9ltwj2w/outlineViewCoreData.zip?dl=0
this example is ko. ==> why
the entity EntityCategory is not key value coding-compliant for the key "category".

I have found the solution
All is ok
https://www.dropbox.com/s/nnh0e0atwmrqpmm/outlineViewCoreDataOK.zip?dl=0
i have add
extension EntityCategory {
#objc var children : NSSet {
return []
}
#objc var count : Int {
return 0
}
#objc var isLeaf : Int {
return 1
}
}
and
extension EntityAffectation {
#objc var children : NSSet {
return category!
}
#objc var count : Int {
return category!.count
}
#objc var isLeaf : Int {
return 0
}
}
and

Related

SwfitUI 2.0 Xcode 12 How To Update A Single Attribute Of A Core Data Entity

I am trying to implement a reset method in my View Model that will be called in a View on button action So the Views will get updated and persisted
Here are the attributes of the entity published in my View Model
var viewContext: NSManagedObjectContext { PersistenceController.shared.container.viewContext }
#Published var price: Double
#Published var qty: Int
#Published var subtotal: Double
Here is my resetAllCounters method I tried I know it's the wrong approach I just want to reset the qty to 0 and update all the counters
func resetAllSubtotals(){
let allCounters: NSFetchRequest<WindowCounter> = WindowCounter.fetchRequest()
do {
let savedWindowCounters = try self.viewContext.fetch(allCounters)
for counter in savedWindowCounters {
counter.qty = 0
}
try self.viewContext.save()
} catch {
print(error.localizedDescription)
}
}
Use NSBatchUpdateRequest
func resetAllSubtotals(){
let request = NSBatchUpdateRequest(entityName: "WindowCounter")
request.propertiesToUpdate = ["qty":0]
request.resultType = .updatedObjectsCountResultType
do {
let result = try context.execute(request) as! NSBatchUpdateResult
//This print the number of rows affected/updated
print(result.result!)
}catch {
//Handel Catch here
}
}
My Solution is I decided not to Observe object in the view model and instead just observe the entity itself as it conforms to ObservableObject Then made a subclass to handle the method
extension NSManagedObjectContext {
func resetAllSubtotals(){
let allCounters: NSFetchRequest<WindowCounter> =
WindowCounter.fetchRequest()
do {
let savedWindowCounters = try self.fetch(allCounters)
for counter in savedWindowCounters {
counter.qty = 0
counter.subtotal = 0
}
try self.save()
} catch {
print(error.localizedDescription)
}
}
}

OSX SwiftUI integrating NSComboBox Not refreshing current sélected

SwiftUI Picker is looking very bad on OSX especially when dealing with long item lists
Swiftui Picker on OSX with a long item list
And since did find any solution to limit the number of item displayed in by Picker on Osx , I decided to interface NSComboBox to SwiftUI
Everythings looks fine until the selection index is modified programmatically using the #Published index of the Observable Comboselection class instance (see code below) :
the updateNSView function of the NSViewRepresentable instance is called correctly then (print message visible on the log)
combo.selectItem(at: selected.index)
combo.selectItem(at: selected.index)
combo.objectValue = combo.objectValueOfSelectedItem
print("populating index change \(selected.index) to Combo : (String(describing: combo.objectValue))")
is executed correctly and the printed log shows up the correct information
But the NSComboBox textfield is not refreshed with the accurate object value
Does somebody here have an explanation ?? ; is there something wrong in code ??
here the all code :
import SwiftUI
class ComboSelection : ObservableObject {
#Published var index : Int
init( index: Int ) {
self.index = index
}
func newSelection( newIndex : Int ) {
index = newIndex
}
}
//
// SwiftUI NSComboBox component interface
//
struct SwiftUIComboBox : NSViewRepresentable {
typealias NSViewType = NSComboBox
var content : [String]
var nbLines : Int
var selected : ComboSelection
final class Coordinator : NSObject ,
NSComboBoxDelegate {
var control : SwiftUIComboBox
var selected : ComboSelection
init( _ control: SwiftUIComboBox , selected : ComboSelection ) {
self.selected = selected
self.control = control
}
func comboBoxSelectionDidChange(_ notification: Notification) {
print ("entering coordinator selection did change")
let combo = notification.object as! NSComboBox
selected.newSelection( newIndex: combo.indexOfSelectedItem )
}
}
func makeCoordinator() -> SwiftUIComboBox.Coordinator {
return Coordinator(self, selected:selected)
}
func makeNSView(context: NSViewRepresentableContext<SwiftUIComboBox>) -> NSComboBox {
let returned = NSComboBox()
returned.numberOfVisibleItems = nbLines
returned.hasVerticalScroller = true
returned.usesDataSource = false
returned.delegate = context.coordinator // Important : not forget to define delegate
for key in content{
returned.addItem(withObjectValue: key)
}
return returned
}
func updateNSView(_ combo: NSComboBox, context: NSViewRepresentableContext<SwiftUIComboBox>) {
combo.selectItem(at: selected.index)
combo.objectValue = combo.objectValueOfSelectedItem
print("populating index change \(selected.index) to Combo : \(String(describing: combo.objectValue))")
}
}
Please see updated & simplified your code with added some working demo. The main reason of issue was absent update of SwiftUI view hierarchy, so to have such update I've used Binding, which transfers changes to UIViewRepresentable and back. Hope this approach will be helpful.
Here is demo
Below is one-module full demo code (just set
window.contentView = NSHostingView(rootView:TestComboBox()) in app delegate
struct SwiftUIComboBox : NSViewRepresentable {
typealias NSViewType = NSComboBox
var content : [String]
var nbLines : Int
#Binding var selected : Int
final class Coordinator : NSObject, NSComboBoxDelegate {
var selected : Binding<Int>
init(selected : Binding<Int>) {
self.selected = selected
}
func comboBoxSelectionDidChange(_ notification: Notification) {
print ("entering coordinator selection did change")
if let combo = notification.object as? NSComboBox, selected.wrappedValue != combo.indexOfSelectedItem {
selected.wrappedValue = combo.indexOfSelectedItem
}
}
}
func makeCoordinator() -> SwiftUIComboBox.Coordinator {
return Coordinator(selected: $selected)
}
func makeNSView(context: NSViewRepresentableContext<SwiftUIComboBox>) -> NSComboBox {
let returned = NSComboBox()
returned.numberOfVisibleItems = nbLines
returned.hasVerticalScroller = true
returned.usesDataSource = false
returned.delegate = context.coordinator // Important : not forget to define delegate
for key in content {
returned.addItem(withObjectValue: key)
}
return returned
}
func updateNSView(_ combo: NSComboBox, context: NSViewRepresentableContext<SwiftUIComboBox>) {
if selected != combo.indexOfSelectedItem {
DispatchQueue.main.async {
combo.selectItem(at: self.selected)
print("populating index change \(self.selected) to Combo : \(String(describing: combo.objectValue))")
}
}
}
}
struct TestComboBox: View {
#State var selection = 0
let content = ["Alpha", "Beta", "Gamma", "Delta", "Epselon", "Zetta", "Eta"]
var body: some View {
VStack {
Button(action: {
if self.selection + 1 < self.content.count {
self.selection += 1
} else {
self.selection = 0
}
}) {
Text("Select next")
}
Divider()
SwiftUIComboBox(content: content, nbLines: 3, selected: $selection)
Divider()
Text("Current selection: \(selection), value: \(content[selection])")
}
.frame(width: 300, height: 300)
}
}

iOS DynamoDB Object Mapper load does not return all the attributes

I'm running below function in my iPad app to get an Item by it's hash key (IdName).
This table only contains an Hashkey (No Range key available) but when I run this, It returns an result object which contains only the HashKey Value (The same which I pass). The other property (IdValue) is not there in the result. What am I doing wrong here?
func getCurrentFinalReportNumber()->Int
{
let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.defaultDynamoDBObjectMapper()
var currentId :Int = -1
dynamoDBObjectMapper .load(DDBIDStoreTableRow.self, hashKey: "FinalReportNumber", rangeKey: nil) .continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task:AWSTask!) -> AnyObject! in
if (task.error == nil) {
if (task.result != nil) {
let resultRow :DDBIDStoreTableRow = task.result as! DDBIDStoreTableRow
print(resultRow.IdValue)
currentId = Int(resultRow.IdValue!)
}
} else {
print("Error: \(task.error)")
let alert = SCLAlertView()
alert.addButton("Close", colorButtonBackground: Constants.InspectionTypes.colorClose) {}
alert.showCloseButton = false
alert.showError("", subTitle: (task.error?.description)!)
}
return nil
}).waitUntilFinished()
return currentId
}
class DDBIDStoreTableRow :AWSDynamoDBObjectModel ,AWSDynamoDBModeling {
var IdName:String? //HK
var IdValue:Int? = -1
class func dynamoDBTableName() -> String! {
return AWSIDStoreDynamoDBTableName
}
class func hashKeyAttribute() -> String! {
return "IdName"
}
//MARK: NSObjectProtocol hack
override func isEqual(object: AnyObject?) -> Bool {
return super.isEqual(object)
}
override func `self`() -> Self {
return self
}
}
Just found the mistake. Problem is in the data type of the mapping class. Earlier it was
var IdValue:Int? = -1
But once I change the Int to NSNumber as below. It started to work fine
var IdValue:NSNumber? = -1

Cocoa: react to keyDown in QLPreviewPanel

I implemented quick look in my project in the following way in Swift 2 (I'm including this here for reference and because it might help someone else set it up).
My NSViewController contains a NSTableView subclass where I implemented keyDown to listen to the spacebar key being pressed (maybe not the best way but it works):
override func keyDown(theEvent: NSEvent) {
let s = theEvent.charactersIgnoringModifiers!
let s1 = s.unicodeScalars
let s2 = s1[s1.startIndex].value
let s3 = Int(s2)
if s3 == Int(" ".utf16.first!) {
NSNotificationCenter.defaultCenter().postNotification(NSNotification(name: "MyTableViewSpacebar", object: nil))
return
}
super.keyDown(theEvent)
}
In my view controller, I have an observer for this notification and the functions required by the QLPreviewPanel:
//...
class ViewController: NSViewController {
#IBOutlet weak var myTableView: MyTableView!
var files = [FilesListData]() //array of custom class
//...
override func viewDidLoad() {
//...
NSNotificationCenter.defaultCenter().addObserver(self, selector: "spaceBarKeyDown:", name: "MyTableViewSpacebar", object: nil)
}
func spaceBarKeyDown(notification: NSNotification) {
if let panel = QLPreviewPanel.sharedPreviewPanel() {
panel.makeKeyAndOrderFront(self)
}
}
override func acceptsPreviewPanelControl(panel: QLPreviewPanel!) -> Bool {
return true
}
override func beginPreviewPanelControl(panel: QLPreviewPanel!) {
panel.delegate = self
panel.dataSource = self
}
override func endPreviewPanelControl(panel: QLPreviewPanel!) {
}
}
extension ViewController: QLPreviewPanelDataSource {
func numberOfPreviewItemsInPreviewPanel(panel: QLPreviewPanel!) -> Int {
return self.myTableView.selectedRowIndexes.count
}
func previewPanel(panel: QLPreviewPanel!, previewItemAtIndex index: Int) -> QLPreviewItem! {
if self.myTableView.selectedRow != -1 {
var items = [QLPreviewItem]()
let manager = NSFileManager.defaultManager()
for i in self.myTableView.selectedRowIndexes {
let path = self.files[i].path //path to a MP3 file
if manager.fileExistsAtPath(path) {
items.append(NSURL(fileURLWithPath: path))
} else {
items.append(qm_url) //image of a question mark used as placeholder
}
}
return items[index]
} else {
return qm_url //image of a question mark used as placeholder
}
}
}
What I would like to do now is listen to the keys "up arrow" and "down arrow" being pressed while the quick look panel is open, in order to change the selected row in the NSTableView, much like Finder behaves when you preview files with quick look. I have no clue as to how I could implement this. Any ideas?
Thanks.
Finally found what I was looking for and it's actually pretty simple.
Since my main view controller is also my delegate for the QLPreviewPanel, I added this:
extension ViewController: QLPreviewPanelDelegate {
func previewPanel(panel: QLPreviewPanel!, handleEvent event: NSEvent!) -> Bool {
let kc = event.keyCode
if (kc == 126 || kc == 125) { //up and down arrows
if event.type == NSEventType.KeyDown {
self.myTableView.keyDown(event) //send the event to the table
} else if event.type == NSEventType.KeyUp {
self.myTableView.keyUp(event)
}
return true
}
return false
}
}
Then in my table view delegate:
func tableViewSelectionDidChange(notification: NSNotification) {
guard myTableView.numberOfSelectedRows > 0 else {
if let panel = QLPreviewPanel.sharedPreviewPanel() {
if panel.visible {
panel.close()
}
}
return
}
if let panel = QLPreviewPanel.sharedPreviewPanel() {
if panel.visible {
panel.reloadData()
}
}
}
That's it! The QLPreviewPanelDataSource handles the rest.

Overriding description method in NSObject on swift

I'm getting one compiler error when I try to build one object in my xcode project. This is the code:
import UIKit
class Rectangulo: NSObject {
var ladoA : Int
var ladoB : Int
var area: Int {
get {
return ladoA*ladoB
}
}
init (ladoA:Int,ladoB:Int) {
self.ladoA = ladoA
self.ladoB = ladoB
}
func description() -> NSString {
return "El area es \(area)"
}
}
The error in compilation time is:
Rectangulo.swift:26:10: Method 'description()' with Objective-C selector 'description' conflicts with getter for 'description' from superclass 'NSObject' with the same Objective-C selector
What I need to do to override this function without issues?
description is a (computed) property of NSObjectProtocol, not a method.
Its Swift view returns a String, not NSString.
Since you are overriding a property of a superclass, you must specify override explicitly.
Together:
// main.swift:
import Foundation
class Rectangulo: NSObject {
var ladoA : Int
var ladoB : Int
var area: Int {
get {
return ladoA*ladoB
}
}
init (ladoA:Int,ladoB:Int) {
self.ladoA = ladoA
self.ladoB = ladoB
}
override var description : String {
return "El area es \(area)"
}
}
let r = Rectangulo(ladoA: 2, ladoB: 3)
print(r) // El area es 6

Resources