swiftobjective-c-blocksibm-watsonalchemyapibridging

How to use swift function that returns a value - in objective C?


I am using IBM Watson APIs - Alchemy Data news

The problem is, I am using swift - objective C bridging and in between I am stuck with the function that returns a value. How do I use that value in my objective C code?

Here is my swift class

@objc class alchemyNews : NSObject {

func getNewsList() -> NewsResponse {

    let apiKey = "api-key"
    let alchemyDataNews = AlchemyDataNews(apiKey: apiKey)

    let start = "now-1d" // yesterday
    let end = "now" // today
    let query = [
        "q.enriched.url.title": "O[IBM^Apple]",
        "return": "enriched.url.title,enriched.url.entities.entity.text,enriched.url.entities.entity.type"
    ]
    let failure = { (error: NSError) in print(error) }

    alchemyDataNews.getNews(start, end: end, query: query, failure: failure) { news in
        print(news)

    }

    let response : NewsResponse = alchemyDataNews.getNews(start, end: end) { news in
        return news
    }

    return response
}
}

I want to have alchemyDataNews.getNews print value to be display. So I am calling this function in my Objective C class in this way.

@property (strong, nonatomic) AlchemyDataNews *getnews;

-(void)loadNews
{
   self.getnews = [[AlchemyDataNews alloc]init];
   [self.getnews getNewsList];

}

But what to do now? This will just call the function and not give me the response so that I can put it in array and display in tableview.


Solution

  • I think the issue is getNewsList returns an instance of NewsResponse, you should store it in a variable and then use it.

    self.getnews = [[AlchemyDataNews alloc]init];
    NewsResponse *newResponse = [self.getnews getNewsList];
    
    // now you can use 'newResponse'
    // ...
    

    Hope that helped.