4
struct Line {

    NSUInteger x1;
    NSUInteger x2;
};

// ...

- (Line)visibleLine;

上面的代码显然不起作用,因为 Line 不是有效类型。有什么建议么?

4

4 回答 4

11

Objective-C 是基于 C 而不是 C++ 的。在C我们需要使用struct Line,而在 C++Line中很好。

你可以这样做:

struct {
    NSUInteger x1;
    NSUInteger x2;
} Line;

// ...

- (struct Line)visibleLine{
    
}

或者

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

// ...

- (Line)visibleLine;

以上是大多数 C 框架的首选。

还,

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

// ...

- (Line)visibleLine;
于 2013-03-14T10:04:50.630 回答
7
typedef struct {
    NSUInteger x1;
    NSUInteger x2;
} Line;

// ...

- (Line)visibleLine;

我最初(在其他答案之前)出于明确的原因提出了上述建议:这就是 Apple 在自己的代码中的做法。这不是唯一的方式,但它是 Apple 的标准方式。它从不将struct方法的原型放在其 API 的任何位置。

于 2013-03-14T10:03:21.223 回答
5

C中,struct LineLine是不同的。您需要使用别名struct Line来引用它Line。所以,

struct Line { /* ... */ };        // Make a struct.

typedef struct Line       Line;   // Make an alias.

这也可以一次写。

typedef struct Line { /* ... */ }        Line;

C++自动生成别名,但您应该将C++视为与C完全不同的语言。不要对他们的名字感到困惑。

于 2013-03-14T10:27:42.460 回答
3

您错过了关键字struct

- (struct Line)visibleLine;
于 2013-03-14T10:06:56.753 回答