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.
Related
Im trying to write a Swift framework with a protocol used for a delegate method. I've made the protocol public, but when I add my framework to my project, the project spits out the following error
ViewController.swift:15:83: No type named 'MyControllerDelegate' in module 'MyModule'
import MyModule
class ViewController: UIViewController, MyModule.MyControllerDelegate {
Any help in these regards would be appreciated.
Update: not sure if this makes any difference but some of the object types in the protocol are defined by another framework...
Let me explain a bit of the context.
Our project has 1 framework target and 1 test app target that consumes the framework. All logic and code goes to the framework and the test app only imports the framework. No logic is included in the test app.
Now these are my 2 problems:
1. Xcode can't find the class (SearchVC) I declared within the framework target from my XCTest class. I get "Use of unresolved identifier" when I try to create an instance from the class. However, I have no problems when making instances from other classes in the framework target. In fact, I create a property (of type SearchVC) into another class (DashboardVC) and I can access and initialize it correctly, which takes me to the next problem.
2. Xcode can't find the internal property (of type SearchVC) I created (in DashboardVC) from the XCTest class. I'm able to find any other internal properties except this new one.
Things to consider:
I'm using #testable from the XCTest class
Both classes are included in the framework target only while the XCTest class is included in the corresponding Test target.
If I include, as a workaround, the SearchVC in the Test Target I'm able to overcome problem number 1. But, problem 2 persists. XCode is still unable to find the property inside the other class.
These are simplified snippets of the classes.
class DashboardVC : UIViewController, UITableViewDelegate, UITableViewDataSource {
... many other properties ...
var searchVC: SearchVC!
override func viewDidLoad() {
super.viewDidLoad()
searchVC = SearchVC()
}
}
Now the second class
class SearchVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
Then I have been trying to write the following test member of the Test target.
import XCTest
#testable import Invest
class SearchVC: XCTestCase {
... setup and tearDown methods not included for clarity ...
func testSearchVC_isAddedToDashboard() {
let dashboard = DashboardVC()
_ = dashboard.view
XCTAssertNotNil(dashboard.searchVC.view, "View should not be nil")
}
}
Any ideas why this might be happening or how to work around it?
Thank you
I was finally able to make it work.
It turned out to be a glitch from Xcode. For unexplainable reasons, I was able to pull from the latest version of our code and even though I was getting errors, I was able to solve those by taking out every non-test class from the Test Target (the way it should be) and only keep the relevant classes. I was able to access my property and keep the correct separation between Targets.
Has anybody experienced the same glitches?
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?
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.
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.