数组追加不适用于 Swift
Array append not working on Swift
我有这个功能,但是当我运行:
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 以将其转换为我自己的模型 class 并在内存中以这种方式管理它。所以我翻译它并将其附加到数组以交付它与食谱一起完成。
我想澄清一下,我已经检查过我成功地从 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>());
}
};
}
使用返回器方法异步传送我映射的食谱。
我有这个功能,但是当我运行:
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 以将其转换为我自己的模型 class 并在内存中以这种方式管理它。所以我翻译它并将其附加到数组以交付它与食谱一起完成。
我想澄清一下,我已经检查过我成功地从 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>());
}
};
}
使用返回器方法异步传送我映射的食谱。