如何访问闭包外的数组?
Posted
技术标签:
【中文标题】如何访问闭包外的数组?【英文标题】:How to access Array outside Closure? 【发布时间】:2017-02-22 04:44:16 【问题描述】:我正在尝试下载图像,然后在滚动视图中显示它们。
首先从Firebase
检索链接作为字符串
字符串转换为数组
DLImageLoader
下载图像“i”并将其显示在图像视图中。
我使用本地图像测试了滚动视图,它可以正常工作。当我使用下面的代码时,我收到错误“超出索引”。 我正在使用 Swift 3
override func viewDidLoad()
super.viewDidLoad()
FIRDatabase.database().reference(withPath: "Main data").child("Home Image URLs").observeSingleEvent(of: .value, with: (snapshot) in
if let snapString = snapshot.value as? String
self.imageURLsVAR = snapString
self.imageURLs = self.imageURLsVAR.components(separatedBy: ",")
// self.label.text = String(self.imageURLs[1])
) (error) in
print(error.localizedDescription)
for i in 0..<imageURLs.count
let imageview = UIImageView()
// imageview.image = homeImages[i]
dlURL = String(imageURLs[i])
DLImageLoader.sharedInstance().image(fromUrl: String(imageURLs[i]), imageView: imageview)
imageview.contentMode = .scaleAspectFit
let xPosition = self.view.frame.width * CGFloat(i)
imageview.frame = CGRect(x: xPosition, y: 0, width: self.imgScrollView.frame.width, height: self.imgScrollView.frame.height)
imgScrollView.contentSize.width = imgScrollView.frame.width * CGFloat(i + 1)
imgScrollView.addSubview(imageview)
view.sendSubview(toBack: imgScrollView)
2017-02-24 22:25:20.037: <FIRMessaging/INFO> FIRMessaging library version 1.2.2
2017-02-24 22:25:20.045: <FIRMessaging/WARNING> FIRMessaging AppDelegate proxy enabled, will swizzle app delegate remote notification receiver handlers. Add "FirebaseAppDelegateProxyEnabled" to your Info.plist and set it to NO
2017-02-24 22:25:20.118 tda[1828] <Notice> [Firebase/Crash][I-CRA000004] Successfully initialized
2017-02-24 22:25:20.119 tda[1828] <Notice> [Firebase/Analytics][I-ACS023007] Firebase Analytics v.3700000 started
2017-02-24 22:25:20.120 tda[1828] <Notice> [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled
2017-02-24 22:25:20.134 tda[1828] <Notice> [Firebase/Analytics][I-ACS003007] Successfully created Firebase Analytics App Delegate Proxy automatically. To disable the proxy, set the flag FirebaseAppDelegateProxyEnabled to NO in the Info.plist
2017-02-24 22:25:20.209 tda[1828] <Warning> [Firebase/Analytics][I-ACS005000] The AdSupport Framework is not currently linked. Some features will not function properly.
2017-02-24 22:25:20.214 tda[1828] <Notice> [Firebase/Analytics][I-ACS023012] Firebase Analytics enabled
correct
fatal error: Index out of range
【问题讨论】:
【参考方案1】:你正在块(闭包)中获取图像,所以你必须在块中编写图像加载代码试试这个
override func viewDidLoad()
super.viewDidLoad()
FIRDatabase.database().reference(withPath: "Main data").child("Home Image URLs").observeSingleEvent(of: .value, with: (snapshot) in
if let snapString = snapshot.value as? String
self.imageURLsVAR = snapString
self.imageURLs = self.imageURLsVAR.components(separatedBy: ",")
// self.label.text = String(self.imageURLs[1])
for i in 0..< imageURLs.count-1
let imageview = UIImageView()
// imageview.image = homeImages[i]
dlURL = String(imageURLs[i])
DLImageLoader.sharedInstance().image(fromUrl: String(imageURLs[i]), imageView: imageview)
imageview.contentMode = .scaleAspectFit
let xPosition = self.view.frame.width * CGFloat(i)
imageview.frame = CGRect(x: xPosition, y: 0, width: self.imgScrollView.frame.width, height: self.imgScrollView.frame.height)
imgScrollView.contentSize.width = imgScrollView.frame.width * CGFloat(i + 1)
imgScrollView.addSubview(imageview)
view.sendSubview(toBack: imgScrollView)
) (error) in
print(error.localizedDescription)
【讨论】:
我得到同样的错误。此外,当我添加您提供的编辑代码时,它说要在几乎所有内容中添加“自我”。 @varunNaharia 只需像 for i in 0 一样运行循环.. 我仍然遇到同样的错误。除了在我检索它的两个大括号之间,我无法在其他任何地方访问数组或变量。 @varunNaharia 我还能如何从其他地方的“块”访问数据? @varunNaharia 更改代码后如何得到同样的错误,请在此处打印你得到的错误以上是关于如何访问闭包外的数组?的主要内容,如果未能解决你的问题,请参考以下文章