gpt4 book ai didi

ios - 数组 append 不适用于 Swift

转载 作者:搜寻专家 更新时间:2023-10-31 19:39:44 25 4
gpt4 key购买 nike

我有这个功能,但是当我运行时:

theRecipes.append(theRecipe);

...数组“theRecipes”的大小完全相同。我将代码分享给您,这样您就可以大致了解我在 Swift 语言上尝试做的事情。

func retrieveAll() -> [Recipe] {
var query = PFQuery(className: RECIPE_PARSE_CLASSNAME);
var theRecipes: [Recipe] = [Recipe]();

query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]!, error: NSError!) -> Void in

if (error == nil) {
// The find succeeded.
println("Successfully retrieved \(objects.count) recipes.");

// Do something with the found objects
if let recipes = objects as? [PFObject] {
for recipe in recipes {
let theRecipe: Recipe = Recipe();
theRecipe.name = recipe[RECIPE_PARSE_NAME_NAME] as String;
theRecipe.about = recipe[RECIPE_PARSE_ABOUT_NAME] as String;
theRecipe.cover = recipe[RECIPE_PARSE_COVER_NAME] as String;
theRecipe.preview = recipe[RECIPE_PARSE_PREVIEW_NAME] as String;

println(theRecipe.name);
theRecipes.append(theRecipe);
}
}
} else {
// Log details of the failure
println("Error: \(error) \(error.userInfo!)");
Bugsnag.notify(nil, withData: ["data": error.description]);
}
};

return theRecipes;
}

我想做的是收集每个 PFObject 以将其转换为我自己的模型类,并在内存中以这种方式管理它。所以我翻译它并将其 append 到数组以交付它与食谱一起完成。

我想澄清一下,我已经检查过我成功地从 Parse 中检索了每个对象,并且它们不为空。

最佳答案

这是我的解决方案:

func retrieveAll(returner: (Array<Recipe>) -> Void) {
var query = PFQuery(className: RECIPE_PARSE_CLASSNAME);
var theRecipes: Array<Recipe> = Array<Recipe>();

query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]!, error: NSError!) -> Void in

if (error == nil) {
// The find succeeded.
println("Successfully retrieved \(objects.count) recipes.");

// Do something with the found objects
if let recipes = objects as? [PFObject] {
for recipe in recipes {
let theRecipe: Recipe = Recipe();
theRecipe.name = recipe[RECIPE_PARSE_NAME_NAME] as String;
theRecipe.about = recipe[RECIPE_PARSE_ABOUT_NAME] as String;
theRecipe.cover = recipe[RECIPE_PARSE_COVER_NAME] as String;
theRecipe.preview = recipe[RECIPE_PARSE_PREVIEW_NAME] as String;

println(theRecipe.name);
theRecipes.append(theRecipe);
}

returner(theRecipes);
} else {
println();
returner(Array<Recipe>());
}
} else {
// Log details of the failure
println("Error: \(error) \(error.userInfo!)");
Bugsnag.notify(nil, withData: ["data": error.description]);
returner(Array<Recipe>());
}
};
}

使用返回器方法异步传送我的映射食谱。

关于ios - 数组 append 不适用于 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29358487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com