【问题标题】:Smart way to group PHAsset fetch results by date按日期对 PHAsset 获取结果进行分组的智能方法
【发布时间】:2015-07-30 05:53:49
【问题描述】:

与我正在做的事情相比,有没有更聪明/更合适的方法? 我创建了一本字典,然后通过查看每个获取的资产继续枚举和填写年/月/日。

        let assetFetchResult = PHAsset.fetchAssetsInAssetCollection(album, options: assetFetchOptions)
        if assetFetchResult.count > 0 {
            var fetchedAssets = [String:[String:[String:[PHAsset]]]]()
            //[year[month[date:arrayOfPhotos]]]

            assetFetchResult.enumerateObjectsUsingBlock({
                object, index, stop in

                let asset:PHAsset = object as! PHAsset
                let dateComponents = NSCalendar.currentCalendar().components(.CalendarUnitDay | .CalendarUnitMonth | .CalendarUnitYear, fromDate: asset.creationDate)

                //year group creation
                if fetchedAssets["\(dateComponents.year)"] == nil {
                    fetchedAssets["\(dateComponents.year)"] = [String:[String:[PHAsset]]]()
                }
                //monthly group creation
                if fetchedAssets["\(dateComponents.year)"]!["\(dateComponents.month)"] == nil  {
                    fetchedAssets["\(dateComponents.year)"]!["\(dateComponents.month)"] = [String:[PHAsset]]()
                }

                //daily group creation
                if fetchedAssets["\(dateComponents.year)"]!["\(dateComponents.month)"]!["\(dateComponents.day)"] == nil  {
                    fetchedAssets["\(dateComponents.year)"]!["\(dateComponents.month)"]!["\(dateComponents.day)"] = [PHAsset]()
                }

                fetchedAssets["\(dateComponents.year)"]!["\(dateComponents.month)"]!["\(dateComponents.day)"]?.append(asset)
            })
            println(fetchedAssets)
            return fetchedAssets
        }

【问题讨论】:

    标签: ios swift phasset


    【解决方案1】:

    我建议直接使用Dictionary grouping,更安全和自动管理,如下所述:https://developer.apple.com/documentation/swift/dictionary/3127163-init

    这可以是您的优化代码:

    let fetchedAssets = Dictionary(grouping: <#assets-array#>) { asset -> DateComponents in
      return Calendar.current.dateComponents([.day, .year, .month], from: (asset.creationDate)!)
    }
    print( fetchedAssets )
    

    您将在控制台中收到如下信息:

    (lldb) po fetchedAssets.first
    ▿ Optional<(key: DateComponents, value: Array<Assets>)>
      ▿ some : 2 elements
        ▿ key : year: 2021 month: 5 day: 18 isLeapMonth: false 
          - year : 2021
          - month : 5
          - day : 18
          - isLeapMonth : false
        ▿ value : 1 element
          ▿ 0 : Assets
            - asset : <PHAsset: 0x7fcebcc43740> A86BC4A1-63E3-41EB-897D-695A395531D6/L0/001 mediaType=1/4, sourceType=1, (2112x1590), creationDate=2021-05-18 11:19:23 +0000, location=0, hidden=0, favorite=0, adjusted=0 
    
    (lldb)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      相关资源
      最近更新 更多