-->

Swift UIDevice.currentDevice() not compiling

2019-08-06 07:23发布

问题:

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

回答1:

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 :

In your target Framework.h add :

#import <BSStaticObjc.h>

Now in Swift call method

Device()


回答2:

This has been driving me crazy as well. I can't fix it. As a workaround I made an Objective-C class that solely sublcasses UIDevice. My class is AWDevice. In Swift the compiler is happy with AWDevice.currentDevice()

This is really strange.