Search code examples
iosswiftuidevice

Swift UIDevice.currentDevice() not compiling


I have a swift project in which i want to retrieve the name of the device and show in in a UITableViewCell.

But when i want to call UIDevice.currentDevice() in

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

the compiler fails with:

Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1

The following build commands failed:
CompileSwift normal arm64 "path to project folder"/custom_tableview_controller.swift
CompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler (2 failures)


The awkward thing is calling UIDevice.currentDevice() in any other file than my custom_tableview_controller.swift file is working.

Additional information:
- Xcode 6.4(6E35b)
- Debug


Solution

  • I create that for get UIDevice in framework with Swift :

    Create Obj-c Header.h and add

    #import <UIKit/UIKit.h>
    
    /**
     *  Static Current Device
     *  @note Can't get UIDevice CurrentDevice in Swift, Hack with obj-c
     *
     *  @return UIDevice
     */
    static UIDevice* Device() { return [UIDevice currentDevice]; }
    

    In your target Framework -> Build Phases make this :

    enter image description here

    In your target Framework.h add :

    #import <BSStaticObjc.h>
    

    Now in Swift call method

    Device()