Implementation of a singleton class

Source: Internet
Author: User

Singleton class: A class that is probably understood to have only one object, no matter how it is created, only allocates one memory

These days, need to implement a function, and in a lot of classes to call this method, so I Soching encapsulated a class, directly invoke the method inside the class can be implemented, do not need to write the same method every time (this is the big background), Then in the call I found I needed to use the same object every time (with emphasis), so I needed to encapsulate a singleton class, and it wouldn't waste memory.

Okay, here's the code:

+ (uiview*) Initactivityindictionview

{

static UIView *view = nil;

Static dispatch_once_t predicted;

Dispatch_once (&predicted, ^{

view = [[Self alloc]init];

View.bounds = CGRectMake (0, 0, kscreenwidth, 100);

View.center = Cgpointmake (KSCREENWIDTH/2, 50);

Uiactivityindicatorview *testactivityindicator = [[Uiactivityindicatorview alloc]initwithactivityindicatorstyle: Uiactivityindicatorviewstylegray];

Testactivityindicator.center = Cgpointmake (KSCREENWIDTH/2-60, 50);

Testactivityindicator.bounds = CGRectMake (0, 0, 50, 50);

[Testactivityindicator startanimating];

UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (KSCREENWIDTH/2-30, 25, 120, 50)];

Label.text = @ "trying desperately to load ...";

Label.font = [Uifont systemfontofsize:14];

[View Addsubview:testactivityindicator];

[View Addsubview:label];

});

return view;

}

The above is my own package of a singleton class, test words can create several objects, print (%p) memory can be

Implementation of a singleton class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.