作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有这个功能,但是当我运行时:
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/
我是一名优秀的程序员,十分优秀!