Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode won't recognize a new Swift class

Tags:

ios

swift

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.

like image 260
Sam J Avatar asked May 09 '15 23:05

Sam J


People also ask

How do I add a class in Xcode?

Creating a Class To make a new file, click on File, and then New → File. A selection screen will come up, for an iphone app, select cocoa touch and objective C class. The next screen a name of the new class is entered and an existing class to inherit the attributes from is chosen from a drop down menu.

Can I use Objective-C in Swift?

You can use Objective-C and Swift files together in a single project, no matter which language the project used originally. This makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.


2 Answers

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:

enter image description here

If not, check it! And now all will be well.

like image 103
matt Avatar answered Oct 02 '22 09:10

matt


In my case I had to remove the references from the project navigator, and then add them again (the target membership was not the problem).

like image 24
Lukas Kalinski Avatar answered Oct 02 '22 08:10

Lukas Kalinski