0

I created a multi-platform game in Xcode and UIBezierPath gives a "Cannot find 'UIBezierPath' in scope" error. I'm sure I'm just missing importing a kit but I don't know which one.

Floyd Resler
  • 1,786
  • 3
  • 22
  • 41

1 Answers1

1

short answer: if you're trying to use UIBezierPath you need to import UIKit

longer note re "multi-platform": your question doesn't provide much context.. but if you mean you started developing a UIBezierPath for iOS, and then switched to a different platform target (ex: macOS), then yes many of your classes won't work. often, but not always, there is simply an equivalent class for macOS that uses the "NS" prefix instead of the "UI" prefix. (NS is a legacy reference to "Next Step"). so try using AppKit classes like NSBezierPath when you target macOS, instead of UIKit classes.

#if os(macOS)
import AppKit
#elseif os(iOS)
import UIKit
#endif 

warning: UI/NS classes frequently have different implementation. so it's normal to have different constructors, function calls, etc. meaning you often have to have two implementations in code using the above if/elseif/endif structure. yes this can be tedious.

UPDATE

Here is an example for drawing an ellipse in both macOS and iOS. note: the macOS version uses a CGMutablePath (since there's not a simple way to construct a SKShapeNode from an NSBezierPath on macOS like there is on iOS). if you do want to use NSBezierPath, you might also try these techniques for converting NSBezierPath to CGPath

override func didMove(to view: SKView) {
    let rect = CGRect(x: 0, y: 0, width: 200, height: 200)
    
    #if os(macOS)
    let path = CGMutablePath(ellipseIn:rect, transform:nil)
    let shape = SKShapeNode(path:path)
    
    #elseif os(iOS)
    let path = UIBezierPath(ovalIn: rect)
    let shape = SKShapeNode(path: path.cgPath)
    #endif
    
    addChild(shape)
}
Fault
  • 1,115
  • 1
  • 7
  • 11
  • That helped! I changed UIBezierPath to NSBezierPath. However, the path property of SKShapeNode won't accept NSBezierPath. – Floyd Resler Mar 03 '22 at 21:29
  • oops yeah in this case the implementations are probably too different to just swap UI for NS. updated example using `CGMutablePath` for macOS instead. – Fault Mar 04 '22 at 20:38