How do i translate this from Objective-C to Swift

310 views Asked by At

I am unclear how to use the Radio Icecast library in Swift. How do i translate the following into Swift?

radio = [[Radio alloc] init:@"my app"];
[radio connect:STREAM_URL withDelegate:self withGain:(1.0)];
playing = YES;   
1

There are 1 answers

6
Rob On BEST ANSWER

The way that this class implemented init is not correct. Generally you'd see a method called init (which takes no parameters) and if you needed a rendition with a user agent parameter, the method would be called initWithUserAgent:.

So, in the Radio.h file, find the declaration of:

- (id)init:(NSString *)userAgent;

And replace it with:

- (id)initWithUserAgent:(NSString *)userAgent;

Do the same with the Radio.m file.

Then the Objective-C syntax becomes:

radio = [[Radio alloc] initWithUserAgent:@"my app"];
[radio connect:STREAM_URL withDelegate:self withGain:1.0];
playing = YES;

And the Swift equivalent would be:

radio = Radio(userAgent: "my app")
radio.connect(STREAM_URL, withDelegate: self, withGain: 1.0)
playing = true

Obviously, if this Radio class is implemented in Objective-C, then you'd include the .h file in your bridging header. For more information about bridging headers, see the Swift and Objective-C in the Same Project reference.