Search code examples
iosuiviewcgrectdesignated-initializer

Any caveats using [UIView new];?


Being thought to basically always use the designated initializer I feel a bit dirty when creating new viewInstances with [UIView new]; rather than [[UIView alloc] initWithFrame:CGRectZero];?

Is there any reason not to this? Is there any practical difference at all? If you are creating a new view-instance without any frame information wouldn't the fallback to [UIView new]; simply cut away a lot of the code-cruft? My guess would be that [[UIView alloc] initWithFrame:CGRectZero]; is actually called under the hood anyways(?)


Solution

  • [UIView new] is a convenience method to replace [[UIView alloc] init] so it is1 different to [[UIView alloc] initWithFrame:CGRectZero]

    Is there any reason not to this?`

    In this case, I'd say "yes". You can't guarantee that init is equivalent to initWithFrame:CGRectZero in this case.

    My guess would be that ...

    It's possible you are correct and the two will act identically; this isn't future proof. Implementation details like this may change.

    1. might be different, I don't know what initWithFrame: does under the hood.