简体   繁体   English

Xcode 无法识别新的 Swift 类

[英]Xcode won't recognize a new Swift class

I have created a new Swift class in a new file我在一个新文件中创建了一个新的 Swift 类

import Foundation

class RecordedAudio: NSObject{
    var filePathUrl: NSURL!
    var title: String!
}

and now I want use it inside another class by creating a new object.现在我想通过创建一个新对象在另一个类中使用它。

import UIKit
import AVFoundation

class recordSoundsViewController: UIViewController, AVAudioRecorderDelegate {
    var audioRecorder: AVAudioRecorder!
    var recordedAudio: RecordedAudio! // new object gives error

But Xcode is giving me the error "Use of undeclared type RecordedAudio" so I got stuck there.但是 Xcode 给了我错误“使用未声明的类型 RecordedAudio”,所以我被困在那里。 A similar question has been posted here: How do I import a Swift file from another Swift file?此处发布了一个类似的问题: 如何从另一个 Swift 文件导入 Swift 文件? but none of those solutions worked for me.但这些解决方案都不适合我。 BTW, this is part of the Udacity swift class.顺便说一句,这是 Udacity swift 课程的一部分。 Thanks, any help would be appreciated.谢谢,任何帮助将不胜感激。

In the Project Navigator on the left, select the file where you declared RecordedAudio.在左侧的 Project Navigator 中,选择您声明 RecordedAudio 的文件。

Now, in the File inspector on the right, look at the Target Membership.现在,在右侧的文件检查器中,查看 Target Membership。 The app target (not the Test target) should be checked , similar to this:应该检查应用程序目标(不是测试目标),类似于:

在此处输入图像描述

If not, check it!如果没有,请检查它! And now all will be well.现在一切都会好起来的。

在我的情况下,我必须从项目导航器中删除引用,然后再次添加它们(目标成员身份不是问题)。

I just had to do the Full Monty to get it to work...我只需要做完整的蒙蒂才能让它工作......

  1. Comment out referencing lines注释掉引用行
  2. Fix other bugs to ensure a build修复其他错误以确保构建
  3. Cmd+Shft+K (Clean build folder) Cmd+Shift+K(清理构建文件夹)
  4. Nay...let's delete the build folder: /Users/[YOU]/Library/Developer/Xcode/DerivedData/project-name-ADM2KD...不...让我们删除构建文件夹:/Users/[YOU]/Library/Developer/Xcode/DerivedData/project-name-ADM2KD...
  5. Restart Xcode重启 Xcode
  6. Build建造
  7. Uncomment our referencing lines取消注释我们的参考线

Also maybe add @objc to the line above your class definition and maybe make it public class just to be explicit and possible add public to your methods as well.也可以将@objc 添加到您的类定义上方的行中,并可能将其设为public class ,以便明确并且可能也将public添加到您的方法中。 Oh and make sure you are subclassing NSObject .哦,确保你是NSObject的子类。

If you are stuck getting your initialiser to show up, I've also just noticed that Swift 4.2 has lots more problems here.如果你在让你的初始化程序出现时遇到困难,我也刚刚注意到 Swift 4.2 在这里有很多问题。 I switched back to Swift 3. No problems...我切换回 Swift 3。没问题...

如果您的类在另一个模块中,请确保您的类和类初始化器具有公共访问修饰符。

对我来说,我必须检查类中与 ViewController 中相同的目标成员资格(我有一个 TodayExtension)

Tried the ownership toggling, cleaning, restarted x-code, deleted and re-adding reference but no result.尝试了所有权切换、清理、重新启动 x 代码、删除并重新添加引用但没有结果。

A pod install fixed it for me, weird because my class was not from another module or library.一个pod install为我修复了它,很奇怪,因为我的类不是来自另一个模块或库。

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

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