Swift UIDevice.currentDevice() not compiling

ぐ巨炮叔叔 提交于 2019-12-24 17:39: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.



来源:https://stackoverflow.com/questions/32312378/swift-uidevice-currentdevice-not-compiling

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!