Skip to content

Instantly share code, notes, and snippets.

@susemeee
Last active August 29, 2015 14:06
Show Gist options
  • Save susemeee/d138908e983de80f5dcd to your computer and use it in GitHub Desktop.
Save susemeee/d138908e983de80f5dcd to your computer and use it in GitHub Desktop.
Segmentation fault bug in Swift language
//
// ViewController.swift
// SwiftBug
//
// Created by *守細美 on 9/25/14.
// Copyright (c) 2014 iplab. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let handler = completedHandler(self, "register")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
typealias DLDefaultCompletionHandler = (success: Bool, message: String?) -> ()
//swift compiler has a bug while returning closures in a closure
let completedHandler: (AnyObject, String) -> (DLDefaultCompletionHandler) = { (me, title) in
return
{(success, message) -> () in
if success{
//proceed to main controller
let tabView : UITabBarController = UITabBarController(nibName: nil, bundle: nil)
let naviController: UINavigationController = UINavigationController(rootViewController:tabView)
me.presentViewController(naviController, animated: true, completion: nil)
}else{
println("\(title) failed")
}
}
}
}
Copy link

ghost commented Sep 25, 2014

크;

@Luavis
Copy link

Luavis commented Sep 25, 2014

Solution

//
//  ViewController.swift
//  SwiftCrashTest
//
//  Created by Luavis on 9/26/14.
//  Copyright (c) 2014 Luavis. All rights reserved.
//

import UIKit

typealias DLDefaultCompletionHandler = (success: Bool, message: String?) -> ()

class ViewController: UIViewController {

    let completedHandler: (AnyObject, String) -> (DLDefaultCompletionHandler);


    required init(coder aDecoder: NSCoder)
    {
       self.completedHandler = { (me, title) in

            return
                {(success, message) -> () in
                    if success{
                        //proceed to main controller
                        let tabView : UITabBarController = UITabBarController(nibName: nil, bundle: nil)
                        let naviController: UINavigationController = UINavigationController(rootViewController:tabView)
                        me.presentViewController(naviController, animated: true, completion: nil)

                    }else{
                        println("\(title) failed")
                    }
            }
        }

        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let handler = completedHandler(self, "register")
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment