无法更改匿名块中的实例变量 (Swift/Parse.com)
Cannot change instance variables in anonymous block (Swift/Parse.com)
Parse Data 是一个 PFObject
class,其中包含我正在查询的 "Companies" 数据。获取数据并将其分配为 ParseData
对象工作正常,但问题是将其放入我的 list
ivar。从输出中可以看出,none 个 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
method.You 之前是否无法将您的异步代码编写为同步代码。试着在下面写下你的打印代码 self.testString = "no matter"
。祝你好运!
Parse Data 是一个 PFObject
class,其中包含我正在查询的 "Companies" 数据。获取数据并将其分配为 ParseData
对象工作正常,但问题是将其放入我的 list
ivar。从输出中可以看出,none 个 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
method.You 之前是否无法将您的异步代码编写为同步代码。试着在下面写下你的打印代码 self.testString = "no matter"
。祝你好运!