ios - 数组 append 不适用于 Swift

标签 ios arrays swift asynchronous append

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

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/

相关文章:

ios - 仅在顶部停止过度滚动 UITableView?

ios - CMTimeMake 用户选择器

php - 尝试从数组中删除重复的行。到栏目内容

c - 如何检查以下数组是否包含这些字符?

swift - "Charts"(IOS) xcode 9.0 编译错误 : "static var ' defaultFormatter ' is not public"

ios - Firebase Firestore 和 Protobuf 的词法或前身问题,这两个文件夹是问题所在

ios - Facebook 图形 API 链接参数返回 nil

ios - 在 appdelegate 中设置 UIButton 的外观 - setClipsToBounds

iOS SQLite 插入和选择不起作用?

python - 在 numpy 一维数组中查找拐点和固定点