Ios 数据从UNIUrlConnection中消失
我试图做的是将数据从Objective-C传递到swift文件Ios 数据从UNIUrlConnection中消失,ios,objective-c,swift,unirest,Ios,Objective C,Swift,Unirest,我试图做的是将数据从Objective-C传递到swift文件 @IBAction func search(_ sender: Any) { let ingreArr = IngredientsText.text.components(separatedBy: [",", " "]) var ingre:String = ingreArr[0] let sep:String = "%2C" for (index,element) in ingreArr.enum
@IBAction func search(_ sender: Any) {
let ingreArr = IngredientsText.text.components(separatedBy: [",", " "])
var ingre:String = ingreArr[0]
let sep:String = "%2C"
for (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) as Dictionary
let num:Int = result.count
print(num)
}
最近,当我成功地将objective c文件与swift文件连接后,我可以打印从UNIUrlConnection获取的数据(这意味着swift和objective-c之间的连接工作正常)
然后创建一个NSMutableDictionary对象,并将这些数据添加到NSMutableDictionary中。我想将这个NSMutableDictionary对象传递到我的swift文件,以便在我的viewController上输出它们
但是,NSMutableDictionary在UNIUrlConnection内包含正确的数据,但在UNIUrlConnection外则不包含任何数据。因此,传递给swift文件的NSMutableDictionary始终不包含任何数据。我怎样才能解决这个问题
这是我的目标c文件
#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 an 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];
for (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 is %d",size);
NSLog(@"successfully test API");
}];
// int size = [temp count];
// NSLog(@"temp size is %d",size);
return temp;
}
@end
num是0。
asyncConnection
&asJsonAsync:
这给mis留下了很多异步的印象。我猜呢<代码>打印(num)在NSLog之前调用(@“dic3大小为%d”,大小)代码>。使用闭包/块。没错,在NSLog之前调用print(num)。我试图做的是将NSMutableDictionary从Objective-C传递到swift文件。但是,它不起作用。有什么想法吗?你必须创建一个块(在Objective-C中)并在Swift中使用它(称为闭包)。关于这一点有很多疑问,但只有一种语言,但逻辑是一样的。对不起。。。在搜索块和闭包之后,我仍然不知道如何更改我的代码。。。我对目标c和斯威夫特很陌生。你能提供更多的信息,比如一个例子,让我可以试试吗?非常感谢…-(void)connectAPI:(NSString*)成分编号:(NSString*)带块编号:(void(^)(NSMutableDictionary*)块代码>整数大小=[temp count];如果(block){block(temp);}
对于Swift部分,我不使用Swift,所以我不知道。