在多平台项目上找不到 UIBezierPath
我在 Xcode 中创建了一个多平台游戏,UIBezierPath 给出了“在范围内找不到‘UIBezierPath’”错误。我确信我只是缺少导入一个套件,但我不知道是哪一个。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答:如果您尝试使用
UIBezierPath
您需要导入UIKit
更长的注释“多平台”:您的问题没有提供太多上下文..但是如果您的意思是您开始为 iOS 开发
UIBezierPath
,然后切换到不同的平台目标(例如:macOS),那么是的,您的许多类将无法工作。通常(但并非总是),macOS 上有一个等效类,它使用“NS”前缀而不是“UI”前缀。 (NS 是对“下一步”的传统引用)。因此,当您面向 macOS 时,请尝试使用AppKit
类(例如NSBezierPath
),而不是UIKit
类。警告:UI/NS 类经常有不同的实现。因此,有不同的构造函数、函数调用等是正常的。这意味着您通常必须使用上面的 if/elseif/endif 结构在代码中拥有两个实现。是的,这可能很乏味。
更新
这是在 macOS 和 iOS 中绘制椭圆的示例。注意:macOS 版本使用
CGMutablePath
(因为在 macOS 上没有像 iOS 那样从NSBezierPath
构建SKShapeNode
的简单方法)。如果您确实想使用 NSBezierPath,您也可以尝试这些技术用于将 NSBezierPath 转换为 CGPathshort answer: if you're trying to use
UIBezierPath
you need to importUIKit
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 usingAppKit
classes likeNSBezierPath
when you target macOS, instead ofUIKit
classes.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 aSKShapeNode
from anNSBezierPath
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