Swift 7.OC单例

前端之家收集整理的这篇文章主要介绍了Swift 7.OC单例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

ViewController.m

#import "ViewController.h"


#import "单例-Swift.h"

//OCd调用 swift  需要导入桥接文件 命名空间-Swift.h 文件
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    SoundTools *tools = [SoundTools tools];
    
    
    NSLog(@"%@",[SoundTools tools]);
    NSLog(@"%@",tools);
    
    
}


NetworkTools.h

#import <Foundation/Foundation.h>

@interface NetworkTools : NSObject

//设置全局访问点
+ (instancetype) sharedTools;
@end

NetworkTools.m

#import "NetworkTools.h"

@implementation NetworkTools

+ (instancetype)sharedTools {
    static NetworkTools *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        instance = [[NetworkTools alloc] init];
    });
    
    
    return instance;
}
@end

SoundTools.swift
import UIKit

class SoundTools: NSObject {

    //let 不可变 同样是线程安全的 
    var baseURL: String?
    
    static let tools: SoundTools = { () -> SoundTools in
        let instance = SoundTools()
        instance.baseURL = "www.douniwan.com"
        
        return instance
    }()
    
}
原文链接:https://www.f2er.com/swift/324962.html

猜你在找的Swift相关文章