Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using objective c types from C

Tags:

c

objective-c

I there any way to use objective c types for example NSRange, CGRect, etc. (they are structs) from C?

I'm using objective c runtime to access objective c classes, but some methods returns and accepts objective c types which are structs, and my problem is how to use returned objective c struct from C?

like image 223
user232343 Avatar asked Jun 27 '13 14:06

user232343


2 Answers

Provided that you import the correct headers, of course you can, being those plain and simple C structs. Specifically you will find NSRange defined in <Foundation/NSRange.h> and CGRect in <CoreGraphics/CGGeometry.h>.

There's nothing of Objective-C-specific in NSRange, CGRect and similar structs.

like image 161
Gabriele Petronella Avatar answered Nov 18 '22 04:11

Gabriele Petronella


NSRange can be simple used by define:

typedef struct {
    unsigned long location;
    unsigned long length;
} NSRange;

CGRect is a little bit tricky:

struct CGRect {
   CGPoint origin;
   CGSize size;
};
typedef struct CGRect CGRect;

struct CGPoint {
   CGFloat x;
   CGFloat y;
};
typedef struct CGPoint CGPoint;

struct CGSize {
   CGFloat width;
   CGFloat height;
};
typedef struct CGSize CGSize;

typedef float CGFloat;    // 32-bit
typedef double CGFloat; // 64-bit
like image 2
Leo Chapiro Avatar answered Nov 18 '22 06:11

Leo Chapiro