【问题标题】:for loop crash when iterating over array of iTunesFileTracks迭代 iTunesFileTracks 数组时 for 循环崩溃
【发布时间】:2014-04-05 23:51:34
【问题描述】:

OS x 和 Objective C 的新手。我的程序出现崩溃,来自多个用户报告,我无法在我的机器上重现它。

Feb 22, 2014, 9:06:44 AM: An uncaught exception was raised
Feb 22, 2014, 9:06:44 AM: *** -[NSArray getObjects:range:]: range {3392, 16} extends beyond bounds for empty array
Feb 22, 2014, 9:06:44 AM: (
    0   CoreFoundation                      0x00007fff8f05541c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff94c12e75 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff8ef9bc9f -[NSArray getObjects:range:] + 271
    3   CoreFoundation                      0x00007fff8ef9da63 -[NSArray countByEnumeratingWithState:objects:count:] + 163
    4   MYAppMac                        0x00000001000037b9 +[ITunesProvider getITunesMediaFiles:] + 5209

线路崩溃线指向这个for循环

        // Get tracks from iTunesUserPlaylist
        SBElementArray *fileTracks = [list fileTracks];   

        for(iTunesFileTrack* track in fileTracks)
        { 
         // my code here. I do not modify filetracks anywhere in the loop
        }

for 循环遍历数组中的项目,但在某些时候它会在尝试访问更下方的项目时崩溃。我根本不修改 fileTracks 数组。有什么想法吗?

【问题讨论】:

    标签: objective-c macos itunes


    【解决方案1】:

    刚刚遇到同样的问题。您正在迭代一个实时数组,该数组会根据您的用户对 iTunes 的操作而动态变化。您可以尝试使用-get 获取数组,并像这样遍历生成的固定NSArray

    NSArray* fileTracks = [[list fileTracks] get];
    for (iTunesFileTrack* track in fileTracks)
    {
        // Your code
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多