swift中Dictionary的grouping by使用

swift中Dictionary的grouping by使用今天在写一个功能的时候用到了Dictionary的groupingby这个用法,代码先贴出来importUIKitclassAlignFlowLayout:UICollectionViewFlowLayout{requiredinit(itemSize:CGSize=CGSize.zero,minimumInteritemSpacing:CGFloat=0,minimumLineSpacing:CGFloat=0,sectionInset:

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

今天在写一个功能的时候用到了Dictionary 的 grouping by 这个用法,代码先贴出来

import UIKit

class AlignFlowLayout: UICollectionViewFlowLayout { 
   
    
    required init(itemSize: CGSize = CGSize.zero, minimumInteritemSpacing: CGFloat = 0, minimumLineSpacing: CGFloat = 0, sectionInset: UIEdgeInsets = .zero) { 
   
        super.init()

        self.itemSize = itemSize
        self.minimumInteritemSpacing = minimumInteritemSpacing
        self.minimumLineSpacing = minimumLineSpacing
        self.sectionInset = sectionInset
        
    }

    required init?(coder aDecoder: NSCoder) { 
   
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 
   
        let layoutAttributes = super.layoutAttributesForElements(in: rect)!.map { 
    $0.copy() as! UICollectionViewLayoutAttributes }
        guard scrollDirection == .vertical else { 
    return layoutAttributes }

        // Filter attributes to compute only cell attributes
        let cellAttributes = layoutAttributes.filter({ 
    $0.representedElementCategory == .cell })

        // Group cell attributes by row (cells with same vertical center) and loop on those groups
        for (_, attributes) in Dictionary(grouping: cellAttributes, by: { 
    ($0.center.y / 10).rounded(.up) * 10 }) { 
   
            // Set the initial left inset
            var leftInset = sectionInset.left

            // Loop on cells to adjust each cell's Origin and prepare leftInset for the next cell
            for attribute in attributes { 
   
                attribute.frame.origin.x = leftInset
                leftInset = attribute.frame.maxX + minimumInteritemSpacing
            }
        }

        return layoutAttributes
    }
}

可以看出来这这个代码是做了一个UICollectionViewFlowLayout的自定义,是为了做UICollectionView的Items的居左显示排列,但这不是我们要研究的重点,我们需要研究Dictionary grouping by 的用法。

其实通过grouping by 这个叫法来说应该大概说明了它的含义,它应该是按照某种条件分组使用的,那下面我们来举个例子。

例如我们有一个现有数组

enum Sex{ 
   
    male,
    female
}

class Student { 
   
	var name:String?
	var sex:Sex = Sex.male
	var age:Int = 0
}

可以看出来我们以上定义了一个枚举和一个类,类中用到了这个枚举来代表性别。比如说我们有如下一组数据。

val student1 = Student()
student1.name = "小明"
student1.sex = Sex.male
student1.age = 18

val student2 = Student()
student2.name = "小红"
student2.sex = Sex.female
student2.age = 20

val student3 = Student()
student3.name = "小童"
student3.sex = Sex.male
student3.age = 18

val students = [student1,student2,student3]

那么如果我们想用性别来把数组进行分组应该怎么写呢,我们下面来研究一下

let groups = Dictionary(grouping:students, by: { 
   
	$0.sex
})

//对就这样用Dictionnary的grouping by 操作一下就好了,那么得到数据应该是如下这样

let groups = [Sex.male:[student1,student3], Sex.female:[student2]]
当然真是数据肯定是每个组里边的数据是实体数据,我这在这里只是表象一下,这样大家应该很好理解。

好的,写到这里应该算是明白了,我们可以动手自己写着体验一下,还可以用这种方式试着写一下按照相同年龄分组一下或者按照不同年龄段分组一下。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/171336.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • .java编译成.class 与 .class反编译成.java

    .java编译成.class 与 .class反编译成.java.java编译成.class 与 .class反编译成.java

    2022年4月23日
    103
  • Lightroom人像磨皮滤镜插件portraiture Mac版

    Lightroom人像磨皮滤镜插件portraiture Mac版本次小编为您带来了Portraiture3forLightroomforMac,这是一款适用于Lightroom的lr人像磨皮滤镜插件。lr磨皮插件portraitureMac版功能非常强大,能够快速对图像中的皮肤,眉毛,头发,眼睛瞪部位进行磨皮修饰,去除瑕疵,同时为您保持皮肤的色泽!链接:https://pan.baidu.com/s/1U0QVMQ6Qa8F5NITbBVr3…

    2022年7月22日
    16
  • mcu单片机开发_AVR单片机

    mcu单片机开发_AVR单片机关于单片机(MCU)最强科普

    2022年10月8日
    0
  • kindeditor配置syntaxhighlighter…「建议收藏」

    kindeditor配置syntaxhighlighter…「建议收藏」kindeditor默认是prettify,我行号没弄出来,换了syntaxhighlighter,下面是配置的代码。。。 kindeditortest body{ font-family:”MicrosoftYaHei”; } #editor{ word-wrap:break-word; } #ContentSubm

    2022年10月11日
    0
  • phpstorm2021激活码(已测有效)

    phpstorm2021激活码(已测有效),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月14日
    174
  • ssm管理系统课题_p2实验室

    ssm管理系统课题_p2实验室开发目的方便高效地实验室设备统一管理,除了实现基本的增删改查,还提供借用、归还、购买和问题反馈功能,可实现对实验室设备的基本业务的处理本项目由本人负责开发完成,项目能保证正常运行,当然其中不免也会有缺漏或不完善的地方解决方案1.后端Java框架使用spring+springmvc+mybatisspring功能是实现参数参数注入,请求分发处理,对数据库操作进行事务控制,其中mybatis使用注解查询,整体上大部分使用xml配置,少部分使用注解2.前端使用HTML+javascript+css+j

    2022年10月13日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号