Why are NSRect and CGRect incompatible types?

Ok, I fear the error:

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

Here is what it makes:

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

Definition:

 - (id)initWithFrame:(NSRect)frameRect; 

So, the first argument is NSRect, let's check it out:

 typedef CGRect NSRect; 

How can this cause an error? They are the same types in different ways!

+7
objective-c cocoa
source share
3 answers

NSRect is the same type as CGRect if for iOS, for 64-bit Mac architecture or 32-bit Mac architecture with the macro NS_BUILD_32_LIKE_64 is defined as 1 on the command line or in the prefix header.

Or, to indicate NSGeometry.h:

 #if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64 
+9
source share

In addition to the zoul answer, I thought I mentioned two helper functions (macros?) Of Apple (starting at 10.5):

 NSRect NSRectFromCGRect(CGRect cgrect) CGRect NSRectToCGRect(NSRect nsrect) 
+11
source share

Where did you get the definition of typedef NSRect ? According to the docs, NSRect is defined as follows:

 typedef struct _NSRect { NSPoint origin; NSSize size; } NSRect; 

In other words, it is a structure that looks like a CGRect (but not a simple typedef ). Theres has already written something about the conversions between CGRect and NSRect .

+3
source share

All Articles