Floyd Resler
Floyd Resler

Reputation: 1786

UIBezierPath Not Found On Multi-platform Project

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.

Upvotes: 0

Views: 401

Answers (1)

Fault
Fault

Reputation: 1339

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)
}

Upvotes: 1

Related Questions