Before WWDC 2014, I studied Objective-C. I created the Lift Simulator application, which exists simply for my training. When you press one of the seven buttons, it closes the elevator doors (animation), moves one level at a time to the selected level (as indicated in the text box and the color of the buttons), and then opens the doors again.
Following WWDC and the Swift declaration, I am trying to recreate this application in Swift. So far, I find Swift a lot nicer to work with, but I ended up in one major roadblock regarding unnamed links. The application started normally until I realized the function of a text label and coloring of buttons for installation based on the properties of the Elevator model when the application started. After implementing these functions, the application always crashes at startup.
In the end, I narrowed down the problem caused by any call from the view controller to the Elevator model. After commenting on the call to the View controller method to update the interface, the application starts up fine again, and I can confirm that the problem is with calling the Elevator model by simply trying to call the initializer. The following code snippets reproduce the problem:
Application Member:
var window: UIWindow?
let mainElevator = Elevator()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
mainElevator.view = ViewController(elevator: mainElevator)
window!.rootViewController = mainElevator.view
window!.makeKeyAndVisible()
return true
}
Lift (model):
class Elevator: NSObject {
var view: ViewController?
var currentLevel = 1
var newLevel: Int?
}
ViewController:
class ViewController: UIViewController {
unowned let elevator: Elevator
init(elevator: Elevator) {
self.elevator = elevator
super.init(nibName: nil, bundle: nil)
}
}
. , , , . , , Person and CreditCard ( Apple), , .
ViewController , ( loadView() viewDidLoad()), , . , , , . ( , - ), . , Apple Person/CreditCard.
, . (, ), . , , , .
:
libswift_stdlib_core.dylib`_swift_abortRetainUnowned:
0x1001c3980: pushq %rbp
0x1001c3981: movq %rsp, %rbp
0x1001c3984: leaq 0x176a7(%rip), %rax ; "attempted to retain deallocated object"
0x1001c398b: movq %rax, 0x792ce(%rip) ; gCRAnnotations + 8
0x1001c3992: int3
0x1001c3993: nopw %cs:(%rax,%rax) Thread 1: EXC_BREAKPOINT (code=EXT_1386_BPT, subcode=0x0)