objc-guide
objc-guide copied to clipboard
Blocks
I was asked if we had anything about blocks in here. We should probably add this? No idea what the convention should be :D I always go here to remind myself how to use em
Convention
Rationale
Example
Really good question and topic. Thanks for bringing it up!
Some initial thoughts:
- We don't want to get into the territory of making recommendations here that the compiler already enforces by default. The link you provided only deals with that and is excellent and profane at doing so, but I don't think we don't need to replace, mimic, or even link to it since it is compiler enforced.
- When to pass weak objects into blocks to avoid retain cycles is probably very necessary as that comes up frequently
- There should also be a convention on passing the minimal amount of needed context into the block - don't refer to self in the block if all you need is a known property of self that you can fetch to the heap before declaring the block.
- Possibly contradictory to the first thought, but I wonder if this is the place where we should be proposing compiler flags. My main guidance around blocks would be to use these settings to enforce the above CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES CLANG_WARN_OBJC_REPEATED_USE_OF_WEAK = YES
- Whether to use typedefs for complex blocks may be interesting as well, though I haven't yet seen an opinion to sway me in one way or the other. It adds some convenience but also some obfuscation.
- When to use blocks vs delegate/dataSource style is definitely something worthy of discussion here. I'm increasingly of the opinion that we should avoid new delegates for the most part in favor of block params that don't enforce the client to be an ObjC class and thus allow for slimmer, more declarative consuming code. But I don't think we're anywhere close to a consensus on that yet.
- Any other frequent questions come up?