• 单例的实现(完整版代码)


    #import "XMGTool.h"

     

    static XMGTool * _instance;//静态变量保证了单例的唯一性,静态变量是程序一开始就存在的

     

    @interface XMGTool ()<NSCopying, NSMutableCopying>

     

    @end

     

    @implementation XMGTool

     

    +(instancetype)allocWithZone:(struct _NSZone *)zone

    {

        static dispatch_once_t onceToken;

        dispatch_once(&onceToken, ^{

            _instance = [super allocWithZone:zone];

        });

        return _instance;

    }

     

    +(instancetype)shareTool

    {

        return [[self alloc]init];

    }

     

    - (nonnull id)copyWithZone:(nullable NSZone *)zone {

        return _instance;

    }

     

    - (nonnull id)mutableCopyWithZone:(nullable NSZone *)zone {

        return _instance;

    }

     

    @end

     

     

    外界的调用:

     

      XMGTool *t1 = [[XMGTool alloc]init];

       XMGTool *t2 = [[XMGTool alloc]init];

       XMGTool *t3 = [XMGTool shareTool];

       XMGTool *t4 = [t1 mutableCopy];

        

        NSLog(@" %@-- %@-- %@--- %@",t1,t2,t3,t4);

     

    打印结果

    <XMGTool: 0x6000011b2e30>--

    <XMGTool: 0x6000011b2e30>--

    <XMGTool: 0x6000011b2e30>---

    <XMGTool: 0x6000011b2e30>

  • 相关阅读:
    SpringBoot+Vue+HIKVSION实现摄像头多选并多窗口预览(插件版)
    Vue+Video.js播放m3u8视频流(海康威视摄像头+RTMP服务+FFmpeg)
    Vue+Openlayers中实现地图旋转
    Vue中实现检测当前是否为IE模式(极速模式还是兼容模式)
    Vue+Openlayer使用overlay实现弹窗弹出显示与关闭
    Vue+Openlayers实现显示图片并分优先级多图层加载
    Vue+Openlayer使用Draw实现交互式绘制线段
    ActionScriopt 产生随机颜色
    在AS3中格式化日期
    ArcGis For Flex 之 QueryTask地理坐标展现【原创】
  • 原文地址:https://www.cnblogs.com/dashengios/p/10420541.html
Copyright © 2020-2023  润新知