问题
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!
MY CODE BELOW:
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
回答1:
I'm posting this as an answer because it has some code. I hope this helps you, but if not please let me know.
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)")
The printed result was as expected:
Check_1 [Yes, No, Maybe, story_title]
Check_1 [Yes, No, Maybe, story_title, story_title]
Check_1 [Yes, No, Maybe, story_title, story_title, story_title]
Check_2 [Yes, No, Maybe, story_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.
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.
EDIT:
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)")
}
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)")
}
What's up with this?
来源:https://stackoverflow.com/questions/27861216/unable-to-get-array-value-in-swift