使用块缩短我的 Objective-C 代码

发布于 2024-09-09 21:28:12 字数 638 浏览 2 评论 0原文

这是我用来修改 UIView 的类别。该代码有效,但在第一个方法(setFrameHeight)中我使用了一个块,而在第二个方法(setFrameWidth)中我没有使用。在这个例子中,有什么方法可以更有效地使用块吗?

typedef CGRect (^modifyFrameBlock)(CGRect);

- (void) modifyFrame:(modifyFrameBlock) block {
    self.frame = block(self.frame);
}

- (void) setFrameWidth:(CGFloat)newWidth {
    modifyFrameBlock b = ^CGRect (CGRect frame) { 
        frame.size.width = newWidth;
        return frame; 
    };      
    [self modifyFrame:b];
}

- (void) setFrameHeight:(CGFloat)newHeight {
    CGRect f = self.frame;
    f.size.height = newHeight;
    self.frame = f;
}

答案可能是块不适合这么短的方法,或者其他什么。语法确实看起来很时髦。

This is from a category that I'm using to modify UIView. The code works, but in the first method (setFrameHeight) I'm using a block and in the second method (setFrameWidth) I'm not. Is there any way to use blocks more efficiently in this example?

typedef CGRect (^modifyFrameBlock)(CGRect);

- (void) modifyFrame:(modifyFrameBlock) block {
    self.frame = block(self.frame);
}

- (void) setFrameWidth:(CGFloat)newWidth {
    modifyFrameBlock b = ^CGRect (CGRect frame) { 
        frame.size.width = newWidth;
        return frame; 
    };      
    [self modifyFrame:b];
}

- (void) setFrameHeight:(CGFloat)newHeight {
    CGRect f = self.frame;
    f.size.height = newHeight;
    self.frame = f;
}

The answer may be that blocks are not appropriate for such short methods, or something. The syntax sure seems funky.

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

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

发布评论

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

评论(2

巷子口的你 2024-09-16 21:28:12

您获得的唯一好处是无需声明新矩形的局部变量,作为交换,您需要定义一个块。这并不是一件好事,尤其是因为它会分散你正在做的事情的注意力。

请注意,您的块使用可以稍微缩短:

[self modifyFrame:^(CGRect frame) {
    frame.size.width = newWidth;
    return frame; 
}];

或者甚至:

[self modifyFrame:^(CGRect* frame) {
    frame->size.width = newWidth;
}];

使用 modifyFrame: 是:

CGRect frame;
block(&frame);
self.frame = frame;

但我仍然将这种方法限制为更复杂的方法,只需要代码的一小部分不同。

The only thing you gain is not declaring the local variable for the new rect, in exchange you need to define a block. That is not a good deal, especially as it distracts from what you are doing.

Note that your block-usage could be shortened a bit:

[self modifyFrame:^(CGRect frame) {
    frame.size.width = newWidth;
    return frame; 
}];

Or even:

[self modifyFrame:^(CGRect* frame) {
    frame->size.width = newWidth;
}];

With modifyFrame: being:

CGRect frame;
block(&frame);
self.frame = frame;

But i'd still limit such approaches to more complex methods that only require minor parts of the code to be different.

一身骄傲 2024-09-16 21:28:12

这种特殊的模式在其他情况下很有用(通常在运行时需要可扩展性的情况下),并且在 C 中经常看到用函数指针代替块(例如 qsort()bsearch()< /code>。)但是,对于仅更新字段,通常的方法是从更简单的方法调用更复杂的方法:

- (void)setDisplaysUserInterface: (BOOL)flag {
    [self setDisplaysUserInterface: flag animated: YES];
}


- (void)setDisplaysUserInterface: (BOOL)flag animated: (BOOL)animated {
    [self setDisplaysUserInterface: flag animated: animated hideAfterDelay: NAN];
}

- (void)setDisplaysUserInterface: (BOOL)flag animated: (BOOL)animated hideAfterDelay: (NSTimeInterval)hideDelay {
    // and so forth
}

This particular pattern is useful in other situations (generally where extensibility is needed at runtime) and is frequently seen in C with function pointers in place of blocks (e.g. qsort() and bsearch().) For just updating a field, however, the usual way is to just call from the simpler method to the more complex one:

- (void)setDisplaysUserInterface: (BOOL)flag {
    [self setDisplaysUserInterface: flag animated: YES];
}


- (void)setDisplaysUserInterface: (BOOL)flag animated: (BOOL)animated {
    [self setDisplaysUserInterface: flag animated: animated hideAfterDelay: NAN];
}

- (void)setDisplaysUserInterface: (BOOL)flag animated: (BOOL)animated hideAfterDelay: (NSTimeInterval)hideDelay {
    // and so forth
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文