Swift: import framework but can't find class in it - ios

I import a swift framework to a swift project, but when I call the class in this framework, Xcode throw a compile error "Use of undeclared type 'xxxx(class name)' ".
I feel Xcode have found the framework, otherwise it will complaint "can't find xxx(framework name)".
But why Xcode can't find the class of this framework.
I have tried remove and re-add framework, and delete DeivedData files, but all of them not work. I haven't use CocoaPods to import framework.
Any idea?

In a Framework that was built for Release (and not Debug), the class symbols need to be accessible.
Make sure you are you trying to access Public or Open classes from your Swift framework.
// set the Framework class to Public
public class rnHello{
// set the initializer to public, otherwise you cannot invoke class
public init() {
}
// set the function to public, as it defaults to internal
public static func world() {
print("hello from a static method")
}
}
Now you can access this via your Swift code or if you attach lldb using:
lldb) po rnHello.world()

Make sure that the FrameWorkSearch path in the BuildSettings of the project is reflecting the correct path to your framework.

Related

Call Method in framework

I have a custom swift framework that I am trying to access via my project.
The method in my framework is
public func test(url:String, callType:String){
}
and I am trying to access it from my main project with
import FrameworkName
FrameworkName.FrameworkSwiftClass.test()
The problem is that it looks like it is looking for
FrameworkName.FrameworkSwiftClass.test(FrameworkSwiftClass)
Why is XCode telling me extra argument when I try
FrameworkName.FrameworkSwiftClass.test(url:"url", callType:"type")
You are using the method as if it was a static method but it's not, you have to instantiate your class first:
let framework = FrameworkName.FrameworkSwiftClass()
framework.test(url:"url", callType:"type")

Unresolved public classes in a Cocoa Touch Framework

I'm trying to build a framework to export a few classes as an importable "package of sort".
My Framework project is called "MyFramework"... is was created as a Cocoa Touch Framework project in Swift.
I've added a single swift file called ImplementationTest.swift
It contains the following :
public class ImplementationTest {
public init(){
print("Init called");
}
public func echo(request: String){
print ("Echo called with "+request);
}
}
The framework builds fine and I get a MyFramework.framework folder.
I add that framework to a new swift application project and added it as Linked Framework and Library as well as Embedded Binaries.
I can "import MyFramework" just fine.
But if I try "var test=ImplementationTest()", I get the following error :
use of unresolved identifier "ImplementationTest"
If I try "var test=MyFramework.ImplementationTest()" I get :
module 'MyFramework' has no member name "ImplementationTest"
Am I doing this wrong ? Can my framework only have one single exported class ? and should it be named the same name as the framework itself ?
Check after you build the framework if you can access it via in, show finder.
If not, then try building it using generic ios device.

Write Unity IOS plugin in Swift code

Is it possible to write unity IOS plugin in Swift?
I already have a working swift framework and want to use it as a plugin in Unity
I saw some places which say it can only be done on Objective-c but is there a workaround for swift?
How to call Unity methods
Unity interface functions are defined in UnityInterface.h in Xcode project built by Unity. This header file is imported in UnitySwift-Bridging-Header.h, so you can call the functions directly in your Swift codes.
To call Unity methods, use UnitySendMessage function like below:
// Example.swift
import Foundation
class Example : NSObject {
static func callUnityMethod(_ message: String) {
// Call a method on a specified GameObject.
UnitySendMessage("CallbackTarget", "OnCallFromSwift", message)
}
}
How to access Swift classes from Unity
Step 1: Create your Swift classes.
// Example.swift
import Foundation
class Example : NSObject {
static func swiftMethod(_ message: String) {
print("\(#function) is called with message: \(message)")
}
}
Step 2: Include "unityswift-Swift.h" and define C functions to wrap Swift classes in .mm file (Objective-C++).
// Example.mm
#import <Foundation/Foundation.h>
#import "unityswift-Swift.h" // Required
// This header file is generated automatically when Xcode build runs.
extern "C" {
void _ex_callSwiftMethod(const char *message) {
// You can access Swift classes directly here.
[Example swiftMethod:[NSString stringWithUTF8String:message]];
}
}
Step 3: Create interface class to call exported C functions from C#.
// Example.cs
using System.Runtime.InteropServices;
public class Example {
#if UNITY_IOS && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern void _ex_callSwiftMethod(string message);
#endif
// Use this method to call Example.swiftMethod() in Example.swift
// from other C# classes.
public static void CallSwiftMethod(string message) {
#if UNITY_IOS && !UNITY_EDITOR
_ex_callSwiftMethod(message);
#endif
}
}
Step 4: Call the method from your C# code.
Example.CallSwiftMethod("Hello, Swift!");
The file names of UnitySwift-Bridging-Header.h and unityswift-Swift.h are defined in "Objective-C Bridging Header" entry and "Objective-C Generated Interface Header Name" entry in Build Settings. These settings and other settings about Swift compiler are set automatically by PostProcesser when the Unity build runs.
Requirements
iOS 7 or later
Compatibility
Unity 5.3.5f1 Xcode 7.3.1
As top-level Swift is simply not accessible from Unity, the "workaround" for Swift is to write an Objective-C wrapper class around it, and access that.
Depending on the amount and complexity of your Swift code that might still be the most optimal approach.

'Use of Unresolved Identifier' in Swift

So I have been making an app, and everything has been working great. But today I made a new class like usual and for some reason in this class I can't access Public/Global variable from other classes. All the other classes can, but now when ever I try to make a new class I can't. How would this be fixed?
I am using Swift and Xcode 6.
Working Class:
import UIKit
import Foundation
import Parse
import CoreData
var signedIn = true
class ViewController: UIViewController {
New Class:
import UIKit
class NewClass: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
signedIn = false
}
But on signedIn = false
I get the error:
use of unresolved identifier "signedIn"
One possible issue is that your new class has a different Target or different Targets from the other one.
For example, it might have a testing target while the other one doesn't. For this specific case, you have to include all of your classes in the testing target or none of them.
Once I had this problem after renaming a file. I renamed the file from within Xcode, but afterwards Xcode couldn't find the function in the file. Even a clean rebuild didn't fix the problem, but closing and then re-opening the project got the build to work.
'Use of Unresolved Identifier' in Swift my also happen when you forgot to import a library. For example I have the error:
In which I forgot the UIKit
import UIKit
Sometimes the compiler gets confused about the syntax in your class. This happens a lot if you paste in source from somewhere else.
Try reducing the "unresolved" source file down to the bare minimum, cleaning and building. Once it builds successfully add all the complexity back to your class.
This has made it go away for me when re-starting Xcode did not work.
Another place I've seen this error is when your project has multiple targets AND multiple bridging headers. If it's a shared class, make sure you add the resource to all bridging headers.
A good tip to is to look in the left Issue Navigator panel; the root object will show the target that is issuing the complaint.
For me this error happened because I was trying to call a nested function. Only thing I had to do to have it fixed was to bring the function out to a scope where it was visible.
In my case, I had an Object-C file which was also in the same Target Membership. I fixed by adding #import "YourObjectCFileHeader.h" inside file Bridging-Header.h
Because you haven't declared it. If you want to use a variable of another class you must use
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestViewController : ViewController = segue.destinationViewController as ViewController
DestViewController.signedIn = false
}
You have to put this code at the end of the NewClass code
Your NewClass inherits from UIViewController. You declared signedIn in ViewController. If you want NewClass to be able to identify that variable it will have to be declared in a class that your NewClass inherits from.
I got this error for Mantle Framework in my Objective-Swift Project.
What i tried is ,
Check if import is there in Bridging-Header.h file
Change the Target Membership for Framework in Mantle.h file as shown in below screenshot.
Toggle between Private Membership first build the project , end up with errors.
Then build the project with Public Membership for all the frameworks appeared for Mantle.h file, you must get success.
It is just a bug of building with multiple framework in Objective-C Swift project.
If this is regarding a class you created, be sure that the class is not nested.
F.e
A.swift
class A {
class ARelated {
}
}
calling var b = ARelated() will give 'Use of unresolved identifier: ARelated'.
You can either:
1) separate the classes if wanted on the same file:
A.swift
class A {
}
class ARelated {
}
2) Maintain your same structure and use the enclosing class to get to the subclass:
var b = A.ARelated
I did a stupid mistake. I forgot to mention the class as public or open while updating code in cocoapod workspace.
Please do check whether accesor if working in separate workspace.
You forgot to declare the variable. Just put var in front of signedIn = false
My issue was calling my program with the same name as one of its cocoapods. It caused a conflict. Solution: Create a program different name.
This is not directly to your code sample, but in general about the error. I'm writing it here, because Google directs this error to this question, so it may be useful for the other devs.
Another use case when you can receive such error is when you're adding a selector to method in another class, eg:
private class MockTextFieldTarget {
private(set) var didCallDoneAction = false
#objc func doneActionHandler() {
didCallDoneAction = true
}
}
And then in another class:
final class UITextFieldTests: XCTestCase {
func testDummyCode() {
let mockTarget = MockTextFieldTarget()
UIBarButtonItem(barButtonSystemItem: .cancel, target: mockTarget, action: MockTextFieldTarget.doneActionHandler)
// ... do something ...
}
}
If in the last line you'd simply call #selector(cancelActionHandler) instead of #selector(MockTextFieldTarget.cancelActionHandler), you'd get
use of unresolved identifier
error.

iOS Swift unit test result in unresolved identifier

Simple function in swift for testing:
func testExample() {
var tagname = "someClass()"
var logger = Device("", "") //unresolved identifier
XCTAssert(true, "Pass")
}
Even after I import my module with "import ", I still cannot use classes from my module. Also though I might have messed something up in the project, but NONE of my sample projects will let me use module classes.
Seems like it should work but might have broken in beta 2.
EDIT: fixed it
The IDE didn't pick up the check for the param names. Seems Xcode is still a tad iffy
I hit the same issue today, not sure if this is only available recently - rather than using import TARGET_NAME in your test file and/or declaring your classes/methods as public, you can add your file to your Tests target via XCode's File Inspector.
Cmd + Opt + 1 to show it while in any file, and check the box under Target Membership for your Tests Target.
You may need to rebuild.... Cmd + b.
Should this be specific to my system.... I'm running Xcode 6.3 beta 1, and testing via Quick + Nimble, both installed with the latest cocoapods beta.
The problem for me was that I had non-alphanumerical characters in my main target name.
I had to import it the following way (Note the special #testable annotation)
#testable import my_tutorial_app
It seems that in Xcode 6 Beta 4 you need to declare public classes and methods as "public". Example:
public class Device {
public init(...) {
}
public func myMethod(...) {
}
}
Now they are accessible from the swift test class.
I had the same problem and discovered that it works if I call it like this:
SuperStructName.StructName

Resources