ios - data disappear out of UNIUrlConnection -
what i'm trying pass data objective-c swift file.
recently, when have connected objective c file swift file, can print data uniurlconnection(which means connection between swift , objective-c works fine).
then create nsmutabledictionary object , add data nsmutabledictionary. want pass nsmutabledictionary object swift file can output them on viewcontroller.
however, nsmutabledictionary contains correct data within uniurlconnection contains none out of uniurlconnection. nsmutabledictionary passed swift file contain no data. how can solve this?
this objective c file.
#import <foundation/foundation.h> #import <unirest.h> #import "findbyingredients.h" @implementation findbyingredients -(nsmutabledictionary*) connectapi:(nsstring*) ingredients number:(nsstring*) number{ nsmutabledictionary *temp = [nsmutabledictionary dictionarywithcapacity:10]; nsstring* url = @"https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/findbyingredients?fillingredients=false"; if (![ingredients isequal: @""]) { nsstring* ingre = @"&ingredients="; ingre = [ingre stringbyappendingstring:ingredients]; url = [url stringbyappendingstring:ingre]; } else { nsstring* ingre = @"&ingredients=<required>"; url = [url stringbyappendingstring:ingre]; } nsstring* ll = @"&limitlicense=false"; url = [url stringbyappendingstring:ll]; if (![number isequal: @""]) { nsstring* numberofreturn = @"&number="; numberofreturn = [numberofreturn stringbyappendingstring:number]; url = [url stringbyappendingstring:numberofreturn]; } nsstring* ranking = @"&ranking=1"; url = [url stringbyappendingstring:ranking]; // these code snippets use open-source library. http://unirest.io/objective-c nsdictionary *headers = @{@"x-mashape-key": @"key", @"accept": @"application/json"}; uniurlconnection *asyncconnection = [[unirest get:^(unisimplerequest *request) { [request seturl:url]; [request setheaders:headers]; }] asjsonasync:^(unihttpjsonresponse *response, nserror *error) { nsinteger code = response.code; nsdictionary *responseheaders = response.headers; unijsonnode *body = response.body; nsdata *rawbody = response.rawbody; if (rawbody) { id allkeys = [nsjsonserialization jsonobjectwithdata:rawbody options:0 error:&error]; (int i=0; i<[allkeys count]; i++) { nsdictionary *arrayresult = [allkeys objectatindex:i]; // nsstring* mynewstring = [nsstring stringwithformat:@"%d", i]; nsstring *key = [arrayresult objectforkey:@"id"]; nsstring *value = [arrayresult objectforkey:@"title"]; [temp setobject:value forkey:key]; nslog(@"id=%@",[arrayresult objectforkey:@"id"]); nslog(@"title=%@",[arrayresult objectforkey:@"title"]); } } int size = [temp count]; nslog(@"temp size %d",size); nslog(@"successfully test api"); }]; // int size = [temp count]; // nslog(@"temp size %d",size); return temp; } @end
here size of temp within uniurlconnection correct. if put size before return (which comment out), shows 0, wrong.
this swift file.
@ibaction func search(_ sender: any) { let ingrearr = ingredientstext.text.components(separatedby: [",", " "]) var ingre:string = ingrearr[0] let sep:string = "%2c" (index,element) in ingrearr.enumerated() { if (index >= 1) { ingre = ingre + sep + element } } let test = findbyingredients() // test.connectapi(ingre, number: numberofresults.text) let result: dictionary = test.connectapi(ingre, number: numberofresults.text) dictionary let num:int = result.count print(num) }
the num 0.
Comments
Post a Comment