senty
senty

Reputation: 12847

Converting From Objective-C to Swift

I am trying to convert this script:

 @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

...and this is how far I got however I couldn't complete it. I have almost no experience in Objective C. I researched a lot about converting however I couldn't find any good for @interaface EventSource () part and @property id but I am stuck here:

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)

 }

Upvotes: 0

Views: 147

Answers (1)

Zhengjie
Zhengjie

Reputation: 451

This converting to Swift like this:

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

init function in Objective-c.

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
 }

}

Upvotes: 3

Related Questions