尝试在Swift中使用"WormHole“(来自link),下面从Objective C到Swift的翻译似乎不起作用。你有什么关于如何做的提示吗?
我的Swift-code (还不能工作!)是:
class ViewController: UIViewController {
@IBOutlet weak var numberLabel: UILabel!
@IBOutlet weak var segmentedControl: UISegmentedControl!
// Initialize the wormhole
let wormhole = MMWormhole(applicationGroupIdentifier: "group.ch.ideenkaffee.wormhole", optionalDirectory: "wormhole")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Obtain an initial message from the wormhole
// ERROR occurs here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
let messageObject: AnyObject? = wormhole.messageWithIdentifier("button")
if let number = messageObject!.valueForKey("buttonNumber") as? Int {
numberLabel.text = ("\(number.value)")
}
// Become a listener for changes to the wormhole for the button message
wormhole.listenForMessageWithIdentifier("button", listener: { (messageObject) -> Void in
// The number is identified with the buttonNumber key in the message object
if let number = messageObject.valueForKey("buttonNumber") as? Int {
self.numberLabel.text = ("\(number.value)")
}
})
self.segmentedControlValueDidChange(segmentedControl)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func segmentedControlValueDidChange(sender: UISegmentedControl) {
if let title = segmentedControl.titleForSegmentAtIndex(segmentedControl.selectedSegmentIndex) {
// Pass a message for the selection identifier. The message itself is a NSCoding compliant object
// with a single value and key called selectionString.
wormhole.passMessageObject(["selectionString":title], identifier: "selection")
}
}
}最初的Objective-C代码是:
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UILabel *numberLabel;
@property (nonatomic, weak) IBOutlet UISegmentedControl *segmentedControl;
@property (nonatomic, strong) MMWormhole *wormhole;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Initialize the wormhole
self.wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"group.com.mutualmobile.wormhole"
optionalDirectory:@"wormhole"];
// Obtain an initial message from the wormhole
id messageObject = [self.wormhole messageWithIdentifier:@"button"];
NSNumber *number = [messageObject valueForKey:@"buttonNumber"];
self.numberLabel.text = [number stringValue];
// Become a listener for changes to the wormhole for the button message
[self.wormhole listenForMessageWithIdentifier:@"button" listener:^(id messageObject) {
// The number is identified with the buttonNumber key in the message object
NSNumber *number = [messageObject valueForKey:@"buttonNumber"];
self.numberLabel.text = [number stringValue];
}];
[self segmentedControlValueDidChange:self.segmentedControl];
}
- (IBAction)segmentedControlValueDidChange:(UISegmentedControl *)segmentedControl {
NSString *title = [segmentedControl titleForSegmentAtIndex:segmentedControl.selectedSegmentIndex];
// Pass a message for the selection identifier. The message itself is a NSCoding compliant object
// with a single value and key called selectionString.
[self.wormhole passMessageObject:@{@"selectionString" : title} identifier:@"selection"];
}
@end任何关于Swift-code的帮助都将不胜感激!
发布于 2015-05-24 18:39:43
这对我来说很有效:
if let toWatchOldMessage: AnyObject = wormhole.messageWithIdentifier("toWatch") {
println("*ic getOldWormholeMessages toWatchOldMessage \(toWatchOldMessage)")
} else {
println("*ic getOldWormholeMessages toWatchOldMessage no message")
}https://stackoverflow.com/questions/30003180
复制相似问题