我是新手,但很快就可以学习。我有一个使用for..loop从AWS DynamoDB表获取数据的功能。我正在循环内的结构变量中捕获表项的字段值,但是当我在表视图中调用结构变量时,它在Xcode的日志屏幕中显示空白数组,例如[]。这是代码:
import Foundation
import SQLite3
import UIKit
import AWSDynamoDB
struct MyVariables {
static var empDict: [String: String] = [:]
static var arrayEmployees = Array<Dictionary<String, String>>()
}
// This is a class defined to hold various methods to be used throughout the app.
class DatabaseClass: UIViewController, UITextFieldDelegate
{
// other methods within DatabaseClass
// ........
//
//
// Scan DynamoDB table for employees
func scanEmployees () {
let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.default()
let scanExpression = AWSDynamoDBScanExpression()
scanExpression.limit = 20
dynamoDBObjectMapper.scan(Employees.self, expression: scanExpression).continueWith(block: { (task:AWSTask<AWSDynamoDBPaginatedOutput>!) -> Any? in
if let error = task.error as NSError? {
print("The request failed. Error: \(error)")
}
let paginatedOutput = task.result!
for emp in paginatedOutput.items as! [Employees] {
if emp._empid != nil {
MyVariables.empDict["Empid"] = emp._empid
} else {
MyVariables.empDict["Empid"] = nil
}
if emp._email != nil {
MyVariables.empDict["email"] = emp._email
} else {
MyVariables.empDict["email"] = nil
}
if emp._firstname != nil {
MyVariables.empDict["firstname"] = emp._firstname
} else {
MyVariables.empDict["firstname"] = nil
}
if emp._lastname != nil {
MyVariables.empDict["lastname"] = emp._lastname
} else {
MyVariables.empDict["lastname"] = nil
}
if emp._location != nil {
MyVariables.empDict["location"] = emp._location
} else {
MyVariables.empDict["location"] = nil
}
if emp._mobile != nil {
MyVariables.empDict["mobile"] = emp._mobile
} else {
MyVariables.empDict["mobile"] = nil
}
if emp._work != nil {
MyVariables.empDict["work"] = emp._work
} else {
MyVariables.empDict["work"] = nil
}
if emp._site != nil {
MyVariables.empDict["site"] = emp._site
} else {
MyVariables.empDict["site"] = nil
}
MyVariables.arrayEmployees.append(MyVariables.empDict)
//print(MyVariables.arrayEmployees)
} // for loop
print ("printing array employees just after for loop")
print(MyVariables.arrayEmployees)
// The above print works
return nil
})
// print(MyVariables.arrayEmployees)
// Above doesn’t print
}
}
稍后,我在表视图中将此数据库类称为:
class JsonParseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let createEmp = DatabaseClass()
createEmp.scanEmployees()
print("in JsonParseViewController, calling scan function")
print(MyVariables.arrayEmployees)
在模拟器中运行时,日志文件显示以下内容:
in JsonParseViewController, calling scan function
[]
printing array employees just after for loop
[["location": "XXXXXX", "email": "johnSmith1@email.com", "firstname": "John1", "lastname": "Smith1", "mobile": "123-456-7890", "work": "123-456-7890", "Empid": "12345", "site": "KKKKKK"], ["location": "YYYYY", "email": "johnSmith8@email.com", "firstname": "John8", "lastname": "Smith8", "mobile": "123-456-7890", "work": "123-456-7890", "Empid": "12415", "site": "ZZZZZ"], ......etc]]
在执行函数scanEmployees时,似乎跳过了DynamoDB扫描命令并在表视图中返回[],但是以某种方式转到函数并执行了数据。它无法捕获结构变量中的数据。我有很多天试图解决这个问题。
答案 0 :(得分:0)
您错过了异步呼叫
func scanEmployees (completion:@escaping ()-> Void) {
let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.default()
let scanExpression = AWSDynamoDBScanExpression()
scanExpression.limit = 20
dynamoDBObjectMapper.scan(Employees.self, expression: scanExpression).continueWith(block: { (task:AWSTask<AWSDynamoDBPaginatedOutput>!) -> Any? in
if let error = task.error as NSError? {
print("The request failed. Error: \(error)")
}
let paginatedOutput = task.result!
for emp in paginatedOutput.items as! [Employees] {
myVariables.empDict["Empid"] = emp._empid
myVariables.empDict["email"] = emp._email
myVariables.empDict["firstname"] = emp._firstname
myVariables.empDict["lastname"] = emp._lastname
myVariables.empDict["location"] = emp._location
myVariables.empDict["mobile"] = emp._mobile
myVariables.empDict["work"] = emp._work
myVariables.empDict["site"] = emp._site
MyVariables.arrayEmployees.append(MyVariables.empDict)
//print(MyVariables.arrayEmployees)
} // for loop
print ("printing array employees just after for loop")
print(MyVariables.arrayEmployees)
// The above print works
completion()
return nil
}
)
// print(MyVariables.arrayEmployees)
// Above doesn’t print
}
//
打电话时
createEmp.scanEmployees {
print(MyVariables.arrayEmployees)
}
注意::我建议使用 SwiftyJson 或 JSONDecoder 代替手动解析密钥
答案 1 :(得分:0)
您的静态内存有问题。我建议不要使用静态值,这是非常糟糕的做法。 解决的办法可能是在类DatabaseClass中创建一个结构。例如,var myVariables = MyVariables()并以相同的方式填充它。现在,您可以选择返回它或从DatabaseClass对象使用它。现在,我将显示示例的外观。
struct MyVariables {
var empDict: [String: String] = [:]
var arrayEmployees = Array<Dictionary<String, String>>()
}
// This is a class defined to hold various methods to be used throughout the app.
class DatabaseClass: UIViewController, UITextFieldDelegate
{
// other methods within DatabaseClass
// ........
//
//
// Scan DynamoDB table for employees
public var myVariables = MyVariables()
func scanEmployees () {
let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.default()
let scanExpression = AWSDynamoDBScanExpression()
scanExpression.limit = 20
dynamoDBObjectMapper.scan(Employees.self, expression: scanExpression).continueWith(block: { (task:AWSTask<AWSDynamoDBPaginatedOutput>!) -> Any? in
if let error = task.error as NSError? {
print("The request failed. Error: \(error)")
}
let paginatedOutput = task.result!
for emp in paginatedOutput.items as! [Employees] {
myVariables.empDict["Empid"] = emp._empid
myVariables.empDict["email"] = emp._email
myVariables.empDict["firstname"] = emp._firstname
myVariables.empDict["lastname"] = emp._lastname
myVariables.empDict["location"] = emp._location
myVariables.empDict["mobile"] = emp._mobile
myVariables.empDict["work"] = emp._work
myVariables.empDict["site"] = emp._site
myVariables.arrayEmployees.append(myVariables.empDict)
//print(myVariables.arrayEmployees)
} // for loop
print ("printing array employees just after for loop")
print(myVariables.arrayEmployees)
// The above print works
return nil
})
// print(myVariables.arrayEmployees)
// Above doesn’t print
}
}
//then use it in this way
class JsonParseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let createEmp = DatabaseClass()
createEmp.scanEmployees()
print("in JsonParseViewController, calling scan function")
print(createEmp.myVariables.arrayEmployees)
}
}