Post

Replies

Boosts

Views

Created

Drawing an NSImage using COCOA Drawing
I have the test code below which draws a box with some text in it. I would like to add an image to the drawing. The function getCompanyLogo returns an NSImage and I would like to then add this image to the drawing. I have read apple's archive doc at the Drawing with COCOA but do not see how to complete this. How is this done? Thanks import Cocoa @IBDesignable class invoiceSectionTop: NSView {     override func draw(_ dirtyRect: NSRect) {         super.draw(dirtyRect)         let context = NSGraphicsContext.current?.cgContext         let length = CGPoint(x: 200, y: 50)         let p1 = CGPoint(x: 0, y: 50)         let shape = "square"         context!.beginPath()         context!.move(to: p1)         if shape == "line" {             let pointEnd = CGPoint(x: p1.x + length.x, y: p1.y + length.y)             context!.addLine(to: pointEnd)         } else if shape == "square" {             let p2 = CGPoint(x: p1.x + length.x, y: p1.y)             let p3 = CGPoint(x: p1.x + length.x, y: p1.y + length.y)             let p4 = CGPoint(x: p1.x, y: p1.y + length.y)             context!.addLine(to: p2)             context!.addLine(to: p3)             context!.addLine(to: p4)             context!.addLine(to: p1)         }         context!.setStrokeColor(red: 1, green: 1, blue: 1, alpha: 1.0)         context!.setFillColor(red: 0, green: 1, blue: 0, alpha: 1)         context!.setLineWidth(2.0)         context!.strokePath()         let image = getCompanyLogo()      // getCompanyLogo returns a NSImage object    //     NSGraphicsContext.current?.NSIm.draw(cgimage!, in: dirtyRect)         let textColor = NSColor(calibratedRed: 1, green: 1, blue: 1, alpha: 1.0)         let textColorB = NSColor(calibratedRed: 1, green: 1, blue: 1, alpha: 0.0)         let rect = CGRect(x: 200, y: 200, width: 30, height: 130)         let paragraphStyle = NSMutableParagraphStyle()         paragraphStyle.alignment = .center         let attr = [NSAttributedString.Key.paragraphStyle: paragraphStyle, NSAttributedString.Key.foregroundColor: textColor, NSAttributedString.Key.backgroundColor: textColorB, NSAttributedString.Key.font:NSFont.init(name: "HelveticaNeue-Thin", size: 14)]         let q: NSString = "hello, world"         let center = CGPoint(x: 0, y: 50)         q.draw(at: center, withAttributes: attr as [NSAttributedString.Key : Any])     } }
1
0
1.2k
Aug ’22
How to sort NSTableView Date Column?
I have a date column in a macOS NSTableView column in YYYY-MM-DD format and when using the code below the sort does not sort way I want. When sorting ascending. It has the year correct but the month is in reverse order. See image below. How can I fix this?  func sortInvoices(invoiceColumn: String, ascending: Bool) {          enum SortOrder {             static let Date = "invoiceDateCreated"             static let Number = "invoicenumber"             static let Customer = "invoicecustomer"             static let Status = "invoicestatus"         }         switch invoiceColumn {         case SortOrder.Date:            invoices.sort { (p1, p2) -> Bool in                guard let id1 = p1.invoiceDateBilled, let id2 = p2.invoiceDateBilled else { return true }                if ascending {                    return id1 < id2                } else {                    return id2 < id1                }            }
1
0
921
Sep ’22
NSPredicate format for date comparison
I have the following code to search a tableView. The contains format works fine for string arguments however can't seem to find the correct format to work with date arguments.   @objc func invoiceSearch(sender:NSSearchField) {     //    print ("\(#function): \(sender.stringValue)")             let searchString = sender.stringValue             var predicate:NSPredicate = NSPredicate()             if searchString.isEmpty {                 invoices = self.backUpInvoices             }             else{       // search field contains data                 if (invoiceSearchField.cell as? NSSearchFieldCell)?.placeholderString == "All" {                     predicate = NSPredicate(format: "invoiceNumber contains %@ OR invoiceCustName contains %@ OR invoiceStatus contains %@ OR invoiceDateCreated >= %@",searchString,searchString,searchString,searchString)                 }                 else if (invoiceSearchField.cell as? NSSearchFieldCell)?.placeholderString == "invoice Number" {                     predicate = NSPredicate(format: "invoiceNumber contains %@",searchString)                 }                 else if (invoiceSearchField.cell as? NSSearchFieldCell)?.placeholderString == "invoice Customer"{                     predicate = NSPredicate(format: "invoiceCustName contains %@",searchString)                 }                 else if (invoiceSearchField.cell as? NSSearchFieldCell)?.placeholderString == "invoice Status"{                     predicate = NSPredicate(format: "invoiceStatus contains %@",searchString)                 }                 else if (invoiceSearchField.cell as? NSSearchFieldCell)?.placeholderString == "Invoice Date"{                       predicate = NSPredicate(format: "invoiceDateCreated >= %@", searchString as CVarArg)                 }                 invoices = (self.backUpInvoices as NSArray).filtered(using: predicate) as! [Invoice]             }             invoiceCount = "Found \(items.count ) Invoices"             invoiceStatusLabel.stringValue = invoiceCount             self.tableView.reloadData()     } InvoiceDateCreated is defined by the following within a NSObject class: @objc   var invoiceDateCreated: Date? = Date() The error I get when trying to search on date is 2022-09-26 09:14:36.638553-0500 CashToMe[9107:6838576] [General] -[NSTaggedPointerString objCType]: unrecognized selector sent to instance I have read through the following documentation Apple Doc for NSPredicate How can I fix this. Also, if there is a better/newer way to code this I am open to changing the code. Thanks
4
0
2k
Sep ’22
How to create bar chart with two or more bars per time period
Environment: Xcode 14.1 beta 3 on macOS Ventura Beta 9. No longer need this, I found example of this. struct LocationsChart: View {     var body: some View {         Chart {             ForEach(seriesData, id: \.city) { series in                 ForEach(series.data, id: \.weekday) {                     BarMark(                         x: .value("Weekday", $0.weekday, unit: .day),                         y: .value("Sales", $0.sales)                     )                 }                 .foregroundStyle(by: .value("City", series.city))                 .position(by: .value("City", series.city))             }         }     }     let seriesData = [         (             city: "Cupertino", data: [                 (weekday: date(year: 2022, month: 5, day: 2), sales: 54),                 (weekday: date(year: 2022, month: 5, day: 3), sales: 42),                 (weekday: date(year: 2022, month: 5, day: 4), sales: 88),                 (weekday: date(year: 2022, month: 5, day: 5), sales: 49),                 (weekday: date(year: 2022, month: 5, day: 6), sales: 42),                 (weekday: date(year: 2022, month: 5, day: 7), sales: 125),                 (weekday: date(year: 2022, month: 5, day: 8), sales: 67)             ]         ),         (             city: "San Francisco", data: [                 (weekday: date(year: 2022, month: 5, day: 2), sales: 81),                 (weekday: date(year: 2022, month: 5, day: 3), sales: 90),                 (weekday: date(year: 2022, month: 5, day: 4), sales: 52),                 (weekday: date(year: 2022, month: 5, day: 5), sales: 72),                 (weekday: date(year: 2022, month: 5, day: 6), sales: 84),                 (weekday: date(year: 2022, month: 5, day: 7), sales: 84),                 (weekday: date(year: 2022, month: 5, day: 8), sales: 137)             ]         )     ] } func date(year: Int, month: Int, day: Int = 1) -> Date {     Calendar.current.date(from: DateComponents(year: year, month: month, day: day)) ?? Date() }
0
0
1.1k
Oct ’22
How to Initialize a structure with in a class?
From the class below how do I initialize chartEntry for the structure array? As it is below chartEntry is outside scope of init? class ChartSeriesRow: ObservableObject {     var id = UUID()     var chartCategory: String     struct chartSeriesData{         var chartEntry: [ChartData]     }     struct ChartData: Codable, Identifiable, Hashable {         var id = UUID()         let chartMonth: String         let chartSales: String     }     init() {         chartCategory = ""         chartEntry = chartSeriesData: [ChartData.chartMonth: "01",  ChartData.chartSales: "10.0"]     }      }
3
0
887
Nov ’22
How to populate an array within a Class Instance?
I have the following code: class ChartSeriesRow: ObservableObject {     var id = UUID()     var chartCategory: String     struct ChartSeriesData {         var chartEntry: [ChartData]     }     var chartSeries : ChartSeriesData     struct ChartData: Codable, Identifiable, Hashable {         var id = UUID()         let chartMonth: String         let chartSales: String     }     init() {         chartCategory = ""         chartSeries = ChartSeriesData(chartEntry: [ChartData(chartMonth: "01", chartSales: "10.0") ] )     } } How do I populate ChartSeriesData? I thought I could just add the following: However coding this way there is no append method. extension ChartSeriesRow {     func addRow(chartRow: ChartSeriesData){          ChartSeriesRow.ChartSeriesData.chartEntry.append(contentsOf: chartRow)     } }
2
0
855
Nov ’22
How to add an IBAction to a macOS tableView Cell
I have a macOS tableview I have a textfield as a column along with an IBAction code to capture the data from this field. I wanted to add continuous spell check for this column . So, I decided to change the column from a TextField to a ScrollView to take advantage of the continuous spell check. Using Xcode 14 I changed the Cell to a custom Cell. The custom cell has a scrollview object as the object because I want to take advantage of the built in continuous spell checking instead of using a textfield. All is well with this implementation until I tried to add a IBAction code for when I enter text in the scrollview/textView field. There does not seem to be a way to capture the text? Am I trying to do something that can't be done or is there a way to add continuous spell check to a textField?
4
0
1.4k
Nov ’22
macOS Xcode 14.2 app applicationwillterminate function is not called
I want to put some code to be run when my app terminates but the following:        func applicationWillTerminate(_ aNotification: Notification) {             // Insert code here to tear down your application             print("Termination Code")         } in the AppDelegate class is not called, any ideas how to fix this issue. I saw this posts https://developer.apple.com/forums/thread/126418 but I do not have an info.plist. I think the info.plist is not longer needed. The following doc does not have much to say about this?? https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428522-applicationwillterminate
4
0
1.5k
Jan ’23
Progress Indicator not working as expected
I have a test project with a bar progress indicator create on Ventura with Xcode 14.3.1., It does not show the any animation until the @IBAction function completes. I want to show progress as it progresses not a just the end? Any ideas how to fix this? Below is my test project code.. // // ViewController.swift // ProgressTest // // Created by Bob on 6/5/23. // import Cocoa class ViewController: NSViewController { @IBOutlet weak var progressIndicator: NSProgressIndicator! // var progress: Double = 0.0 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override var representedObject: Any? { didSet { // Update the view, if already loaded. } } @IBAction func progressStart(_ sender: Any) { // Start the progress animation progressIndicator.startAnimation(self) // Do some work here... // Do some work here... var progress: Double = 0.0 for _ in 0..<100 { // Update the progress value in a loop progress += 1.0 progressIndicator.doubleValue = progress // progress = Double(i) // progressIndicator.increment(by: 10) // progressIndicator.doubleValue = progress // Do some work here... } // Stop the progress animation progressIndicator.stopAnimation(self) } } thanks Bob
3
0
1.4k
Jun ’23
Customize NSTableView alternate rows color
I would like customize the nstableview alternate row color as light blue instead og light gray. How is this done? thanks
Replies
0
Boosts
0
Views
610
Activity
Aug ’22
Drawing an NSImage using COCOA Drawing
I have the test code below which draws a box with some text in it. I would like to add an image to the drawing. The function getCompanyLogo returns an NSImage and I would like to then add this image to the drawing. I have read apple's archive doc at the Drawing with COCOA but do not see how to complete this. How is this done? Thanks import Cocoa @IBDesignable class invoiceSectionTop: NSView {     override func draw(_ dirtyRect: NSRect) {         super.draw(dirtyRect)         let context = NSGraphicsContext.current?.cgContext         let length = CGPoint(x: 200, y: 50)         let p1 = CGPoint(x: 0, y: 50)         let shape = "square"         context!.beginPath()         context!.move(to: p1)         if shape == "line" {             let pointEnd = CGPoint(x: p1.x + length.x, y: p1.y + length.y)             context!.addLine(to: pointEnd)         } else if shape == "square" {             let p2 = CGPoint(x: p1.x + length.x, y: p1.y)             let p3 = CGPoint(x: p1.x + length.x, y: p1.y + length.y)             let p4 = CGPoint(x: p1.x, y: p1.y + length.y)             context!.addLine(to: p2)             context!.addLine(to: p3)             context!.addLine(to: p4)             context!.addLine(to: p1)         }         context!.setStrokeColor(red: 1, green: 1, blue: 1, alpha: 1.0)         context!.setFillColor(red: 0, green: 1, blue: 0, alpha: 1)         context!.setLineWidth(2.0)         context!.strokePath()         let image = getCompanyLogo()      // getCompanyLogo returns a NSImage object    //     NSGraphicsContext.current?.NSIm.draw(cgimage!, in: dirtyRect)         let textColor = NSColor(calibratedRed: 1, green: 1, blue: 1, alpha: 1.0)         let textColorB = NSColor(calibratedRed: 1, green: 1, blue: 1, alpha: 0.0)         let rect = CGRect(x: 200, y: 200, width: 30, height: 130)         let paragraphStyle = NSMutableParagraphStyle()         paragraphStyle.alignment = .center         let attr = [NSAttributedString.Key.paragraphStyle: paragraphStyle, NSAttributedString.Key.foregroundColor: textColor, NSAttributedString.Key.backgroundColor: textColorB, NSAttributedString.Key.font:NSFont.init(name: "HelveticaNeue-Thin", size: 14)]         let q: NSString = "hello, world"         let center = CGPoint(x: 0, y: 50)         q.draw(at: center, withAttributes: attr as [NSAttributedString.Key : Any])     } }
Replies
1
Boosts
0
Views
1.2k
Activity
Aug ’22
How to sort NSTableView Date Column?
I have a date column in a macOS NSTableView column in YYYY-MM-DD format and when using the code below the sort does not sort way I want. When sorting ascending. It has the year correct but the month is in reverse order. See image below. How can I fix this?  func sortInvoices(invoiceColumn: String, ascending: Bool) {          enum SortOrder {             static let Date = "invoiceDateCreated"             static let Number = "invoicenumber"             static let Customer = "invoicecustomer"             static let Status = "invoicestatus"         }         switch invoiceColumn {         case SortOrder.Date:            invoices.sort { (p1, p2) -> Bool in                guard let id1 = p1.invoiceDateBilled, let id2 = p2.invoiceDateBilled else { return true }                if ascending {                    return id1 < id2                } else {                    return id2 < id1                }            }
Replies
1
Boosts
0
Views
921
Activity
Sep ’22
NSPredicate format for date comparison
I have the following code to search a tableView. The contains format works fine for string arguments however can't seem to find the correct format to work with date arguments.   @objc func invoiceSearch(sender:NSSearchField) {     //    print ("\(#function): \(sender.stringValue)")             let searchString = sender.stringValue             var predicate:NSPredicate = NSPredicate()             if searchString.isEmpty {                 invoices = self.backUpInvoices             }             else{       // search field contains data                 if (invoiceSearchField.cell as? NSSearchFieldCell)?.placeholderString == "All" {                     predicate = NSPredicate(format: "invoiceNumber contains %@ OR invoiceCustName contains %@ OR invoiceStatus contains %@ OR invoiceDateCreated >= %@",searchString,searchString,searchString,searchString)                 }                 else if (invoiceSearchField.cell as? NSSearchFieldCell)?.placeholderString == "invoice Number" {                     predicate = NSPredicate(format: "invoiceNumber contains %@",searchString)                 }                 else if (invoiceSearchField.cell as? NSSearchFieldCell)?.placeholderString == "invoice Customer"{                     predicate = NSPredicate(format: "invoiceCustName contains %@",searchString)                 }                 else if (invoiceSearchField.cell as? NSSearchFieldCell)?.placeholderString == "invoice Status"{                     predicate = NSPredicate(format: "invoiceStatus contains %@",searchString)                 }                 else if (invoiceSearchField.cell as? NSSearchFieldCell)?.placeholderString == "Invoice Date"{                       predicate = NSPredicate(format: "invoiceDateCreated >= %@", searchString as CVarArg)                 }                 invoices = (self.backUpInvoices as NSArray).filtered(using: predicate) as! [Invoice]             }             invoiceCount = "Found \(items.count ) Invoices"             invoiceStatusLabel.stringValue = invoiceCount             self.tableView.reloadData()     } InvoiceDateCreated is defined by the following within a NSObject class: @objc   var invoiceDateCreated: Date? = Date() The error I get when trying to search on date is 2022-09-26 09:14:36.638553-0500 CashToMe[9107:6838576] [General] -[NSTaggedPointerString objCType]: unrecognized selector sent to instance I have read through the following documentation Apple Doc for NSPredicate How can I fix this. Also, if there is a better/newer way to code this I am open to changing the code. Thanks
Replies
4
Boosts
0
Views
2k
Activity
Sep ’22
How to create bar chart with two or more bars per time period
Environment: Xcode 14.1 beta 3 on macOS Ventura Beta 9. No longer need this, I found example of this. struct LocationsChart: View {     var body: some View {         Chart {             ForEach(seriesData, id: \.city) { series in                 ForEach(series.data, id: \.weekday) {                     BarMark(                         x: .value("Weekday", $0.weekday, unit: .day),                         y: .value("Sales", $0.sales)                     )                 }                 .foregroundStyle(by: .value("City", series.city))                 .position(by: .value("City", series.city))             }         }     }     let seriesData = [         (             city: "Cupertino", data: [                 (weekday: date(year: 2022, month: 5, day: 2), sales: 54),                 (weekday: date(year: 2022, month: 5, day: 3), sales: 42),                 (weekday: date(year: 2022, month: 5, day: 4), sales: 88),                 (weekday: date(year: 2022, month: 5, day: 5), sales: 49),                 (weekday: date(year: 2022, month: 5, day: 6), sales: 42),                 (weekday: date(year: 2022, month: 5, day: 7), sales: 125),                 (weekday: date(year: 2022, month: 5, day: 8), sales: 67)             ]         ),         (             city: "San Francisco", data: [                 (weekday: date(year: 2022, month: 5, day: 2), sales: 81),                 (weekday: date(year: 2022, month: 5, day: 3), sales: 90),                 (weekday: date(year: 2022, month: 5, day: 4), sales: 52),                 (weekday: date(year: 2022, month: 5, day: 5), sales: 72),                 (weekday: date(year: 2022, month: 5, day: 6), sales: 84),                 (weekday: date(year: 2022, month: 5, day: 7), sales: 84),                 (weekday: date(year: 2022, month: 5, day: 8), sales: 137)             ]         )     ] } func date(year: Int, month: Int, day: Int = 1) -> Date {     Calendar.current.date(from: DateComponents(year: year, month: month, day: day)) ?? Date() }
Replies
0
Boosts
0
Views
1.1k
Activity
Oct ’22
Is it possible to have secondary y axis with different scale?
Environment : macOS Ventura Xcode beta 14.1: I have the following chart I was using to learn about SwifUI Charts: I wanted to have different scales on the leading and trailing yaxis as well as have the first 3 bars to use leading scale and the last two bars use the trailing scale. Can this be done? If so, how? Thanks
Replies
1
Boosts
0
Views
1.2k
Activity
Oct ’22
structured editing popover with Xcode 14.1 beta 3
I don't see the structured editing popover when I command right click on an IOS SwiftUI project when I create a new project with just "Hello World" like I see in Xcode 13. Am I missing some thing or is this a bug? Thanks
Replies
6
Boosts
0
Views
2.1k
Activity
Oct ’22
How to Initialize a structure with in a class?
From the class below how do I initialize chartEntry for the structure array? As it is below chartEntry is outside scope of init? class ChartSeriesRow: ObservableObject {     var id = UUID()     var chartCategory: String     struct chartSeriesData{         var chartEntry: [ChartData]     }     struct ChartData: Codable, Identifiable, Hashable {         var id = UUID()         let chartMonth: String         let chartSales: String     }     init() {         chartCategory = ""         chartEntry = chartSeriesData: [ChartData.chartMonth: "01",  ChartData.chartSales: "10.0"]     }      }
Replies
3
Boosts
0
Views
887
Activity
Nov ’22
How to populate an array within a Class Instance?
I have the following code: class ChartSeriesRow: ObservableObject {     var id = UUID()     var chartCategory: String     struct ChartSeriesData {         var chartEntry: [ChartData]     }     var chartSeries : ChartSeriesData     struct ChartData: Codable, Identifiable, Hashable {         var id = UUID()         let chartMonth: String         let chartSales: String     }     init() {         chartCategory = ""         chartSeries = ChartSeriesData(chartEntry: [ChartData(chartMonth: "01", chartSales: "10.0") ] )     } } How do I populate ChartSeriesData? I thought I could just add the following: However coding this way there is no append method. extension ChartSeriesRow {     func addRow(chartRow: ChartSeriesData){          ChartSeriesRow.ChartSeriesData.chartEntry.append(contentsOf: chartRow)     } }
Replies
2
Boosts
0
Views
855
Activity
Nov ’22
How to add an IBAction to a macOS tableView Cell
I have a macOS tableview I have a textfield as a column along with an IBAction code to capture the data from this field. I wanted to add continuous spell check for this column . So, I decided to change the column from a TextField to a ScrollView to take advantage of the continuous spell check. Using Xcode 14 I changed the Cell to a custom Cell. The custom cell has a scrollview object as the object because I want to take advantage of the built in continuous spell checking instead of using a textfield. All is well with this implementation until I tried to add a IBAction code for when I enter text in the scrollview/textView field. There does not seem to be a way to capture the text? Am I trying to do something that can't be done or is there a way to add continuous spell check to a textField?
Replies
4
Boosts
0
Views
1.4k
Activity
Nov ’22
How to change the text color for SwiftUI Chart's legend text?
For a macOS app using Xcode 14.1. I have a bar chart with two bars for each interval. See screen chart below. I want to change the "Invoice" and Payments" text I can't seem to find a modifier for the Legend text. How do you change the text color for SwiftUI Chart's legend text?
Replies
3
Boosts
0
Views
3.4k
Activity
Nov ’22
macOS Xcode 14.2 app applicationwillterminate function is not called
I want to put some code to be run when my app terminates but the following:        func applicationWillTerminate(_ aNotification: Notification) {             // Insert code here to tear down your application             print("Termination Code")         } in the AppDelegate class is not called, any ideas how to fix this issue. I saw this posts https://developer.apple.com/forums/thread/126418 but I do not have an info.plist. I think the info.plist is not longer needed. The following doc does not have much to say about this?? https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428522-applicationwillterminate
Replies
4
Boosts
0
Views
1.5k
Activity
Jan ’23
Xcode 14.2 Find and Replace in Project not working
The find and replace in project is not working for me in Xcode 14.2 on Ventura 13.2. When I enter the find text as well as the replace text the "Replace" and "Replace All" buttons remained grayed out. Is this a bug with Xcode or am I missing something? Please advise.
Replies
0
Boosts
1
Views
638
Activity
Jan ’23
Progress Indicator not working as expected
I have a test project with a bar progress indicator create on Ventura with Xcode 14.3.1., It does not show the any animation until the @IBAction function completes. I want to show progress as it progresses not a just the end? Any ideas how to fix this? Below is my test project code.. // // ViewController.swift // ProgressTest // // Created by Bob on 6/5/23. // import Cocoa class ViewController: NSViewController { @IBOutlet weak var progressIndicator: NSProgressIndicator! // var progress: Double = 0.0 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override var representedObject: Any? { didSet { // Update the view, if already loaded. } } @IBAction func progressStart(_ sender: Any) { // Start the progress animation progressIndicator.startAnimation(self) // Do some work here... // Do some work here... var progress: Double = 0.0 for _ in 0..<100 { // Update the progress value in a loop progress += 1.0 progressIndicator.doubleValue = progress // progress = Double(i) // progressIndicator.increment(by: 10) // progressIndicator.doubleValue = progress // Do some work here... } // Stop the progress animation progressIndicator.stopAnimation(self) } } thanks Bob
Replies
3
Boosts
0
Views
1.4k
Activity
Jun ’23
Companion Xcode project
Don't see a link to download the Xcode project, starter and complete? How can I get it? Thanks Bob
Replies
2
Boosts
1
Views
810
Activity
Jun ’23