简体   繁体   English

将Obj-c iOS应用程序转换为Swift时的链接器错误

[英]Linker error when converting Obj-c iOS app to Swift

I have an iOS app written in Objective-C that I want to convert to Swift. 我有一个用Objective-C编写的iOS应用,想要转换为Swift。 Per Apple's Using Swift with Cocoa and Objective-C , I added a new Swift file with the same name as the Objective-C class it's replacing, put in a stub class declaration, and removed the original Objective-C class from the target. 根据Apple 在Cocoa和Objective-C中使用Swift的要求 ,我添加了一个新的Swift文件,该文件的名称与它要替换的Objective-C类的名称相同,放入了存根类声明,并从目标中删除了原始的Objective-C类。

The error is reporting that a symbol for the class can't be found at link time: 该错误报告在链接时找不到该类的符号:

Undefined symbols for architecture x86_64:
  "_OBJC_CLASS_$_Chunk", referenced from:
      objc-class-ref in ViewController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here's my stub class declaration: 这是我的存根类声明:

import Foundation

@objc class Chunk {

}

Is there some linker setting I can check? 我可以检查一些链接器设置吗?

And yes, I have cleaned my project, removed my derived data folder, and confirmed that the Swift file is in the Compile Sources section of the Build Phases . 是的,我已经清理了项目,删除了派生数据文件夹,并确认Swift文件在Build Phases的Compile Sources部分中

I've also now reproduced the problem with a brand new project created in Xcode 6.1.1, available at https://github.com/dpassage/Foo . 现在,我还通过在Xcode 6.1.1中创建的全新项目(位于https://github.com/dpassage/Foo)重现了该问题。

On your link (Apple Dev)... you must check again "Importing Swift into Objective-C" :) 在链接上(Apple Dev)...,您必须再次检查“将Swift导入Objective-C” :)

In your ViewController.m ... remove this line: #import "Chunk.h" and add this line: #import "Foo-Swift.h" 在您的ViewController.m ,删除以下行: #import "Chunk.h"并添加以下行: #import "Foo-Swift.h"

"The file: <#YourProjectName#>-Swift.h should already be created automatically in your project, even if you can not see it." “文件: <#YourProjectName#>-Swift.h应该已经在您的项目中自动创建,即使您看不到它。”

UPDATE UPDATE

If Chunk.swift is descendant of an Objective-C class.. replace this @objc class Chunk with this: @objc class Chunk : NSObject (or conf with your requirement). 如果Chunk.swift是Objective-C类的后代..请将以下@objc class Chunk替换为: @objc class Chunk : NSObject (或与您的要求conf)。 If not... check "Using PURE Swift Classes in Objective-C" from here https://stackoverflow.com/a/24005242/1702413 如果不是...,请从此处https://stackoverflow.com/a/24005242/1702413检查“在Objective-C中使用PURE Swift类”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM