Difference between [[NSDate date] retain] and [[NSDate alloc] init]

前端 未结 5 1897
醉梦人生
醉梦人生 2021-01-03 10:36

As both of the following serves the same purpose,

today = [[NSDate date] retain];    

and

today = [[NSDate alloc] init]; 
<         


        
5条回答
  •  旧巷少年郎
    2021-01-03 11:18

    [NSDate date] is a convenience constructor using which you can leave the headache of releasing the object to autorelease pool. Sending a retain message to the convenience constructor like [[NSDate date] retain] makes you the owner of the object and you are responsible for releasing it properly.

    [[NSDate alloc] init] is the default initializer by which you become the owner of the object, which is almost equal to [[NSDate date] retain].

提交回复
热议问题