简体   繁体   中英

weakSelf when setting frame

I recently read this post and figured it would be a good idea to use the tips from the article. I'm using it in blocks, but should I also use it in the 'block' below. Is the 'block' below a real block?

avatar.frame = ({
    CGRect frame = avatar.frame;
    frame.origin.y = CGRectGetMaxY(self.view);
    frame;
});

That would become:

__weak typeof(self)weakSelf = self;
avatar.frame = ({
    __strong typeof(weakSelf)strongSelf = weakSelf;

    CGRect frame = avatar.frame;
    frame.origin.y = CGRectGetMaxY(strongSelf.view);
    frame;
});

It's not a block. It is an GCC C extension, which causes a code block to return a value if enclosed within brackets and parentheses.

This not only segregates configuration details into initialization, but the additional scope allows generic variable names like frame, button, and view to be reused in subsequent initializations. No more loginButtonFrame = ... / signupButtonFrame = ...!

source: http://nshipster.com/new-years-2014/

This is not a block, block start with ^. I believe this code:

({
    CGRect frame = avatar.frame;
    frame.origin.y = CGRectGetMaxY(self.view);
    frame;
});

Is a way to create a CGRect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM