我以前没见过的CGRect语法 [英] CGRect syntax I haven't seen before

查看:81
本文介绍了我以前没见过的CGRect语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一些示例代码中看到了下面的语法,我不确定是否理解。

I saw the syntax below in some example code and am not sure I understand it.

 CGRect imageRect = (CGRect){.size = baseImage.size};

这只是初始化 CGRect 相当于:

 CGRect imageRect = CGRectMake(0,0,baseImage.size.width, baseImage.size.height);

除了稍微少打字外,这种语法有什么好处吗?

Is there any benefit to this syntax aside from slightly less typing?

推荐答案

这是 C99初始化程序语法。您可以将它与任何结构一起使用。

That's C99 initializer syntax. You can use it with any structure.

Objective-C的主要优点是它为您提供了一些非常类似于Objective-C的语法,其中字段接近于价值而不是定位所暗示的价值。 (这并不是说这是故意相似的,或者说这是唯一的优势。但它很不错。)

The main advantage to an Objective-C is that it gives you some very Objective-C like syntax, where the fields are close to the values rather than implied by positioning. (That's not to say this is intentionally similar, or that it's the only advantage. But it is nice.)

有时会稍微更多打字,但我现在到处都使用它。

It's sometimes slightly more typing, but I use it everywhere now.

考虑:

CGRect a = CGRectMake(a+c/2, b+d/2, c, d);

为了理解这一点,您需要了解参数的顺序。你还需要能够用眼睛轻松捕捉逗号。在这种情况下,这很简单,但如果表达式更复杂,你可能首先将它们存储在临时变量中。

In order to understand this, you need to understand the order of the parameters. You also need to be able to catch the commas easily with your eyes. In this case, that's pretty easy, but if the expressions were more complicated you'd probably be storing them in a temporary variable first.

C99方式:

CGRect a = (CGRect){
    .origin.x = a+c/2,
    .origin.y = b+d/2,
    .size.width = c,
    .size.height = d
};

它更长,但更明确。无论表达多长时间,都可以很容易地按照指定的内容进行操作。它也更像是Objective-C方法。毕竟,如果CGRect是一个类,它可能看起来像这样:

It's longer, but it's more explicit. It's also very easy to follow what is assigned to what, no matter how long the expression are. It's also more like an Objective-C method. After all, if CGRect was a class, it would probably look like this:

CGRect *a = [[CGRect alloc] initWithOriginX:x originY:y width:w height:h];

你也可以这样做:

CGRect a = (CGRect){
    .origin = myOrigin,
    .size = computedSize
};

在这里,您使用 CGPoint构建矩形 CGSize 。编译器理解 .origin 期望 CGPoint .size 期望 CGSize 。你提供了这个。所有的肉汁。

Here, you're building a rectangle using a CGPoint and CGSize. The compiler understands that .origin expects a CGPoint, and .size expects a CGSize. You've provided that. All's gravy.

等价的代码是 CGRectMake(myOrigin.x,myOrigin.y,size.width,size.height)。通过使用 CGRectMake ,您不再对编译器表达同样的含义。它不能阻止您将部分大小分配给原点。它也不会阻止您将宽度指定为高度。它甚至没有给你一个关于哪个是X和Y的好线索;如果您已经使用了首先提供垂直坐标的API,那么您就会出错。

The equivalent code would be CGRectMake(myOrigin.x, myOrigin.y, size.width, size.height). By using CGRectMake you're no longer expressing the same kind of meaning to the compiler. It can't stop you from assigning part of the size to the origin. It also won't stop you from assigning the width to the height. It doesn't even give you a good clue about which is the X and Y; if you've used APIs that provide vertical coordinates first, you'll get it wrong.

您可以从结构中分配部件,也可以从浮动部件中分配部件:

You can assign part from a structure and part from floats as well:

CGRect a = (CGRect){
    .origin = myOrigin,
    .size.width = c,
    .size.height = d
};

CGRectMake 函数早于C99。我没有证据证明这一点,但我认为如果C99首先出现 CGRectMake 可能根本就不存在;当你的语言没有直接的方法来执行初始化时,你会编写一种硬壳函数。但现在确实如此。

The CGRectMake function predates C99. I have no evidence to this effect, but I think if C99 had come first CGRectMake probably wouldn't exist at all; it's the sort of crusty function you write when your language has no direct way to perform the initialization. But now it does.

基本上,如果你使用它一段时间,你可能会更喜欢C99语法。它更明确,更灵活,更像Objective-C并且更难搞砸。

Basically, if you use it for a while, you'll probably come to prefer C99 syntax. It's more explicit, more flexible, more Objective-C-like and harder to screw up.

不幸的是,从4.6开始,Xcode在C99中不会自动完成结构字段名称字段初始化列表。

Unfortunately, as of 4.6 Xcode will not autocomplete structure field names when in the C99 field initializer list.

这篇关于我以前没见过的CGRect语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆