Xcode won't recognize a new Swift class - ios

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.

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

I just had to do the Full Monty to get it to work...
Comment out referencing lines
Fix other bugs to ensure a build
Cmd+Shft+K (Clean build folder)
Nay...let's delete the build folder: /Users/[YOU]/Library/Developer/Xcode/DerivedData/project-name-ADM2KD...
Restart Xcode
Build
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...

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

For me, I had to check the same target memberships on the class as in the ViewController (I had a 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.

Related

Use swift code in objective C file in swift based project

There are lots of question about how to use swift code in objective c. I had swift based project. there is Objectice C file where i need to use swift code.
Xcode did not create ProjectName-Swift.h automatically. So i created manually and check the following things.
Generated interface header name : ProjectName-Swift.h
Product Module Name : myproject
Defines Module : YES
Embedded Content Contains Swift : YES
Install Objective-C Compatibility Header : YES
Add #objc in swift class
import UIKit
import SwiftyJSON
#objc class User: NSObject, NSCoding
{
}
Then Import ProjectName-Swift.h in objective c file. But gives error Unknown Type name User
I had tried with add #class User It gives error forward reference using #class
How can I fix this erros
Hopefully you have already solved the problem after reading the documentation referenced by #AnupamMishra, but just in case: try removing from the project the ProjectName-Swift.h file that you created manually. It hides the file of the same name auto-generated by Xcode and not listed in your project. The file is still there, somewhere deep in the DerivedData directory.
Another observation: you didn't have to set Objective-c Generated Interface Header Name in Build Settings. Xcode would generate one and name it myproject-Swift.h by default, since myproject is your Product Module Name.

How does swift know which files to import?

I'm diving into swift from the land of Objective-C, and I'm curious about swift's importing functionality. I've discovered that there's no need to import my own classes, like so:
Objective-C:
#import <UIKit/UIKit.h>
#import "CustomObject.h"
CustomObject* newObject = ...
Swift:
import UIKit
//no need to import CustomObject
var newObject: CustomObject...
My question is, how does swift accomplish this? Where does it look for .swift files to automatically import? Is it just any .swift file that's added to your project's target? I don't want to just handwave this and then get caught by surprise later when something doesn't import like magic!
I'm not sure I'm going to explain this with grace but here it goes...
Let's say you're creating an app called Battlefront. When you're adding files/classes to your application, they are in fact added to your app's module, the Battlefront module. Let's say you created a class called Hero, well, your class is not only Hero, it is Battlefront.Hero but since you're using it in the context of the Battlefront module, there is no need to specify the module name when using your class.
Imports in Swift works mostly with modules. I suppose you could import a single class in Swift but I haven't tried it so I can't comment on this. Let's say you're importing CoreData, well you're importing the whole module by using import CoreData.
By default, classes are using the internal access control. If you wanted to expose classes inside Battlefront to be available to other modules, you'd have to specify your class as public:
// Default is internal, not available outside Battlefront
class Hero {
}
// Public class, is available outside Battlefront
public class Weapon {
}
You can read more on Access Control here.
I suppose you could simplify the relationship to Target equals a Module but that would be taking a shortcut. Could be a start of understanding the concept though.
Short answer: yes, Xcode accesses all .swift files in your project.

using custom Swift framework in another Swift project

I am having an issue using a custom pure Swift framework in another project.
Some notes (as I've believe I've thorougly searched for all possible answers) :
I have my classes declared as public in the framework.
I have successfully built and ran the framework with a Swift target application but only from within the framework project.
I have included my framework in Linked Framework and Libraries and Embedded Binaries.
What i am trying to do is build a pure Swift single view application project by importing only the product framework. The error I am getting is "MyClass is unavailable: cannot find Swift declaration for this class" (which as mentioned above is public) . Also I have an public enumaration with a similar error: "Use of undeclared type 'MY_ENUM'"
Example code below:
import Foundation
import UIKit
import MyFramework /// don't know if this is needed.
public class ViewController: UIViewController{
var myclass:MyClass? ///here is the above error ,it's initialized in the viewDidLoad() function
var myEnum:MY_ENUM = MY_ENUM.MY_ENUM_VALUE /// 2nd enumeration error.
override public func viewDidLoad(){
super.viewDidLoad()
self.myClass = MyClass(arguments) ///same error as above
self.myClass?.myFunction /// ViewController does not have a member 'MyClass' error
/// more code here with errors regarding the class and enum.
}
I've also tried using an objective-C Bridging Header (though I believe this is wrong) and importing my framework header.
#import <MyFramework/MyFramework.h>
Is it possible for a solution to the above or is a restriction from Swift and I am trying something in vain?
One final note: I've included some other headers from another Objective-C framework in my framework because it was the only way to build it as a custom Swift framework. The classes there are visible to the Swift application.
P.S.If more code is needed I'll be happy to provide.
Solved the issue.
Had to put the .swift files together with the .h files as public headers in my custom framework (don't understand why though) and build again.
Maybe the path of your framework is wrong?

Swift build error (involving word "class" as argument) with subclassed Objective-C class

I have a build error when trying to subclass a custom Objective-C class (a subclass of UIViewController) in Swift.
When I try to subclass in Swift, I get the build errors in the picture below. All of them relate to the use of the word class as an argument in the OCMapper library (where I've opened an issue as well).
Some more notes:
In the project, I both import and use Objective-C code in the Swift code and import and use Swift code in the Objective-C code.
I import the compiled Module-Swift.h only in .m and .mm files and forward declare classes that I need in .h files.
I've attempted to create a Module-Swift-Fixed.h class where I forward declare and/or import the custom Objective-C class headers (as recommended here), but that hasn't made a difference.
Has anyone seen anything like this before or have a solution?
I have as yet not been able to trace where in the language spec this is documented, but I suspect you have come across the same problem that I recently faced in objective-c since moving to Xcode 6.4.
I had a message (method) defined as follows
- (BOOL)canProcessClass:(Class) class {
return [class isSubclassOfClass:[NSSet class]];
}
with the same compile error as you mentioned Expected identifier. The fix was simple - just rename the the class argument to something like classToProcess. Which would give you the following
- (BOOL)canProcessClass:(Class) classToProcess {
return [classToProcess isSubclassOfClass:[NSSet classToProcess]];
}
Hence just rename the arguments in your Swift code to not use the (key)word class and you should be fine.
If anyone can point me to the language spec that documents this I would really appreciate it. As far as I'm aware you shouldn't use Class, but I haven't able to find anything about class except the obvious that it is a message (method) available on classes.

Swift: Importing Custom Framework - Use of Unresolved Identifier

After moving some code into an external framework I've been trying to import and use the framework in my app. I've added the framework as a dependency in my app.
My framework is called DiceKit. In one of the classes, just to test things out, I've added import DiceKit to the top of my file. This is not throwing any errors.
When I try to access the classes that should be in the framework, I get a Use of Unresolved Identifier error.
import UIKit
import DiceKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
println (Die(12).roll()) // `Die` doesn't exist at compile time
}
}
What could be causing the classes in the framework to not be compiled? I have made sure that all the classes and methods are marked with public and I haven't changed any build settings from the default in my framework.
I'm using XCode 6.3 Beta
Thanks for your help!
In your DiceKit custom framework
You should declare your Die class as public.
public public public all the things! Or at least, the things that others need to use from the framework. 

Resources