使Swift类可用于Interface Builder

时间:2014-07-08 18:18:10

标签: objective-c interface-builder swift

我有一个XCode项目,我在目标C中开发,但现在使用Swift我有几个类保存为.swift文件。当我在Interface Builder中将它们作为自定义类引用时,它们不起作用,我得到一个错误,该类不存在。

在弥合两者之间的差距方面我缺少什么?我有头文件使目标C可用于Swift但不确定相反的方式

由于

2014-07-08 20:18:07.463 FoodForTeeth[22030:70b] Unknown class SecondViewController in Interface Builder file.
2014-07-08 20:18:07.496 FoodForTeeth[22030:70b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x8c85390> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key date.'
*** First throw call stack:
(
    0   CoreFoundation                      0x01acf5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x018528b6 objc_exception_throw + 44
    2   CoreFoundation                      0x01b5f6a1 -[NSException raise] + 17
    3   Foundation                          0x01513c2e -[NSObject(NSKeyValueCoding) setValue:forUndefinedKey:] + 282
    4   Foundation                          0x0147ff3b _NSSetUsingKeyValueSetter + 88
    5   Foundation                          0x0147f493 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 267
    6   Foundation                          0x014e194a -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 412
    7   UIKit                               0x00866cd5 -[UIRuntimeOutletConnection connect] + 106
    8   libobjc.A.dylib                     0x018647d2 -[NSObject performSelector:] + 62
    9   CoreFoundation                      0x01acab6a -[NSArray makeObjectsPerformSelector:] + 314
    10  UIKit                               0x0086582e -[UINib instantiateWithOwner:options:] + 1417
    11  UIKit                               0x006d7c95 -[UIViewController _loadViewFromNibNamed:bundle:] + 280
    12  UIKit                               0x006d843d -[UIViewController loadView] + 302
    13  UIKit                               0x006d873e -[UIViewController loadViewIfRequired] + 78
    14  UIKit                               0x006d8c44 -[UIViewController view] + 35
    15  UIKit                               0x006f2a72 -[UINavigationController _startCustomTransition:] + 778
    16  UIKit                               0x006ff757 -[UINavigationController _startDeferredTransitionIfNeeded:] + 688
    17  UIKit                               0x00700349 -[UINavigationController __viewWillLayoutSubviews] + 57
    18  UIKit                               0x0083939d -[UILayoutContainerView layoutSubviews] + 213
    19  UIKit                               0x0062fdd7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
    20  libobjc.A.dylib                     0x0186481f -[NSObject performSelector:withObject:] + 70
    21  QuartzCore                          0x045ab72a -[CALayer layoutSublayers] + 148
    22  QuartzCore                          0x0459f514 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    23  QuartzCore                          0x0459f380 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    24  QuartzCore                          0x04507156 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
    25  QuartzCore                          0x045084e1 _ZN2CA11Transaction6commitEv + 393
    26  QuartzCore                          0x04508bb4 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
    27  CoreFoundation                      0x01a9753e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    28  CoreFoundation                      0x01a9748f __CFRunLoopDoObservers + 399
    29  CoreFoundation                      0x01a753b4 __CFRunLoopRun + 1076
    30  CoreFoundation                      0x01a74b33 CFRunLoopRunSpecific + 467
    31  CoreFoundation                      0x01a7494b CFRunLoopRunInMode + 123
    32  GraphicsServices                    0x02ebc9d7 GSEventRunModal + 192
    33  GraphicsServices                    0x02ebc7fe GSEventRun + 104
    34  UIKit                               0x005c594b UIApplicationMain + 1225
    35  FoodForTeeth                        0x0001c42d main + 141
    36  libdyld.dylib                       0x02b5e701 start + 1
    37  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)  

SecondViewController.swift

import UIkit
import CoreData

@objc(SecondViewController) class SecondViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var txtTask: UITextField!
    @IBOutlet var txtDesc: UITextField!
    @IBOutlet var date: UIDatePicker!
    @IBOutlet var dateDisplayLabel: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()

        configureDatePicker()
    }

    func configureDatePicker() {

        date.addTarget(self, action: "updateDatePickerLabel", forControlEvents: .ValueChanged)

        updateDatePickerLabel()
    }

    func updateDatePickerLabel() {
        dateDisplayLabel.text = dateFormatter.stringFromDate(date.date)
    }

    @lazy var dateFormatter: NSDateFormatter = {
        let dateFormatter = NSDateFormatter()

        dateFormatter.timeStyle = .ShortStyle

        return dateFormatter
        }()


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    //Events
    @IBAction func btnAddTask_Click(sender: UIButton){

        let appDel: foodforteethAppDelegate = UIApplication.sharedApplication().delegate as foodforteethAppDelegate
        let context:NSManagedObjectContext = appDel.managedObjectContext

        let ent = NSEntityDescription.entityForName("Food", inManagedObjectContext: context)

        var newFood = food(entity: ent, insertIntoManagedObjectContext: context)
        newFood.foodname = txtTask.text
        newFood.date = dateFormatter.stringFromDate(date.date)

        context.save(nil)


      /*
        taskMgr.addTask(txtTask.text, desc: dateFormatter.stringFromDate(date.date));

        self.view.endEditing(true)


        txtTask.text = ""
        //txtDesc.text = ""
        //self.tabBarController.selectedIndex = 0;*/

        self.navigationController.popViewControllerAnimated(true)

    }

    //iOS touch functions
    override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!){
        self.view.endEditing(true)
    }


    func textFieldShouldReturn(textField: UITextField!) -> Bool {
        textField.resignFirstResponder();
        return true
    }

}

1 个答案:

答案 0 :(得分:0)

您需要阅读https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html


  

从同一目标将Swift代码导入Objective-C   将该目标的Swift代码导入任何Objective-C .m文件   在该目标内使用此语法,并替换相应的   名称:

     

#import“ProductModuleName-Swift.h”

     

目标中的任何Swift文件都将显示在包含此导入的Objective-C .m文件中   声明。有关从Objective-C代码使用Swift的信息,请参阅   使用Objective-C中的Swift。