使用函数

时间:2017-06-20 15:07:39

标签: swift function firebase firebase-realtime-database uicollectionview

我想获取帖子的数量,然后将返回的值分配给变量。

我得到了正确的帖子数,但变量没有变化。

这是我如何获取计数(工作正常)

注意:这在我的api中

   func fetchVideosFailsCount( completion: @escaping (Int) -> Void) {
    API.Kategorien.REF_FAILS_VIDEOS.observe(.value, with: {
        snapshot in
        let count = Int(snapshot.childrenCount)
        completion(count)
    })
}

现在我想将它分配给另一个类中的变量

        var anzahlInFails : String = ""
    print("anzahl ist", anzahlInFails)


    API.Kategorien.fetchVideosFailsCount() { (count) in
       let one = "\(count)"
        anzahlInFails = one
        print(one)
    }

    print("anzahl ist", anzahlInFails)

我想通过一个代表的值来操纵anzahlInFails。

相反,两个print语句都会向我发送一个“”作为值。

我有这方面的问题,因为我没有可以分配的标签(没有问题),因为我在集合视图中工作。

课程视频正在关注

class Videos {
var title = ""
var featuredImage: UIImage
var color: UIColor
var anzahlBeiträge : String

init(title: String, featuredImage: UIImage, color: UIColor, anzahlBeiträge: String)
{
    self.anzahlBeiträge = anzahlBeiträge
    self.title = title
    self.featuredImage = featuredImage
    self.color = color
}

static func fetchInterests() -> [Videos]
{



    var anzahlInFails : String = ""
    print("anzahl ist", anzahlInFails)


    API.Kategorien.fetchVideosFailsCount() { (count) in
       let one = "\(count)"
        anzahlInFails = one
        print(one)
    }

    print("anzahl ist", anzahlInFails)

    return [


        Videos(title: "Cat1", featuredImage: UIImage(named: "img1")!, color: UIColor(red: 224/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.4), anzahlBeiträge: " Beiträge geteil"),
        Videos(title: "Cat2", featuredImage: UIImage(named: "img1")!, color: UIColor(red: 224/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.4), anzahlBeiträge: "142 Beiträge geteil"),
        Videos(title: "Cat3", featuredImage: UIImage(named: "img1")!, color: UIColor(red: 224/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.4), anzahlBeiträge: "39432 Beiträge geteil"),
        Videos(title: "Cat4", featuredImage: UIImage(named: "img1")!, color: UIColor(red: 224/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.4), anzahlBeiträge: "2482 Beiträge geteil"),
        Videos(title: "Cat15", featuredImage: UIImage(named: "img1")!, color: UIColor(red: 224/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.4), anzahlBeiträge: "67912 Beiträge geteil"),
    ]
}

}

我希望每个{anzahlBeiträge}代表我使用的函数的实际值。

有任何帮助吗? 提前谢谢:)

更新:获取帖子数量的当前代码

   func counterVideosFails(completion: @escaping (_ count: Int)->()) {
        API.Kategorien.REF_FAILS_VIDEOS.observe(.value, with: { (snapshot) in
            let count = Int(snapshot.childrenCount)
            completion(count)
        })
    }


    counterVideosFails() { (count)
    in
      let  countOfFailsVideos = count
        print("countOfFailsVideos", countOfFailsVideos )

    }

0 个答案:

没有答案