Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C How do I return a struct datatype?

Tags:

objective-c

struct Line {

    NSUInteger x1;
    NSUInteger x2;
};

// ...

- (Line)visibleLine;

The code above obviously doesn't work because Line is not a valid type. Any suggestions?

like image 998
Nick Keroulis Avatar asked Nov 29 '22 02:11

Nick Keroulis


1 Answers

Objective-C is a based on C not C++. In C we need to use struct Line, while in C++ Line is fine.

You can do this as :

struct {
    NSUInteger x1;
    NSUInteger x2;
} Line;

// ...

- (struct Line)visibleLine{
    
}

OR

struct Line {
    NSUInteger x1;
    NSUInteger x2;
};
typedef struct Line Line;

// ...

- (Line)visibleLine;

Above is preferred by most C frameworks.

Also,

typedef struct {
    NSUInteger x1;
    NSUInteger x2;
} Line;

// ...

- (Line)visibleLine;
like image 190
Anoop Vaidya Avatar answered Dec 10 '22 10:12

Anoop Vaidya