如何制作像 CGRectMake (iphone) 这样的结构生成器

发布于 2024-08-29 12:38:16 字数 899 浏览 9 评论 0原文

我有一个带有两个 CGFloat 的 struct HLRange,

struct HOLRange
{
CGFloat min;
CGFloat max;
};
typedef struct HOLRange HOLRange;

但是如何制作像 HLRangeMake(1,2); 这样的函数? ..像CGRectMake?

--编辑--

我的头文件

#import <Foundation/Foundation.h>

struct HOLRange
{
    CGFloat min;
    CGFloat max;
};
typedef struct HOLRange HOLRange;

HOLRange HOLRangeMake(CGFloat min, CGFloat max) {
    HOLRange range;
    range.min = min;
    range.max = max;
    return range;
 }

@interface Structs : NSObject {

}

@end


error message: ld: duplicate symbol _HOLRangeMake in             /Users/Documents/projects/iphone/test/catalog/base1/build/base1.build/Debug-iphoneos/base1.build/Objects-normal/armv6/base1AppDelegate.o and /Users/Documents/projects/iphone/test/catalog/base1/build/base1.build/Debug-iphoneos/base1.build/Objects-normal/armv6/main.o

i have a struct HLRange with two CGFloat's

struct HOLRange
{
CGFloat min;
CGFloat max;
};
typedef struct HOLRange HOLRange;

but how do i make a function like HLRangeMake(1,2); .. like CGRectMake?

--EDIT--

my header file

#import <Foundation/Foundation.h>

struct HOLRange
{
    CGFloat min;
    CGFloat max;
};
typedef struct HOLRange HOLRange;

HOLRange HOLRangeMake(CGFloat min, CGFloat max) {
    HOLRange range;
    range.min = min;
    range.max = max;
    return range;
 }

@interface Structs : NSObject {

}

@end


error message: ld: duplicate symbol _HOLRangeMake in             /Users/Documents/projects/iphone/test/catalog/base1/build/base1.build/Debug-iphoneos/base1.build/Objects-normal/armv6/base1AppDelegate.o and /Users/Documents/projects/iphone/test/catalog/base1/build/base1.build/Debug-iphoneos/base1.build/Objects-normal/armv6/main.o

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱你是孤单的心事 2024-09-05 12:38:16
HOLRange HLRangeMake(CGFloat min, CGFloat max) {
    HOLRange range;
    range.min = min;
    range.max = max;
    return range;
}
HOLRange HLRangeMake(CGFloat min, CGFloat max) {
    HOLRange range;
    range.min = min;
    range.max = max;
    return range;
}
弥枳 2024-09-05 12:38:16

您可以在 CGGeometry.h 中查看 CGRectMake 源代码,以便您可以执行相同的操作:

CG_INLINE CGRect
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
  CGRect rect;
  rect.origin.x = x; rect.origin.y = y;
  rect.size.width = width; rect.size.height = height;
  return rect;
}

编辑: 您必须将函数定义为内联函数或将其实现移至 .m 文件。当您的函数在导入 HoleRange.h (?) 标头的每个编译单元中定义时,您会收到链接器错误。

You can see CGRectMake source in CGGeometry.h so you can do the same:

CG_INLINE CGRect
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
  CGRect rect;
  rect.origin.x = x; rect.origin.y = y;
  rect.size.width = width; rect.size.height = height;
  return rect;
}

Edit: You must either define your function as inline or move its implementation to .m file. You're getting linker errors as you function becomes defined in every compile unit that imports HoleRange.h (?) header.

终遇你 2024-09-05 12:38:16

旧帖子。不过,我想为未来的观众分享我解决这个问题的方法。

typdef struct _HOLRange {
    CGFloat min;
    CGFloat max;
} HOLRange;

static inline HOLRange(CGFloat min, CGFloat max) {
    return (HOLRange) {min, max};
}

您可以像这样定义您的 Stuct 和 Make 函数。又短又快。

Old post. However, I'd like to share my method of resolving this problem for future viewers.

typdef struct _HOLRange {
    CGFloat min;
    CGFloat max;
} HOLRange;

static inline HOLRange(CGFloat min, CGFloat max) {
    return (HOLRange) {min, max};
}

You can define your stuct and Make function like this. Short and quick.

初懵 2024-09-05 12:38:16

我更喜欢这个格式。它在视觉上更有意义并且看起来更“正确”。

typedef struct {
    CGFloat min;
    CGFloat max;
} HOLRange;

static inline HOLRange HOLRangeMake(CGFloat min, CGFloat max) {
    HOLRange range;
    range.min = min;
    range.max = max;
    return range;
}

I like the format of this better. It makes more sense visually and seems more "correct."

typedef struct {
    CGFloat min;
    CGFloat max;
} HOLRange;

static inline HOLRange HOLRangeMake(CGFloat min, CGFloat max) {
    HOLRange range;
    range.min = min;
    range.max = max;
    return range;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文