ios - 我的 For 循环中的计数没有递增

标签 ios macos swift eventkit

当运行我的代码时,我在控制台打印了一些 1 而不是 1,2,3,4,5....

一些帮助解释为什么会发生这种情况会很好,我很难弄明白。

想法是遍历日历名称,直到找到“旅行”日历。

func checkCalendarExists(){
    var eventCalendars = store.calendarsForEntityType(EKEntityTypeEvent) as [EKCalendar]

    for i in eventCalendars {

        var count = 0
        var calendarCount = eventCalendars.count

        if i.title != "Travel" && count != calendarCount
        {
            ++count
            println(count)
        }

        else if i.title == "Travel"
        {
            // do something
        }
        else
        {
           aMethod()
        }

    }
}

最佳答案

您的 count 变量没有递增,因为它是在循环内声明的,并在每次迭代开始时初始化为值零。为了让您的代码按预期工作,您必须将 var count = 0 移到 for 循环之外。

关于ios - 我的 For 循环中的计数没有递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30923306/

相关文章:

mysql - 在 Mac OS X 上增加 MySQL InnoDB 表大小

objective-c - CTFontManagerRegisterGraphicsFont 不注册姓氏

ios - 当 imageview 在里面时,Swift CollectionView 会改变大小

ios - 更改 UITableView 中每个单元格的 UIButton

ios - 在 NodeJs HTTPS 中使用 certificates.cer

android - 同步数据集 - 多个用户,多个时间戳

macos - Kubernetes 未在 Mac 的 docker 中启动

ios - 为什么我的UITableViewController页脚高度不会更改?

ios - 在 iBooks 阅读器中打开一本书

ios - 删除以前在 Swift 中从同一个应用程序存储的健康数据?