【问题标题】:Array append not working on Swift数组追加在 Swift 上不起作用
【发布时间】:2015-06-04 04:38:12
【问题描述】:

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

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 以将其转换为我自己的模型类,并在内存中以这种方式管理它。所以我翻译它并将它附加到数组中,以提供它与食谱相匹配。

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

【问题讨论】:

  • 您不能从使用异步方法获取数据的方法中返回这样的数组。 return 语句将在 findObjectsInBackgroundWithBlock 中的块执行之前立即运行。
  • 你完全正确。我马上修。但即便如此,当我在追加后调试一行时,数组仍有0个元素是否正常?
  • 你是在调试器中还是在日志中这样做的?如果您在添加后立即将食谱记录在该行中,则它应该显示正确的元素数量。
  • 为什么 Parse "fetchInBackground" 问题变得像 Java 字符串比较与 == 问题一样普遍?
  • @rdelmar 什么是解决方案呢?我遇到了同样的问题,我需要从异步方法返回一个数组。

标签: ios arrays swift asynchronous append


【解决方案1】:

这是我的解决方案:

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>());
        }
    };
}

使用返回方法异步传递我的映射配方。

【讨论】:

    猜你喜欢
    • 2018-07-28
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 2016-11-16
    • 2011-08-22
    • 1970-01-01
    • 2011-11-10
    • 2014-04-15
    相关资源
    最近更新 更多