无法在SWIFT中获取数组值 [英] Unable to get Array Value in SWIFT

查看:111
本文介绍了无法在SWIFT中获取数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从parse中检索了值,并在一个名为sto_tit的数组中逐个存储。
但是如果我尝试打印这些值,我就无法打印。它只在内部循环打印,但不能打印在FOR LOOP或OUTSIDE ELSE块之外,我不知道我的代码中有什么错误。请指导我!

I retrieved values from parse and stored one by one in an array called "sto_tit". But if I try to print the values, I unable to print. It is get printed inside loop only, but not get printed Outside FOR LOOP or OUTSIDE ELSE block, I don't know what error in my code. Kindly guide me!

我的代码如下:

func parse_db()
{

    var sto_tit = [String]() // EITHER GLOBAL DECLARATION or INSIDE BLOCK, SAME ISSUE
    // FACING

    par_query.selectKeys(["story_title"])
    par_query.findObjectsInBackgroundWithBlock({(NSArray objects, NSError error) in

        if (error != nil) {
            NSLog("error " + error.localizedDescription)
        }
        else
        {

            self.results = NSArray(array: objects)
            //NSLog("results %@", results)

            for i in 0...self.results.count
            {


                self.par_object = self.results.objectAtIndex(i) as PFObject
                var sto = self.par_object["story_title"] as String
                sto_tit.append(sto)
                println("Check_1 \(sto_tit)") // PRINTING [A],[AB],[ABC] ...

            } //FOR ENDS
            println("HELLO") // GETS SKIPPED
            println("Check_2 \(sto_tit)") // GETS SKIPPED

        } // ELSE ENDS
        println("Check_3 \(sto_tit)") // GETS SKIPPED

    }) //PARSE ENDS

    println("Check_4 \(sto_tit)") //PRINTING "Check_4 []"

} // BLOCK ENDS


推荐答案

我发布这个作为答案,因为它有一些代码。我希望这会对你有所帮助,但如果没有,请告诉我。

I'm posting this as an answer because it has some code. I hope this helps you, but if not please let me know.

我将以下代码粘贴到我的Swift应用程序中:

I pasted the following code into one of my Swift Apps:

var sto_tit:[String] = ["Yes", "No", "Maybe"];
if(false)
{

}else{

    for var i:Int  = 0; i < 3; i++
    {
        var sto = "story_title"
        sto_tit.append(sto)
        println("Check_1 \(sto_tit)")
    }

    println("Check_2 \(sto_tit)")
}
println("Check_3 \(sto_tit)")

打印结果符合预期:

Check_1 [是,否,可能,故事_title]

Check_1 [Yes, No, Maybe, story_title]

Check_1 [是,否,可能,story_title,story_title]

Check_1 [Yes, No, Maybe, story_title, story_title]

Check_1 [是,否,可能,故事_title,story_title, story_title]

Check_1 [Yes, No, Maybe, story_title, story_title, story_title]

Check_2 [是,否,可能,故事_title,story_title,story_title]

Check_2 [Yes, No, Maybe, story_title, story_title, story_title]

Check_3 [是的,不,可能,故事_title,story_title,story_title]

Check_3 [Yes, No, Maybe, story_title, story_title, story_title]

请注意,我的代码的某些细节与您的不同,但它是我能够快速到来的最接近的。

Note that there are some details of my code that are different from yours, but it's the closest I could come quickly.

另请注意,check_2不会被跳过。我认为您的代码中的某些内容并未完全按照原样发布。

Also note that check_2 does not get skipped. I think there's something in your code that is not being posted exactly as it is.

编辑:

这是一个有趣的异常,这段代码表现如预期:

Here's an interesting anomalie, This code behaves as expected:

var sto_tit:[String] = ["Yes", "No", "Maybe"];

println("sto_tit count = \(sto_tit.count)")

var j :Int = sto_tit.count

for var i:Int = 0; i < j; i++
{
    var sto = "story_title"
    sto_tit.append(sto)
    println("Check_1 \(sto_tit)")
}

但此代码将for循环发送到无限循环:

but this code sends the for loop into an infinite loop:

var sto_tit:[String] = ["Yes", "No", "Maybe"];

println("sto_tit count = \(sto_tit.count)")

for var i:Int = 0; i < sto_tit.count; i++
{
    var sto = "story_title"
    sto_tit.append(sto)
    println("Check_1 \(sto_tit)")
}

这是怎么回事?

这篇关于无法在SWIFT中获取数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆