struct Line {
NSUInteger x1;
NSUInteger x2;
};
// ...
- (Line)visibleLine;
上面的代码显然不起作用,因为 Line 不是有效类型。有什么建议么?
struct Line {
NSUInteger x1;
NSUInteger x2;
};
// ...
- (Line)visibleLine;
上面的代码显然不起作用,因为 Line 不是有效类型。有什么建议么?
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;
typedef struct {
NSUInteger x1;
NSUInteger x2;
} Line;
// ...
- (Line)visibleLine;
我最初(在其他答案之前)出于明确的原因提出了上述建议:这就是 Apple 在自己的代码中的做法。这不是唯一的方式,但它是 Apple 的标准方式。它从不将struct
方法的原型放在其 API 的任何位置。
在C中,struct Line
和Line
是不同的。您需要使用别名struct Line
来引用它Line
。所以,
struct Line { /* ... */ }; // Make a struct.
typedef struct Line Line; // Make an alias.
这也可以一次写。
typedef struct Line { /* ... */ } Line;
C++自动生成别名,但您应该将C++视为与C完全不同的语言。不要对他们的名字感到困惑。
您错过了关键字struct。
- (struct Line)visibleLine;