iOS单例模式

 

+(HRTNetworking *)shareHRTNetworking

{

   static HRTNetworking *_sharedClient = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        _sharedClient = [[HRTNetworking alloc] initWithBaseURL:[NSURL URLWithString:HrtApp_URL]];

        [[_sharedClient requestSerializer] setTimeoutInterval:NET_WORKING_TIME_OUT];//超时时间

    });

 

    return _sharedClient;

}

 

self,在实例方法(对象方法)中表现是实例。在类方法中则表现为一种多态(可能是某一个类)的类实例(class instance),他总是会返回正确的类型(某一类),比如这样:

+ (id)new
{
    return [[self alloc] init];
}

而在本文的这个@synchronized(self)里的self,总是会指向同一个对象,即那个特殊的类实例。(class也是一个对象),故而此处可以使用self。

原文地址:https://www.cnblogs.com/dengchaojie/p/7815068.html