2

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(?)

T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32

2 Answers2

7

[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.

James Webster
  • 31,873
  • 11
  • 70
  • 114
0

I recently wondered the same thing about [UIView new]. While using it in a new code example I ran into problems covered in Jame’s answer. These were resolved once I reverted to [[UIView alloc] initWithFrame:CGRectZero]. At least I know what properties the new UIView inherits.

Greg
  • 1,750
  • 2
  • 29
  • 56