如何在Closure外部访问数组?

时间:2017-02-22 04:44:17

标签: ios arrays xcode uiscrollview uiimageview

我尝试下载图片,然后在滚动视图中显示它们。

  • 首先,链接从Firebase作为字符串
  • 检索
  • 字符串转换为数组
  • DLImageLoader下载图片'我'并在图像视图中显示它。

我使用本地图像测试了滚动视图,但它确实有效。当我使用下面的代码时,我得到错误' out of index'。 我使用的是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 个答案:

答案 0 :(得分:0)

你正在获取块中的图像(Closure)所以你必须在块中编写图像加载代码试试这个

console.log()