2016-11-26 37 views
0

每隔BAD_ACCESS我是以前通常是一個快速錯字修復,但是這一次是非常混亂幻影EXC_BAD_ACCESS

此代碼應該下載的.ics谷歌日曆文件,然後將它傳遞到解析器功能,即將返回的事件對象(這裏不包括在內)的陣列

的問題是,當一個物理iOS裝置上進行測試,loadEventIntoCalendar()的每3〜4運行的EXC_BAD_ACCESS是當該行被稱爲

拋出tempHold = calendarString.substring(with: tempRange)

在event()函數的while循環中。

我試着用很多不同的技術解決這個問題。 殭屍對象不會在日誌中打印任何東西。 我試着分析儀器的運行,但我沒有發現任何有用的。我嘗試在參數中傳遞一個單獨的String文件副本,但是這並沒有改變任何東西。

我認爲這個問題與calendarString有關,或者至少是指向的值。我試着通過Xcode分析內存塊,但是我找不到任何變量,這會指向導致錯誤的內存位置。

我敢肯定,RAM不會超載爲整個應用程序只佔用約70兆字節的最高(經過與儀器

事件()被認爲是一個單獨的靜態函數。

這裏有兩個功能

func loadEventsIntoCalendar() { 
    // The link from which the calendar is downloaded 
    let url = URL (string: "https://calendar.google.com/calendar/ical/wlmacci%40gmail.com/public/basic.ics")! 


    // The process of downloading and parsing the calendar 
    let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in 
     // The following is simply a declaration and will not execute without the line 'task.resume()' 
     if let URLContent = data { // If Data has been loaded 
      // If you got to this point then you've downloaded the calendar so... 
      // Calendar File parsing starts here!!! 
      // The string that holds the contents of the calendar's events 
      var webContent:String = String(data: URLContent, encoding:String.Encoding.utf8)! 
      self.events(forCalendarFile: &webContent, inCalendar: Calendar(identifier: Calendar.Identifier.gregorian)) 
     } 
    }) 
    task.resume() 
} 

// Calendar Parser for this VC 
func events(forCalendarFile webContent:inout String, inCalendar calendar:Calendar) { 
    // The reason for this complication is, i thought copying the object might solve the issue, it did not :(
    let NSWebContent = NSString(string: webContent) 
    let calendarString = NSWebContent.copy() as! NSString 

    // An array of flags used for locating the event fields 
    // [h][0] - The flag that marks the begining of a field, [h][1] - The flag that marks the end of a field 
    let searchTitles:[[String]] = [["SUMMARY:", "TRANSP:"], ["DESCRIPTION:", "LAST-MODIFIED:"], ["DTSTART", "DTEND"], ["DTEND", "DTSTAMP"], ["LOCATION:", "SEQUENCE:"]] 

    // The range of "webContent's" content that is to be scanned 
    // Must be decreased after each event is scanned 
    var range:NSRange = NSMakeRange(0, calendarString.length - 1) 
    // Inside function that will be used to determine the 'difference' range between the begining and end flag ranges. 
    let findDifference:(NSRange, NSRange) -> NSRange = {(first:NSRange, second:NSRange) -> NSRange in 
     let location = first.location + first.length, length = second.location - location // Determine the start position and length of our new range 
     return NSMakeRange(location, length)            // Create and return the new range 
    } 
    // Inside function that will be used to move the searching range to the next event 
    // Returns an NSNotFound range (NSNotFound, 0) if there are no more events 
    let updateRange:(NSRange) -> NSRange = {(oldRange:NSRange) -> NSRange in 
     let beginingDeclaration = calendarString.range(of: "BEGIN:VEVENT", options: NSString.CompareOptions.literal, range: oldRange) 
     // If the "BEGIN:VEVENT" was not found in webContent (no more events) 
     if NSEqualRanges(beginingDeclaration, NSMakeRange(NSNotFound, 0)) { 
      return beginingDeclaration // Return an 'NSNotFound' range (Named it myself;) 
     } 
     // Calculate the index of the last character of 'beginingDeclaration' flag 
     let endOfBeginingDeclaration = beginingDeclaration.location + beginingDeclaration.length 
     // Calculate the length of the new range 
     let length = oldRange.length - endOfBeginingDeclaration + oldRange.location 
     // Calculate the starting location of the new range 
     let location = endOfBeginingDeclaration 
     // Create and return the new range 
     return NSMakeRange(location, length) 
    } 

    // A holder for the begining and end flags for each event field 
    var fieldBoundaries:[NSRange] 
    // The actual parsing of each event 
    repeat { 
     range = updateRange(range) // Move our searching range to the next event 
     if NSEqualRanges(range, NSMakeRange(NSNotFound, 0)) { // If there are no more events in the searching range 
      break;            // Then no more shall be added (break from the loop) 
     } 

     var tempHold:String! 
     // Record each field into our event database 
     for h in 0...searchTitles.count-1 { 
      fieldBoundaries = [NSRange]() // Clear the fieldBoundaries for the new search 
      fieldBoundaries.append(calendarString.range(of: searchTitles[h][0], options: NSString.CompareOptions.literal, range: range)) // Find the begining flag 
      fieldBoundaries.append(calendarString.range(of: searchTitles[h][1], options: NSString.CompareOptions.literal, range: range)) // Find the ending flag 
      let tempRange = findDifference(fieldBoundaries[0], fieldBoundaries[1]) 
      print ("Isolating event content") 
      tempHold = calendarString.substring(with: tempRange)       // Create a new string from whatever is in between the two flags. This will be the current field of the event 
      print ("Event content isolated") 
      tempHold = tempHold.trimmingCharacters(in: CharacterSet.newlines)           // Remove all /r /n and other 'new line' characters from the event field 
      tempHold = tempHold.replacingOccurrences(of: "\u{005C}", with: "", options: .literal, range: nil)   // Replace all backslashes from the event field 
     } 
    } while (true) 
} 

這必須相對快地完成,所以快速反應,將不勝感激。

在此先感謝!

+0

你有沒有嘗試在行上放置一個斷點'tempHold = calendarString.substring(with:tempRange)'或記錄'tempRange'和'calendarString'就在它之前? –

+0

問題在於內存中的某些組件在運行過程中中途遭到破壞,所以除了一個點上的完全活動變量和下一個損壞的斷點之外,斷點不會顯示太多內容。不顯示我的錯誤來源 – sketch204

回答

0

其實,我落得這樣做是完全重寫的算法。它的工作方式有點不同,現在實際上縮短了20行,速度更快。

感謝您的關注!感謝幫助:)

0

據我所知,字符串搜索方法不保證結果範圍的長度爲0。如果您更換兩個範圍檢查會發生什麼?

if range.location == NSNotFound { ... } 

代替

if NSEqualRanges(range, NSMakeRange(NSNotFound, 0)) { ... } 
+0

據我所知,NSNotFound是一個數字,而不是一個範圍,所以我不認爲這條線將工作,但無論如何感謝。 – sketch204