I am new to Swift. The Apple website has some great resources. This one shows how to draw in an ios playground. Crustacean.playground. I found it in the Sample Code {} Crustacean.playground link on this site: https://developer.apple.com/swift/resources/. I tried to get a simple app to do the same drawings in macOS (not ios) and failed. I am wondering it somebody can please help me to create a simple Cocoa project that renders dquivalent images in Cocoa. What would really be helpful would be the code required for each file so I could create the files in a new Cocoa project and just paste the code and see how it works.
The code below was taken from the Apple playground. I got it frrom Project Navigator | Sources | CoreGraphicsDiagramView.swift. When I attempt to convert it to a Cocoa macOS project I get all kinds of errors. Thank you.
import UIKit
import PlaygroundSupport
let drawingArea = CGRect(x: 0.0, y: 0.0, width: 375.0, height: 667.0)
/
/
/
class CoreGraphicsDiagramView : UIView {
override func draw(_ bounds: CGRect) {
let context = UIGraphicsGetCurrentContext()!
context.saveGState()
draw(context)
let lightBlue = CGColor(
colorSpace: CGColorSpaceCreateDeviceRGB(), components: [0.222, 0.617, 0.976, 1.0])!
context.setStrokeColor(lightBlue)
context.setLineWidth(3)
context.strokePath()
context.restoreGState()
}
var draw: (CGContext)->() = { _ in () }
}
/
/
/
public func showCoreGraphicsDiagram(title: String, draw: @escaping (CGContext)->()) {
let diagramView = CoreGraphicsDiagramView(frame: drawingArea)
diagramView.draw = draw
diagramView.setNeedsDisplay()
PlaygroundPage.current.liveView = diagramView
}import UIKit
import PlaygroundSupport
internal let drawingArea: <<error type>>
/
/
/
internal class CoreGraphicsDiagramView : UIView {
override internal func draw(_ bounds: CGRect) -> <<error type>>
internal var draw: (CGContext) -> ()
}
/
/
/
public func showCoreGraphicsDiagram(title: String, draw: (CGContext) -> ()) -> <<error type>>
You probably should avoid trying to translate your existing code. Some of the underlying logic will be useful to have tried, but on macOS your code exists within a larger ecosystem, and you need to get a bit of experience with how that works.
The macOS equivalent of iOS's UIKit is AppKit, which is included as part of the "import Cocoa" that Claude suggested. Note that macOS equivalents of similar types have NS… names instead of UI… names.
However, I suggest you proceed as follows:
1. Create a new single-window macOS app project from the Xcode template. Build and run to see that you get a single window with nothing in it.
2. Create a new NSView subclass in a new Swift file. There should be a template for this in the dialog that comes up when you create a new source file. Don't add any code yet.
3. Go to your storyboard, and find the content view of the window. In the attribute inspector, look at the class name. It should say (grayed out) NSView. Change this to the name of your NSView subclass. Build and run to make sure there are no errors.
4. Go back to your NSView subclass source file. The template should have put an empty "draw" function there. Now you can start adding drawing commands.
Don't try to muck about with the CGContext directly. That was only necessary in the playground because you had to carefully co-exist with the playground's built-in functionality. In your subclass, you control what's drawn. Try creating a NSBezierPath for one of its standard shapes. (Consult the NSBezierPath class documentation for the various methods available.) Use the NSColor class to access standard colors, or create your own.
Use NSColor's set, setStroke and setFill methods (in your "draw(:)" method) to set up the colors you want. Use NSBezierPath's fill and stroke methods to actually draw.
That should get you started. Note that for drawing purposes, you are NOT going to add code to the app delegate. That's not what it's for.