作为一个IOS软件开发者,遇到混编也是常有之事,本文通过一个简单的数字时钟示例,展示如何通过中间文件达到目的,实现混编。
本文创建的是一个OC项目,但是实际上OC项目和Swift项目混编是差不多的,下面上货。
#import "ProductModuleName-Swift.h"
import Foundation
@objc protocol UpdateObserver {
func update ()
}
@objc class HelloWorldTimer :NSObject{
fileprivate var updateObserver : UpdateObserver?
fileprivate var isUpdating : Bool = false
override init() {
super.init()
}
func printLog ( _ printString :String ) -> Void{
print("\(AppDelegate.getAppId()!): \(printString)")
}
func getNowTimeString () -> String{
let date = NSDate()
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"//yyyy-MM-dd HH:mm:ss.SSS
return timeFormatter.string(from: date as Date) as String
}
func registerTimerChangeObserver (obs : UpdateObserver ){
updateObserver = obs
}
func startTimer(){
isUpdating = true
timeUpdatePerSecond()
}
func isTimeUpdating () -> Bool{
return self.isUpdating
}
func endTimer() {
isUpdating = false
}
@objc fileprivate func timeUpdatePerSecond() {
guard isUpdating else {
return
}
if let o = updateObserver {
o.update()
}
perform(#selector(timeUpdatePerSecond), with: nil, afterDelay: 1)
}
}
#import
#import "IOS_App-Swift.h"
@interface ViewController : UIViewController
@end
#import "ViewController.h"
#import "IOS_App-Swift.h"
@interface ViewController ()
{
UILabel *time;
HelloWorldTimer *world;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
world = [[HelloWorldTimer alloc]init];
[world registerTimerChangeObserverWithObs:self];
time = [[UILabel alloc]initWithFrame:CGRectZero];
time.translatesAutoresizingMaskIntoConstraints = false;
time.text = [world getNowTimeString];
time.font = [UIFont systemFontOfSize:33];
time.textColor = [UIColor blueColor];
NSLayoutConstraint * timeCentreXConstraint = [NSLayoutConstraint constraintWithItem:time attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0];
NSLayoutConstraint * timeCentreYConstraint = [NSLayoutConstraint constraintWithItem:time attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0];
[self.view addSubview:time];
[self.view addConstraints:@[timeCentreXConstraint,timeCentreYConstraint]];
NSNotificationCenter* ns = [NSNotificationCenter defaultCenter];
[ns addObserver:self selector:@selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
[ns addObserver:self selector:@selector(applicationWillResignActive) name:UIApplicationWillResignActiveNotification object:nil];
}
- (void)applicationDidBecomeActive {
[world startTimer];
[world printLog:@"applicationDidBecomeActive"];
}
- (void) applicationWillResignActive {
if ([world isTimeUpdating]){
[world endTimer];
}
[world printLog:@"applicationWillResignActive"];
}
- (void)update {
time.text = [world getNowTimeString];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "AppDelegate.h"
#import
#import
@interface AppDelegate : UIResponder
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong) NSPersistentContainer *persistentContainer;
- (void)saveContext;
+ (NSString *) getAppId;
@end
//.....
@implementation AppDelegate
+ (NSString *)getAppId {
return @"IOS-App";
}
//.......
func printLog ( _ printString :String ) -> Void{
print("\(AppDelegate.getAppId()!): \(printString)")
}
IOS-App: applicationDidBecomeActive
IOS-App: applicationWillResignActive