The problem seems to be with the variable invoiceDateCreated because it is a foundation date (@objc) and not NSDate object. Any ideas how to change this to get it working?
Post
Replies
Boosts
Views
Activity
Either of the following two lines can cause the error:
predicate = NSPredicate(format: "invoiceNumber contains %@ OR invoiceCustName contains %@ OR invoiceStatus contains %@ OR invoiceDateCreated >= %@",searchString,searchString,searchString,searchString)
predicate = NSPredicate(format: "invoiceDateCreated >= %@", searchString as CVarArg)
The problem I am having is with the operator >=, I not sure how to filter my table with the date type. The CONTAINS operator only works with strings. I want to be able to filter on a date type variable.
Also, is this meant to say items?
invoiceCount = "Found (items.count ) Invoices" ... No, it should say invoices - good catch
I changed the code to convert date to an integer and that worked for me as well changed to a different column.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYYMMdd"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
invoices.sort { (p1, p2) -> Bool in
let p1Date: Int? = Int(dateFormatter.string(from: p1.invoiceDateCreated!))
let p2Date: Int? = Int(dateFormatter.string(from: p2.invoiceDateCreated!))
guard let id1 = p1Date, let id2 = p2Date else { return true }
if ascending {
return id1 < id2
} else {
return id2 < id1
}
}
Found the following code on gitHub
extension NSImage {
/// Create a CIImage using the best representation available
///
/// - Returns: Converted image, or nil
func asCIImage() -> CIImage? {
if let cgImage = self.asCGImage() {
return CIImage(cgImage: cgImage)
}
return nil
}
/// Create a CGImage using the best representation of the image available in the NSImage for the image size
///
/// - Returns: Converted image, or nil
func asCGImage() -> CGImage? {
var rect = NSRect(origin: CGPoint(x: 0, y: 0), size: self.size)
return self.cgImage(forProposedRect: &rect, context: NSGraphicsContext.current, hints: nil)
}
}
extension CIImage {
/// Create a CGImage version of this image
///
/// - Returns: Converted image, or nil
func asCGImage(context: CIContext? = nil) -> CGImage? {
let ctx = context ?? CIContext(options: nil)
return ctx.createCGImage(self, from: self.extent)
}
/// Create an NSImage version of this image
/// - Parameters:
/// - pixelSize: The number of pixels in the result image. For a retina image (for example), pixelSize is double repSize
/// - repSize: The number of points in the result image
/// - Returns: Converted image, or nil
#if os(macOS)
@available(macOS 10, *)
func asNSImage(pixelsSize: CGSize? = nil, repSize: CGSize? = nil) -> NSImage? {
let rep = NSCIImageRep(ciImage: self)
if let ps = pixelsSize {
rep.pixelsWide = Int(ps.width)
rep.pixelsHigh = Int(ps.height)
}
if let rs = repSize {
rep.size = rs
}
let updateImage = NSImage(size: rep.size)
updateImage.addRepresentation(rep)
return updateImage
}
#endif
}
extension CGImage {
/// Create a CIImage version of this image
///
/// - Returns: Converted image, or nil
func asCIImage() -> CIImage {
return CIImage(cgImage: self)
}
/// Create an NSImage version of this image
///
/// - Returns: Converted image, or nil
func asNSImage() -> NSImage? {
return NSImage(cgImage: self, size: .zero)
}
}
Added the following two lines to get what I needed
let cgimage: CGImage? = image.asCGImage() // Here comes the picture
NSGraphicsContext.current?.cgContext.draw(cgimage!, in: dirtyRect)
I got the answer to this from apple support. It seems the tableView must part of a scrollView for the headers to appear.
So I removed any reference to the explicit creation of a table header view and
I added the following code to get this working:
let scrollView = NSScrollView()
scrollView.documentView = tableViewForPrint
stackView.addArrangedSubview(scrollView)
Maybe another way to accomplish what I want is...If when you change focus of a window an event is posted in the now focused window gets control. In other words can the focused window tell it just got the focus. Is there way to do this?
You are right it compiled fine with the reference and creating and instance, but did throw an exception due to the IBAction used some CustViewController properties.
So, using notification I was able to get it working.
Also, why do you think the delegation may be the cleanest way?
Thanks for the help.
Thanks for the reply!
I tried it and I get the following:
let printAction
CustViewController.printCustomers(self)
Instance member 'printCustomers' cannot be used on type 'CustViewController': did you mean to use a value of this type instead?
The window controller I am referring to is the main window where as the view controller is from a separate/different window controller/view controller. My objective is to have a main printer control action which would control which printout would be generated when a CMD-P is pressed.
One thing from another post mentioned I would need an instance just with contentViewController. Not sure how to code this as well?
Thanks I will give this a try!
I installed 13.2.1 but still don't see any packages in the GitHub source.
Thanks, for your answer.. I have decided to save the image to a file.
The following is the correct answer. It was supplied by Claude31 as well as an offline template he sent me.
The way I do it is to design the report as a view in code.
Schematically, it will be:
I create a PrintView and house all the drawing in the draw function
class PrintView: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect) // Drawing code here. }}
Then, to print, I call (from the handler of Print menu command for instance or from a print button in a view)
let aSize = NSSize(width: printInfo.paperSize.width - 20, height: totalHeight)
let printView = PrintView(frame: NSRect(origin: NSPoint(x: 1, y: 10), size: aSize)) // a full page, depending on orientation
and call the printPanel
let printOp = NSPrintOperation(view: printView, printInfo: printInfo)
Thanks Claude for sending me the additional examples. Very useful!
Claude,
Makes sense what you posted, I was hoping there was a design tool to create the drawing code.
So, does that mean thinks like Font, Font Size, colors, Graphics etc. are created in PrintView Class with code? And I guess the only way to see the report is to print it?
Do you have any code of a PrintView class you could share?
Thanks
Thanks Claude.. I got it working like I want using what I suggested above. Very similar to what you had suggested.