我是 Swift 新手,但不断学习。我有一个使用 for..loop 从 AWS DynamoDB 表获取数据的函数。我正在循环内的结构变量中捕获表项的字段值,但是当我在 TableView 中调用结构变量时,它在 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
}
}
后来,我在 TableView 中调用这个DatabaseClass,如下所示:
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扫描命令并在tableview中返回[],但以某种方式转到该函数并执行数据。它无法捕获结构变量中的数据。我已经尝试解决这个问题很多天了。
最佳答案
您的静态内存有问题。我建议不要使用静态值,这是一个非常糟糕的做法。 解决方案可能是在 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)
}
}
关于Swift 4 - 结构静态变量不显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51178467/