我如何访问数组中的所有值?

时间:2017-06-01 16:36:40

标签: ios arrays swift

我添加了一个带有URL的数组

[FirebaseTest.storiesContent(storyUrl: https://firebasestorage.googleapis.com/v0/b/motive-73352.appspot.com/o/Content%2F20170525130622.jpg?alt=media&token=1e654c60-2f47-43c3-9298-b0282d27f66c), FirebaseTest.storiesContent(storyUrl: https://firebasestorage.googleapis.com/v0/b/motive-73352.appspot.com/o/20170525131400.mp4?alt=media&token=30fd962d-c305-4fa4-955d-dbb06ef91623), FirebaseTest.storiesContent(storyUrl: nil)]

为了创建这个数组,我使用一个结构作为基础来附加内容

struct storiesContent {

var storyUrl : String!

}

但是,我不确定如何从这个数组中获取这些URL,以便使用SDWebImage重复下载每个图像,以便将每个图像附加到UIImage的数组中。我对Swift很新,所以我的理解是有限的。

2 个答案:

答案 0 :(得分:0)

我真的没有使用Firebase,但是根据我的理解,您希望从每个链接下载图像并将所有图像保存在一个数组中。你可以这样做:

//Get all URLS in an NSArray
let urlsArray:NSArray = ["https://firebasestorage.googleapis.com/v0/b/motive-73352.appspot.com/o/Content%2F20170525130622.jpg?alt=media&token=1e654c60-2f47-43c3-9298-b0282d27f66c","https://firebasestorage.googleapis.com/v0/b/motive-73352.appspot.com/o/20170525131400.mp4?alt=media&token=30fd962d-c305-4fa4-955d-dbb06ef91623"]

//Create a NSMutableArray where the final images will be saved.
let imagesArray:NSMutableArray! = NSMutableArray()

//Create a for that checks every link in the urlsArray.
for x in 0..<urlsArray.count
{
   //Set the urlsArray content at position x as a URL
   let imageUrl:URL = URL(string: urlsArray.object(at: x) as! String)!

   //Generate a request with the current imageUrl.
   let request:URLRequest = URLRequest.init(url: imageUrl)

   //Start a NSURLConnection and get a Data that represents your image.
   NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main, completionHandler: { (response, imageDta, error) in

       //Store the received data as an UIImage.
       let imageReceived:UIImage = UIImage(data: imageDta!)!

       //Save the image to our imagesArray.
       imagesArray.add(imageReceived)
   })
   //The Process loops until you get all the images.
}

更新

当然可以,这里唯一的就是我从你的数组中删除了你的最后一个对象,因为它包含一个nil对象而Swift可以没有蜡烛nil对象:

//Using the NSArray style you're using.
let yourFireBaseArray = [FirebaseTest.storiesContent(storyUrl: https://firebasestorage.googleapis.com/v0/b/motive-73352.appspot.com/o/Content%2F20170525130622.jpg?alt=media&token=1e654c60-2f47-43c3-9298-b0282d27f66c), FirebaseTest.storiesContent(storyUrl: https://firebasestorage.googleapis.com/v0/b/motive-73352.appspot.com/o/20170525131400.mp4?alt=media&token=30fd962d-c305-4fa4-955d-dbb06ef91623)]

//Create a NSMutableArray where the final images will be saved.
let imagesArray:NSMutableArray! = NSMutableArray()

//Create a for that checks every link in the yourFireBaseArray.
for x in 0..<yourFireBaseArray.count
{
   //Get your current array position string as a storiesContent object
   let fireBaseString:storiesContent = yourFireBaseArray.object(at: x) as! storiesContent 

   //Use your fireBaseString object, get the storyURL string and set it in an URL.
   let imageUrl:URL = URL(string: fireBaseString.storyURL)!

   //Generate a request with the current imageUrl.
   let request:URLRequest = URLRequest.init(url: imageUrl)

   //Start a NSURLConnection and get a Data that represents your image.
   NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main, completionHandler: { (response, imageDta, error) in

       //Store the received data as an UIImage.
       let imageReceived:UIImage = UIImage(data: imageDta!)!

       //Save the image to our imagesArray.
       imagesArray.add(imageReceived)
   })
   //The Process loops until you get all the images.
}

更新2

好的,这是我的示例项目,复制和粘贴,它将为您提供结果图像。

import UIKit

class ViewController: UIViewController {

@IBOutlet var image:UIImageView!
var urlArray:NSMutableArray! = NSMutableArray()
var imagesArray:NSMutableArray! = NSMutableArray()

override func viewDidLoad() {
    super.viewDidLoad()
    urlArray = NSMutableArray.init(array: ["https://firebasestorage.googleapis.com/v0/b/motive-73352.appspot.com/o/Content%2F20170525130622.jpg?alt=media&token=1e654c60-2f47-43c3-9298-b0282d27f66c"])
    // Do any additional setup after loading the view, typically from a nib.
}

override func viewDidAppear(_ animated: Bool) {
    for x in 0..<urlArray.count
    {
        let imageUrl:URL = URL(string: "\(urlArray.object(at: x) as! String)")!
        let request:URLRequest = URLRequest.init(url: imageUrl)
        NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main, completionHandler: { (response, imageDta, error) in
            if (error == nil)
            {
                self.imagesArray.add(UIImage(data: imageDta!)!)
                if self.imagesArray.count > 0
                {
                    self.image.image = self.imagesArray.object(at: 0) as! UIImage
                }
            }else{
                print("ERROR - \(error!)")
            }
        })
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

enter image description here

答案 1 :(得分:0)

要从Firebase中检索数据,将属性列表文件添加到项目中,它将保存对firbase中数据库的引用。

//Declare an array of string 
var myArray = [String]()

//call this when your view load or in viewDidLoad()
ref = FIRDatabase.database().reference()

ref?.child("path").observe(.value, with: { (snapshot) in
        if let snap = snapshot.value as? [String]{
        self.myArray = snap
        }
    })

然后根据自己的需要对阵列进行menupulate。