简体   繁体   中英

Xcode won't recognize a new Swift class

I have created a new Swift class in a new file

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. A similar question has been posted here: How do I import a Swift file from another Swift file? but none of those solutions worked for me. BTW, this is part of the Udacity swift class. Thanks, any help would be appreciated.

In the Project Navigator on the left, select the file where you declared RecordedAudio.

Now, in the File inspector on the right, look at the 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)
  4. Nay...let's delete the build folder: /Users/[YOU]/Library/Developer/Xcode/DerivedData/project-name-ADM2KD...
  5. Restart 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. Oh and make sure you are subclassing 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. I switched back to Swift 3. No problems...

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

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

Tried the ownership toggling, cleaning, restarted x-code, deleted and re-adding reference but no result.

A pod install fixed it for me, weird because my class was not from another module or library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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