19

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.

Community
  • 1
  • 1
Sam J
  • 193
  • 1
  • 1
  • 6

6 Answers6

56

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.

matt
  • 447,615
  • 74
  • 748
  • 977
  • 4
    Thanks matt, that worked. On the new class, I had both "target" and "test target" checked so I unchecked "test target" and keep only "target" . That did the trick. – Sam J May 10 '15 at 20:54
  • 7
    For future readers, also try unchecking and checking the target again if it's already checked. – SuperCodeBrah Feb 04 '18 at 05:19
4

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).

Lukas Kalinski
  • 2,095
  • 20
  • 25
4

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...

Hari Honor
  • 8,520
  • 6
  • 45
  • 50
  • Sadly, this is what I had to do to get it to work also. – KeithTheBiped Sep 15 '20 at 16:11
  • 1
    Adding the public keyword did the trick for me. I moved the class to other module then Xcode won't recognise in others. I spent quite a few hours trying the above steps already as you mentioned. Thanks mate :) – Ahmed Jan 22 '21 at 05:32
2

If your class is in another Module, please make sure that your class and the class initializers have the public access modifier.

Eray Alparslan
  • 171
  • 1
  • 8
1

For me, I had to check the same target memberships on the class as in the ViewController (I had a TodayExtension)

0

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.

C. Kontos
  • 959
  • 9
  • 18