Swift 无法从其他 Swift 文件中提取信息,除非在 @IBAction 中
Swift unable to pull information from other Swift files unless in @IBAction
我目前正在使用最新版本的 Swift。简而言之,我从网页中提取信息并将所述信息存储到一个数组中。这是我的做法(请原谅缩进..):
class TransactionData {
var transactions: [Transaction] = []
init() {
getTransactionData()
}
func getTransactionData() {
let jsonUrl = "php file with json"
let session = NSURLSession.sharedSession()
let shotsUrl = NSURL(string: jsonUrl)
let task = session.dataTaskWithURL(shotsUrl!) {
(data, response, error) -> Void in
do {
let jsonData: NSArray = (try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as? NSArray)!
for var index = 0; index < jsonData.count; ++index {
let orderID: String = jsonData[index]["orderID"] as! String
let orderDate: String = jsonData[index]["orderDate"] as! String
let orderType: String = jsonData[index]["orderType"] as! String
let paymentType: String = jsonData[index]["paymentType"] as! String
let itemName: String = jsonData[index]["itemName"] as! String
let itemPrice: String = jsonData[index]["itemPrice"] as! String
let itemTaxes: String = jsonData[index]["itemTaxes"] as! String
let orderModifications: String = jsonData[index]["orderModifications"] as! String
let orderVariations: String = jsonData[index]["orderVariations"] as! String
let transaction = Transaction(orderID: orderID, orderDate: orderDate, orderType: orderType, paymentType: paymentType, itemName: itemName, itemPrice: itemPrice, itemTaxes: itemTaxes, orderModifications: orderModifications, orderVariations: orderVariations)
self.transactions.append(transaction)
}
} catch _ {
// Error
}
}
task.resume()
}
当我要调用信息时,我用这个:
let transactionData = TransactionData()
for transaction in transactionData.transactions {
print("\(transaction)")
}
唯一一次将信息传递给 ViewController 是在我使用 IBAction 时。如果我在其他任何地方尝试,它都不会通读信息。例如,我试图从在线网站中提取信息以传递到表 ViewController 中。它只是不会提取信息。
有什么想法吗?
您需要在 ViewController
之间建立连接,以便它们可以在它们之间传递数据。一种常见的方法是通过在屏幕之间的转场期间设置的 delegate
协议。这里有一个tutorial就可以了。
稍微扩展一下,您的 ViewController 调用了一个 class Transactions
来加载数据。如果您随后尝试访问 TableViewController 中的一个新实例,则您没有数据,因为正在创建一个新实例。有两种方法可以避免此问题:
- 通过
delegate
. 从 VC -> TVC 传递数据(或对您的 Transactions
的引用)
- 为您的数据模型使用
singleton
模式,以便所有人都可以访问它。
为了避免并发问题,我建议使用前者。
我目前正在使用最新版本的 Swift。简而言之,我从网页中提取信息并将所述信息存储到一个数组中。这是我的做法(请原谅缩进..):
class TransactionData {
var transactions: [Transaction] = []
init() {
getTransactionData()
}
func getTransactionData() {
let jsonUrl = "php file with json"
let session = NSURLSession.sharedSession()
let shotsUrl = NSURL(string: jsonUrl)
let task = session.dataTaskWithURL(shotsUrl!) {
(data, response, error) -> Void in
do {
let jsonData: NSArray = (try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as? NSArray)!
for var index = 0; index < jsonData.count; ++index {
let orderID: String = jsonData[index]["orderID"] as! String
let orderDate: String = jsonData[index]["orderDate"] as! String
let orderType: String = jsonData[index]["orderType"] as! String
let paymentType: String = jsonData[index]["paymentType"] as! String
let itemName: String = jsonData[index]["itemName"] as! String
let itemPrice: String = jsonData[index]["itemPrice"] as! String
let itemTaxes: String = jsonData[index]["itemTaxes"] as! String
let orderModifications: String = jsonData[index]["orderModifications"] as! String
let orderVariations: String = jsonData[index]["orderVariations"] as! String
let transaction = Transaction(orderID: orderID, orderDate: orderDate, orderType: orderType, paymentType: paymentType, itemName: itemName, itemPrice: itemPrice, itemTaxes: itemTaxes, orderModifications: orderModifications, orderVariations: orderVariations)
self.transactions.append(transaction)
}
} catch _ {
// Error
}
}
task.resume()
}
当我要调用信息时,我用这个:
let transactionData = TransactionData()
for transaction in transactionData.transactions {
print("\(transaction)")
}
唯一一次将信息传递给 ViewController 是在我使用 IBAction 时。如果我在其他任何地方尝试,它都不会通读信息。例如,我试图从在线网站中提取信息以传递到表 ViewController 中。它只是不会提取信息。
有什么想法吗?
您需要在 ViewController
之间建立连接,以便它们可以在它们之间传递数据。一种常见的方法是通过在屏幕之间的转场期间设置的 delegate
协议。这里有一个tutorial就可以了。
稍微扩展一下,您的 ViewController 调用了一个 class Transactions
来加载数据。如果您随后尝试访问 TableViewController 中的一个新实例,则您没有数据,因为正在创建一个新实例。有两种方法可以避免此问题:
- 通过
delegate
. 从 VC -> TVC 传递数据(或对您的 - 为您的数据模型使用
singleton
模式,以便所有人都可以访问它。
Transactions
的引用)
为了避免并发问题,我建议使用前者。