• 【代码笔记】iOS-单例


    一,工程图。

    二,代码。

    NetManager.h

    复制代码
    #import <Foundation/Foundation.h>
    
    @interface NetManager : NSObject
    
    + (id)sharedManager;
    
    -(void)firstPrintf;
    -(void)secondPrintf;
    -(void)threeprintf;
    -(void)fourprintf;
    
    
    
    @end
    复制代码

     

    NetManager.m

    复制代码
    #import "NetManager.h"
    
    
    static NetManager *manager;
    
    @implementation NetManager
    
    #pragma mark - 获取单例
    + (id)sharedManager{
        if (!manager) {
            manager = [[NetManager alloc]init];
        }
        return manager;
    }
    
    -(void)firstPrintf
    {
        NSLog(@"first Printf!!!!");
    }
    -(void)secondPrintf
    {
        NSLog(@"second printf!!!!!");
    }
    -(void)threeprintf
    {
        NSLog(@"three printf!!!!!!!");
    }
    -(void)fourprintf
    {
        NSLog(@"fourprintf!!!!!!");
    }
    复制代码

     

    RootViewController.h

    #import <UIKit/UIKit.h>
    
    @interface RootViewController : UIViewController
    
    @end

     

    RootViewController.m

    复制代码
    #import "RootViewController.h"
    
    //加上单例的头文件
    #import "NetManager.h"
    
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        
        //单例的用法,单例中的函数,可以在程序中直接使用。
        [[NetManager sharedManager] firstPrintf];
        [[NetManager sharedManager] secondPrintf];
        [[NetManager sharedManager] threeprintf];
        [[NetManager sharedManager] fourprintf];
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    复制代码

     

    三,输出。

    2015-10-13 13:55:17.551 单例[7675:197760] first Printf!!!!
    2015-10-13 13:55:17.551 单例[7675:197760] second printf!!!!!
    2015-10-13 13:55:17.551 单例[7675:197760] three printf!!!!!!!
    2015-10-13 13:55:17.551 单例[7675:197760] fourprintf!!!!!!

     

     

     
     
  • 相关阅读:
    Elasticsearch学习系列之介绍安装
    Python学习系列之文件操作
    Python学习系列之异常处理
    Python学习系列之装饰器
    windows 修改xhsell安全加密配置
    ipv6nginx错误
    zabbix 硬盘状态收集,制作表格
    申请免费ssl证书
    gitlab 搭建与迁移
    mogilefsdBUG mogilefsd[15624]: crash log: Modification of a read-only value attempted at /usr/local/share/perl5/Sys/Syscall.pm line 227
  • 原文地址:https://www.cnblogs.com/yang-guang-girl/p/5074928.html
Copyright © 2020-2023  润新知