I am working on a mixed language project, combining both Objective C and Swift in XCode 6.
In this project, the Singleton (Objective C) class posts a notification which is then received by ViewController (Swift).
Singleton.h
#import <Foundation/Foundation.h>
NSString *const notificationString = @"notificationString";
@interface Singleton : NSObject
+ (id)sharedSingleton;
- (void)post;
@end
Singleton.m
#import "Singleton.h"
static Singleton *shared = nil;
@implementation Singleton
- (id)init {
self = [super init];
if (self) {
}
return self;
}
#pragma mark - Interface
+ (Singleton *)sharedSingleton {
static dispatch_once_t pred;
dispatch_once(&pred, ^{
shared = [[Singleton alloc] init];
});
return shared;
}
- (void)post {
char bytes[5] = {5, 7, 9, 1, 3};
NSDictionary *objects = @{@"device":[NSData dataWithBytes:bytes length:5], @"step1":[NSNumber numberWithInt:4], @"step2":[NSNumber numberWithInt:7]};
[[NSNotificationCenter defaultCenter] postNotificationName:notificationString
object:self
userInfo:objects];
}
@end
Of course, in this mixed-language project, bridging header must be setup correctly (just by adding #import "Singleton.h"
in it)
ViewController.swift
import UIKit
class ViewController: UIViewController {
let singleton = Singleton.sharedSingleton() as Singleton
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "action:", name: notificationString, object: nil)
singleton.post()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Notification
func action(notification: NSNotification) {
let userInfo = notification.userInfo as Dictionary<String, String> // things go wrong here?!
let hash = userInfo["device"]
let completed = userInfo["step1"]
let total = userInfo["step2"]
}
}
This makes no compilation error. However, at run time, XCode reports:
fatal error: dictionary cannot be bridged from Objective-C
notification.userInfo
contains an NSDictionary built by NSSTring: NSData
, NSSTring: NSNumber
, NSSTring: NSNumber
while this command let userInfo = notification.userInfo as Dictionary<String, String>
is trying to convert to Dictionary<String, String>
Does this cause the fatal error?
In ViewController.swift
, what should I do to "read" NSDictionary passed in notification.userInfo
, sent from Singleton.m
?
Thanks in advance
try doing this
as you indicated, the userInfo dictionary contains NSData, NSNUmber for values.