从Objective-C转换为Swift

时间:2015-05-11 03:55:08

标签: objective-c swift

我正在尝试转换此脚本:

 @interface EventSource : NSObject

 @end

 @protocol EventSourceDelegate <NSObject>

 - (void)eventSource:(EventSource *)eventSource didFailWithError:(NSError *)error;
 - (void)eventSource:(EventSource *)eventSource didReceiveEvent:(NSString *)event withData:(NSString *)data;

 @end

 @interface EventSource ()

 @property id <EventSourceDelegate> delegate;

 - (instancetype)initWithURL:(NSURL *)url delegate:(id      <EventSourceDelegate>)delegate;
 - (void)disconnect;

 @end

......这就是我走了多远但是我无法完成它。我几乎没有目标C的经验。我研究过很多关于转换的事情,但我找不到@interaface EventSource ()部分和@property id的任何好处,但我被困在这里:

import UIKit

class EventSource: NSObject {
}

//@obj
protocol EventSourceDelegate {

  func eventSource(eventSource: EventSource, didFailWithError: NSError?)
  func eventSource(eventSource: EventSource, didReceiveEvent: NSString, 
       event withData: NSString, data: NSString)

 }

1 个答案:

答案 0 :(得分:3)

这样转换为Swift:

- (instancetype)initWithURL:(NSURL *)url delegate:(id      <EventSourceDelegate>)delegate;
Objective-c中的

init函数。

protocol EventSourceDelegate {

  func eventSource(eventSource: EventSource, didFailWithError: NSError?)
  func eventSource(eventSource: EventSource, didReceiveEvent: NSString,
  event withData: NSString, data: NSString)

}


class EventSource: NSObject {
  var delegate:EventSourceDelegate

  init(url:NSURL,delegate:EventSourceDelegate){
  // TODO:  finish implementation
    self.delegate = delegate
  }
 func disconnect(){
 // TODO:  finish implementation
 }

}