gpt4 book ai didi

ios - 这段代码中的 % 是做什么的?

转载 作者:行者123 更新时间:2023-11-28 18:09:12 26 4
gpt4 key购买 nike

我遇到了一行让我感到困惑的代码:

return [cellSize[indexPath.item % 2] CGSizeValue];

更具体地说:[indexPath.item % 2]

我在 Xcode 中发现的关于 % 的唯一内容主要涉及 %@、%f 等... 以及在字符串中转义 %。我也知道三元,但这不是什么,是吗?谁能解释一下那条线在做什么?

如果需要更多上下文:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CQTaskPhotosCVCell *cell = (CQTaskPhotosCVCell *)[collectionView cellForItemAtIndexPath:indexPath];
return [cellSize[indexPath.item % 2] CGSizeValue];
}

最佳答案

return [cellSize[indexPath.item % 2] CGSizeValue];

%modulo operator (除法后的余数)。

采用 % 2 是选择偶数行和奇数行的常用方法,例如交替着色。在您的情况下,它似乎是从两个不同的单元格大小中进行选择(基于变量 cellSize 的名称)。

关于ios - 这段代码中的 % 是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35163440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com