objective-c - 为什么 NSRect 和 CGRect 是不兼容的类型?

标签 objective-c cocoa

好的,我对错误感到困惑:

error: incompatible type for argument 1 of 'initWithFrame:'

这是造成它的原因:

operationLabel = [[NSTextField alloc] initWithFrame:CGRectMake(0, self.frame.size.height / 2 - (40 * 3), self.frame.size.width, 100)];

定义是:

- (id)initWithFrame:(NSRect)frameRect;

所以第一个参数是 NSRect,让我们检查一下:

typedef CGRect NSRect;

它怎么会导致错误呢? 它们是相同的类型,但名称不同!

最佳答案

除了 zoul 的回答之外,我想我会提到 Apple(从 10.5 开始)的两个辅助函数(宏?):

NSRect NSRectFromCGRect(CGRect cgrect)
CGRect NSRectToCGRect(NSRect nsrect)

关于objective-c - 为什么 NSRect 和 CGRect 是不兼容的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3538347/

相关文章:

ios - 如何获取 uiscrollView 位于 scrollIndicator 的元素

objective-c - 无法从 NSValue 的 mutableArray 中提取 CGPoint 值

objective-c - 编译器错误 : "initializer element is not a compile-time constant"

objective-c - OS X (>=10.6) 上类似 Twitter 客户端的 NSCollectionView 实现?

objective-c - 仅资源 NSBundle : is this kosher?

macos - 如何将图像嵌入 NSAttributedString 中?

iphone - 从 NSString 中的某个字符获取子字符串

ios - 如何在 Objective C 中返回 2 个值?

cocoa - glEnableVertexAttribArray 给出无效操作

objective-c - 从 shell 脚本读取实时输出并更新进度条