zoukankan      html  css  js  c++  java
  • ARC模式下的单例写法


    //
    单例 + (id)sharedInstance { __strong static id sharedObject = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedObject = [[self alloc] init]; }); return sharedObject; }

    dispatch_once

    Executes a block object once and only once for the lifetime of an application.

       void dispatch_once(

       dispatch_once_t *predicate,

       dispatch_block_t block);

    Parameters

    predicate

    A pointer to a dispatch_once_t structure that is used to test whether the block has completed or not.

    block

    The block object to execute once.

    Discussion

    This function is useful for initialization of global data (singletons) in an application. Always call this function before using or testing any variables that are initialized by the block.

    If called simultaneously from multiple threads, this function waits synchronously until the block has completed.

    The predicate must point to a variable stored in global or static scope. The result of using a predicate with automatic or dynamic storage (including Objective-C instance variables) is undefined.

    Availability

    • Available in iOS 4.0 and later.

    Declared In

    dispatch/once.h

  • 相关阅读:
    OPENCV(3) —— 对XML和YAML文件实现I/O 操作
    OPENCV(2) —— Basic Structures(二)
    OPENCV(2) —— Basic Structures(一)
    OPENCV(1)
    转:计算机视觉相关领域代码集
    ScSPM
    ScSPM & LLC
    Spatial Pyramid Matching
    Convolution & Pooling exercise
    Restricted Boltzmann Machines
  • 原文地址:https://www.cnblogs.com/willbin/p/3607090.html
Copyright © 2011-2022 走看看