解析数据是一个PFObject
类,其中包含我要查询的“公司”数据。获取数据并将其分配为ParseData
对象可以很好地工作,但是问题是将其放入了我的list
ivar中。从输出中可以看到,匿名块中的所有ivar均未更改。是否有解决方法或解决方案?
import Foundation
class AllCompanies: NSObject {
var list:[ParseData] = []
var testList:[String] = []
var testString:String = "butter"
override init()
{
super.init()
getCompanies()
}
func getCompanies()
{
let query = ParseData.query()!
query.findObjectsInBackgroundWithBlock { objects, error in
if error == nil
{
for company in objects!
{
let newCompany:ParseData = ParseData()
newCompany.name = company.objectForKey("Name") as! String
newCompany.logo = company.objectForKey("Logo") as! PFFile
self.list.append(newCompany)
self.testList.append("here")
self.testString = "no matter"
}
}
else
{
print("Error: \(error) \(error?.userInfo)")
}
}
}
}
方法调用:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.setApplicationId("m0zIvk7nfP6nEUrGYzyecbhRdqTrhbUoBI00fvZ4", clientKey: "lmqPfyrkeq4p8v6cukV7aFCVdi4evb8MFyjgvnEG")
PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
let allCompanies = AllCompanies()
print("\(allCompanies.list)")
print("\(allCompanies.testList)")
print("\(allCompanies.testString)")
return true
}
最佳答案
getCompanies
方法是异步的吗?您需要确定是否在print
方法之前运行findObjectsInBackgroundWithBlock
命令。您不能将异步代码编写为同步代码。尝试将您的打印代码写在self.testString = "no matter"
下面。祝您好运!