【问题标题】:Swift UIDevice.currentDevice() not compilingSwift UIDevice.currentDevice() 未编译
【发布时间】:2015-08-31 13:24:40
【问题描述】:

我有一个 swift 项目,我想在其中检索设备名称并显示在 UITableViewCell 中。

但是当我想打电话给UIDevice.currentDevice()

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

编译器失败:

命令 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc 退出代码 1 失败

以下构建命令失败:
CompileSwift 普通 arm64“项目文件夹路径”/custom_tableview_controller.swift
CompileSwiftSources 正常 arm64 com.apple.xcode.tools.swift.compiler (2 次失败)


尴尬的是在我的custom_tableview_controller.swift 文件工作之外的任何其他文件中调用UIDevice.currentDevice()

附加信息:
- Xcode 6.4(6E35b)
- 调试

【问题讨论】:

  • 请在您使用UIDevice.currentDevice()的地方显示完整行
  • dsl_textfield_cell!.dsc_textfield.text = UIDevice.currentDevice().name 是完整的行,但是当我在新行中写UIDevice.currentDevice()而不使用它时它也不会编译
  • 很奇怪。相同的 Xcode 版本,它适用于我。建议:清理 Xcode(清理构建 + 清理派生数据)。
  • 在 application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) 中调用该行 -> Bool 正在工作。干净的构建 + 派生数据不起作用。

标签: ios swift uidevice


【解决方案1】:

我创建它是为了使用 Swift 在框架中获取 UIDevice:

创建 Obj-c Header.h 并添加

#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]; }

在您的目标框架中 -> 构建阶段:

在您的目标 Framework.h 中添加:

#import <BSStaticObjc.h>

现在在 Swift 调用方法中

Device()

【讨论】:

    【解决方案2】:

    这也让我发疯了。我无法修复它。作为一种解决方法,我创建了一个仅子类 UIDevice 的 Objective-C 类。我的班级是AWDevice。在 Swift 中,编译器对AWDevice.currentDevice() 很满意

    这真的很奇怪。

    【讨论】:

    • 太棒了!我遇到过同样的问题。我花了相当长的时间来注释掉几乎一半的源代码,直到发现 UIDevice 导致了编译问题。我首先在 Stackoverflow 上搜索编译器消息以寻求解决方案,但没有成功。所以对于其他有同样问题的人,这里是这个 bug 的编译器错误:Global variable initializer type does not match global variable type! %struct._class_t** @"OBJC_CLASSLIST_REFERENCES_$_" LLVM ERROR: Broken module found, compilation aborted! 请注意:你需要选择 All Messages 才能看到这个错误
    猜你喜欢
    • 2017-02-04
    • 2019-02-06
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多