Filipe Sá
Filipe Sá

Reputation: 382

Calling a Objective-C method using Swift 3

I'm going crazy here.

I'm trying to call a Objective-c method in Swift following some answers here on StackOverflow and other sites. But i'm getting some errors initializing the method...

#error : Cannot convert value of type '()' to specified type 'getEvents'

Is it because of Swift 3? what am i doing wrong here?

Here is my full code:

getEvents.h

#import <Foundation/Foundation.h>

@interface getEvents : NSString

+(NSString *) objGetEvents:(NSString *)latitude andLon:(NSString *)longitude andRadius:(NSString *)radius andMeasure:(NSString *)measure;

@end

getEvents.m

#import "getEvents.h"

@implementation getEvents

+(NSString *) objGetEvents:(NSString *)latitude andLon:(NSString *)longitude andRadius:(NSString *)radius andMeasure:(NSString *)measure {

    NSString *link = [[NSString alloc] initWithFormat: @"http://some.website/file.php?lat=%@&lon=%@&ms=%@&dist=%@", latitude, longitude, measure, radius];

    NSData *myData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[link stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]]];

    return [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];

}

@end

ListEvents.swift

func getEvents() {
    let getEventsObj:getEvents = getEvents()
    // error: Cannot convert value of type '()' to specified type 'getEvents'

    let jsonString = getEventsObj.objGetEvents(latitude, andLon: longitude, andRadius: searchRadius, andMeasure: searchMeasure) as NSString 
    // error: Static member 'objGetEvents' cannot be used on instance of type 'getEvents'

    print(jsonString)
}

PS. I am importing "getEvents.h" in Extension-Bridging-Header.h


I am trying to use Objective-C NSData because NSData on Swift and get the contents of my URL as specified in this question: https://stackoverflow.com/questions/40194517/nsdatacontentsof-options-works-on-the-simulator-but-not-on-the-device

Upvotes: 0

Views: 742

Answers (1)

dirtydanee
dirtydanee

Reputation: 6151

You have declared a class function, no need for an instance to be created.

Your code should be like:

 func getEvents() {
        let jsonString = getEvents.objGetEvents(latitude, andLon: longitude, andRadius: searchRadius, andMeasure: searchMeasure) as NSString 

        print(jsonString)
    } 

Upvotes: 2

Related Questions