gpt4 book ai didi

ios - 如何安全地将对象存储在 ARC 下的 countByEnumeratingWithState 中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:23:57 24 4
gpt4 key购买 nike

如何在 NSFastEnumerationState 的额外数组中安全地存储几个对象实例?

我希望在循环运行时保留这些项目,然后在循环完成时释放。

- (NSUInteger)countByEnumeratingWithState: (NSFastEnumerationState *)state
objects: (__unsafe_unretained id *)stackbuf
count: (NSUInteger)len {
unsigned long days = 0;
id current = nil;
id components = nil;
if (state->state == 0)
{
current = [NSCalendar currentCalendar];
state->mutationsPtr = &state->extra[0];
components = [current components: NSDayCalendarUnit fromDate: _startDate toDate: _endDate options: 0];
days = [components day];
state->extra[0] = days;
state->extra[1] = (uintptr_t)(__bridge void *)current;
state->extra[2] = (uintptr_t)(__bridge void *)components;
} else {
days = state->extra[0];
current = (__bridge NSCalendar *)(void *)(state->extra[1]);
components = (__bridge NSDateComponents *)(void *)(state->extra[2]);
}
NSUInteger count = 0;
if (state->state <= days) {
state->itemsPtr = stackbuf;
while ( (state->state <= days) && (count < len) ) {
[components setDay: state->state];
stackbuf[count] = [current dateByAddingComponents: components toDate: _startDate options: 0];
state->state++;
count++;
}
}
return count;
}

这是 NSFastEnumerationState 的定义,来自 Apple 的 header :

typedef struct {
unsigned long state;
id __unsafe_unretained *itemsPtr;
unsigned long *mutationsPtr;
unsigned long extra[5];
} NSFastEnumerationState;

最佳答案

史蒂文

为什么您要尝试使用 C 语言结构来保存这些项,而不是将它们设为您正在为其构建枚举器的类中的私有(private)变量?将它们设为 ivars,问题就不存在了。

安德鲁

关于ios - 如何安全地将对象存储在 ARC 下的 countByEnumeratingWithState 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12866713/

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