久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

iOS 5 中的單例?

Singleton in iOS 5?(iOS 5 中的單例?)
本文介紹了iOS 5 中的單例?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

您好,我有一個用于單例的 iOS 以前版本的實現,如下所示:

Hi I had an implementation previous versions of iOS for a singleton as follows:

.h 文件

@interface CartSingleton : NSObject 
{

}
+(CartSingleton *) getSingleton;

.m 文件

@implementation CartSingleton

static CartSingleton *sharedSingleton = nil;

+(CartSingleton *) getSingleton
{
    if (sharedSingleton !=nil)
       {
        NSLog(@"Cart has already been created.....");
        return sharedSingleton;
       }
    @synchronized(self)
   {
    if (sharedSingleton == nil)
       {
        sharedSingleton = [[self alloc]init];
        NSLog(@"Created a new Cart");
       }
   }
    return sharedSingleton;
}
//==============================================================================
+(id)alloc
{
    @synchronized([CartSingleton class])
   {
    NSLog(@"inside alloc");
    NSAssert(sharedSingleton == nil, @"Attempted to allocate a second instance of a singleton.");
    sharedSingleton = [super alloc];
    return sharedSingleton;
   }

    return nil;
}

//==============================================================================
-(id)init
{
    self = [super init];
}

然而,我在網上看到人們使用以下代碼實現了單例設計模式:

However on the web I see people have implemented the Singleton design pattern using this code:

+ (id)sharedInstance
{
  static dispatch_once_t pred = 0;
  __strong static id _sharedObject = nil;
  dispatch_once(&pred, ^{
    _sharedObject = [[self alloc] init]; // or some other init method
  });
  return _sharedObject;
}

請有經驗的人指導一下.我是一個新手,在單例的舊 iOS 實現和新的 iOS 實現之間完全混淆,哪個是正確的?

Could someone who is experience please guide me. Im a newbie and thoroughly confused between the old iOS implementation of the Singleton and the new one and which is the correct one?

非常感謝

推薦答案

嚴格來說,你必須使用:

Strictly speaking, you must use:

+ (MySingleton*) instance {
     static dispatch_once_t _singletonPredicate;
     static MySingleton *_singleton = nil;

     dispatch_once(&_singletonPredicate, ^{
        _singleton = [[super allocWithZone:nil] init];
     });

     return _singleton;
 }

 + (id) allocWithZone:(NSZone *)zone {
      return [self instance];
 }

現在你保證不能調用 alloc/init 并創建另一個實例.

Now you guarantee that one cannot call alloc/init and create another instance.

說明:實例方法在類級別,是您獲取單例引用的主要訪問方法.該方法僅使用 dispatch_once() 內置隊列,該隊列只會執行一次塊.運行時如何保證塊只執行一次?使用您提供的謂詞(dispatch_once_t 類型).這種低級調用將保證即使有多個線程試圖調用它,也只有一個成功,其他的等到第一個完成后再返回.

Explanation: The instance method is at the class level and is your main access method to get a reference to the singleton. The method simply uses the dispatch_once() built-in queue that will only execute a block once. How does the runtime guarantee that the block is only executed once? Using the predicate you supply (of type dispatch_once_t). This low-level call will guarantee that even if there are multiple threads trying to call it, only one succeeds, the others wait until the first one is done and then returns.

我們重寫 allocWithZone 的原因是因為 alloc 調用 allocWithZone 傳遞 nil 作為區域(對于默認區域).為了防止流氓代碼分配和初始化另一個實例,我們重寫 allocWithZone 以便傳回的實例是已經初始化的單例.這可以防止創建第二個實例.

The reason we override allocWithZone is because alloc calls allocWithZone passing nil as the zone (for the default zone). To prevent rogue code from allocating and init-ializing another instance we override allocWithZone so that the instance passed back is the already initialized singleton. This prevents one from creating a second instance.

這篇關于iOS 5 中的單例?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?(KIF:如何自動運行/壓力測試 iOS 應用程序以找出罕見 UI 錯誤的原因?)
Can#39;t change target membership visibility in Xcode 4.5(無法更改 Xcode 4.5 中的目標成員身份可見性)
UITableView: Handle cell selection in a mixed cell table view static and dynamic cells(UITableView:在混合單元格表視圖靜態和動態單元格中處理單元格選擇)
How to remove Address Bar in Safari in iOS?(如何在 iOS 中刪除 Safari 中的地址欄?)
iOS 5 SDK is gone after upgrade to Xcode 4.5(升級到 Xcode 4.5 后,iOS 5 SDK 消失了)
Having trouble creating UIImage from CIImage in iOS5(在 iOS5 中從 CIImage 創建 UIImage 時遇到問題)
主站蜘蛛池模板: 毛片网站免费观看 | 国产免费一区二区三区 | 精品少妇一区二区三区日产乱码 | 亚洲综合在线网 | 91精品国产91久久综合桃花 | 91爱啪啪 | 国产成人精品一区二区三区在线观看 | www国产成人免费观看视频,深夜成人网 | 337p日本欧洲亚洲大胆精蜜臀 | 成人性视频免费网站 | 激情五月婷婷综合 | 日韩精品国产精品 | 成人看片在线观看 | 91久久久久久久久久久 | 99riav3国产精品视频 | 成人精品一区二区三区中文字幕 | 四虎影 | 亚洲视频一区在线观看 | 欧美性受| 国产精品久久久久一区二区 | 亚洲国产成人精品在线 | 欧美一区二区免费 | 欧美不卡视频一区发布 | 国产a视频 | 欧美日韩在线不卡 | 欧美久久精品一级黑人c片 91免费在线视频 | 91高清视频在线观看 | 国产激情视频 | 久久精品av麻豆的观看方式 | 精品国产一区二区三区四区在线 | 午夜电影在线播放 | 精品乱码一区二区 | 国产91 在线播放 | 欧美成人激情 | 欧美一区二区三区在线播放 | 日韩精品国产精品 | 久久99精品久久久久久国产越南 | 久久久久国产一区二区三区四区 | 欧美日韩亚洲视频 | 中文字幕一区二区在线观看 | 美女福利网站 |