尝试对数组进行排序时的EXC_BAD_ACCESS

时间:2017-10-31 16:49:53

标签: ios swift xcode

我在使用从核心数据中提取的对象对数组进行排序时遇到了一个错误。

对象应该进入tableview。当用户将对象添加到核心数据时,也可以对其进行标记,表明它应该放在新的部分中。

我写了这个函数来填充一个包含我的对象的数组的数组,以便我可以在以后的表视图中正确填充部分:

var sortedArray = [[Day]]()

func mySortingFunction() {

    var days = [Day]()
    var tempArray = [Day]()

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext

    do {
        days = try context.fetch(Day.fetchRequest())
    } catch {
        print("Error")
    }


    // Sorts the array by date in descending order
    days.sort(by: {$0.realDate! > $1.realDate!})


    // My algorithm
    if days.count > 0 {

        for i in 0..<days.count {

            if i == (days.count-1) {

                tempArray.append(days[i])
                sortedArray.append(tempArray)

            } else if days[i].mark == "New section" {         // Error here: Thread 1: EXC_BAD_ACCESS (code=1, address=0x61afa632668725cd)

                tempArray.append(days[i])
                sortedArray.append(tempArray)
                tempArray.removeAll()

            } else {
                tempArray.append(days[i])
            }

        }

    }
}

该应用程序启动正常,但一旦我切换视图并尝试返回,它崩溃并显示此错误。每次加载视图时都会调用该函数。有什么想法吗?

修改 这是堆栈跟踪:

    * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0xd15e73beca0d1e24)
frame #0: 0x00000001055b7290 libswiftCore.dylib`swift_dynamicCastClass + 36
frame #1: 0x00000001054a9eb8 libswiftCore.dylib`function signature specialization <preserving fragile attribute, Arg[0] = Owned To Guaranteed, Arg[1] = Dead> of Swift.String.init(_cocoaString: Swift.AnyObject) -> Swift.String + 76
frame #2: 0x00000001054447d8 libswiftCore.dylib`Swift.String.init(_cocoaString: Swift.AnyObject) -> Swift.String + 24
frame #3: 0x0000000105a0c258 libswiftFoundation.dylib`static (extension in Foundation):Swift.String._unconditionallyBridgeFromObjectiveC(Swift.Optional<__ObjC.NSString>) -> Swift.String + 16
* frame #4: 0x0000000104ea2dcc SalaryCalc`Main.getShifts(self=0x0000000157e6ae50) at Main.swift:60
frame #5: 0x0000000104ea2324 SalaryCalc`Main.viewDidLoad(self=0x0000000157e6ae50) at Main.swift:29
frame #6: 0x0000000104ea25f8 SalaryCalc`@objc Main.viewDidLoad() at Main.swift:0
frame #7: 0x000000018cd7bbfc UIKit`-[UIViewController loadViewIfRequired] + 1040
frame #8: 0x000000018cd7b7d4 UIKit`-[UIViewController view] + 28
frame #9: 0x000000018ce9f9f0 UIKit`-[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 768
frame #10: 0x000000018ce9f6dc UIKit`-[UITabBarController transitionFromViewController:toViewController:] + 68
frame #11: 0x000000018ce9f538 UIKit`-[UITabBarController _setSelectedViewController:] + 400
frame #12: 0x000000018cea2290 UIKit`-[UITabBarController setSelectedViewController:] + 112
frame #13: 0x000000018cd89a84 UIKit`+[UIView(Animation) performWithoutAnimation:] + 104
frame #14: 0x000000018cea4114 UIKit`-[UITabBarController _selectDefaultViewControllerIfNecessaryWithAppearanceTransitions:] + 452
frame #15: 0x000000018cea2cd8 UIKit`-[UITabBarController viewWillAppear:] + 208
frame #16: 0x000000018cd945bc UIKit`-[UIViewController _setViewAppearState:isAnimating:] + 628
frame #17: 0x000000018cd94330 UIKit`-[UIViewController __viewWillAppear:] + 156
frame #18: 0x000000018d0c1f80 UIKit`__56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 1152
frame #19: 0x000000018cfec680 UIKit`_runAfterCACommitDeferredBlocks + 292
frame #20: 0x000000018cfdfb24 UIKit`_cleanUpAfterCAFlushAndRunDeferredBlocks + 288
frame #21: 0x000000018cd6e808 UIKit`_afterCACommitHandler + 132
frame #22: 0x00000001839138b8 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
frame #23: 0x0000000183911270 CoreFoundation`__CFRunLoopDoObservers + 412
frame #24: 0x000000018391182c CoreFoundation`__CFRunLoopRun + 1292
frame #25: 0x00000001838322d8 CoreFoundation`CFRunLoopRunSpecific + 436
frame #26: 0x00000001856c3f84 GraphicsServices`GSEventRunModal + 100
frame #27: 0x000000018cddf880 UIKit`UIApplicationMain + 208
frame #28: 0x0000000104ec6130 SalaryCalc`main at AppDelegate.swift:13
frame #29: 0x000000018335656c libdyld.dylib`start + 4

0 个答案:

没有答案