2017-12-07 10 views
0

in meiner App habe ich einen Viewcontroller mit Tableview und wenn es geladen wird gibt mir etwa 12 gleichzeitige Speicherfehler alle an der gleichen Stelle, wo ich versuche, das Element aus meiner Gewohnheit zu entfernen Array. Im Folgenden sind die dazugehörigen CodesBehebung von simultanen Speicherzugriffsfehler für Array von benutzerdefinierten Klasse iOS

In meinem Viewcontroller, wo Tabellenlasten Ich habe die Methode, wie unten

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 

    //Some Code 

    arrangeTableObject.arrangetableSectionsInOrderForNoteType(withFrameId:tableDataSection.frameId.uintValue) 

    //Some code 
    return cell 
} 

In meiner anderen schnellen Klasse greifen arrangeTableObject.swift das ist, wie ich die Methoden

verwendet habe
//My custom array is defined as 
var tableSections : [TableSection]? 

func arrangetableSectionsInOrderForNoteType(type: NoteType){ 

     var tableIndex = 0 

     if tableSectionForItemType(itemType: .tableItemNoteItems) != nil { 

      let noteItemSection = tableSectionForItemType(itemType: .tableItemNoteItems) 
      removetableSectionWithFrameId(itemType: . tableItemNoteItems) 
      tableSections?.insert(noteItemSection!, at: tableIndex) 
      tableIndex += 1 
     } 


     if let commentSection = tableSectionForItemType(itemType: .tableItemComment) { 
      removetableSectionWithFrameId(itemType: .tableItemComment) 
      tableSections?.insert(commentSection, at: tableIndex) 
      tableIndex+=1 
     } 


     if tableSectionForItemType(itemType: .tableItemAtAGlance) != nil { 

      let otherSection = tableSectionForItemType(itemType: .tableItemOtherItems) 
      removetableSectionWithFrameId(itemType: . tableItemOtherItems) 
      tableSections?.insert(otherSection!, at: tableIndex) 
      tableIndex += 1 

     } 


    } 

// Dies ist das Verfahren, bei dem es zeigt Fehler

func removetableSectionWithFrameId(itemType : tableItemType){ 

     if tableSections?.count != 0 { 
      for section in tableSections! { 

       if section.itemType == itemType { 
       //Error points to this line below 
         self.tableSections?.remove(at: self.tableSections!.index(of: section)!) 
       } 
      } 
     } 
    } 

Für f Synchronisierverriegelung hinzufügen unter Verwendung des Verfahrens unter

func sync(lock: NSObject, closure:() -> Void) { 
     objc_sync_enter(lock) 
     closure() 
     objc_sync_exit(lock) 
    } 

Ich habe versucht, den Abschnitt zu ändern, wo der Fehler aufgetreten ist, wie unten ixing ich versuchte, aber es wirft mir immer noch den gleichen Fehler

if section.itemType == itemType { 
        sync(lock: tableSections! as NSObject){ 
         self.tableSections?.remove(at: self.tableSections!.index(of: section)!) 
       } 
     } 

Jede Beratung oder Beratung ist geschätzt.

Antwort

0

Die mögliche Ursache ist, dass Sie die Elemente während der Iteration aus dem Array entfernen.

Versuchen Sie, die folgenden und sehen Sie, wenn Sie Problem haben

tableSections = tableSections.filter { $0.itemType != itemType } 
+0

Danke, das den Trick getan zu haben scheint. – Gamerlegend

Verwandte Themen