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