Call Method in framework - ios

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

Related

Not able to use Kotlin Extension Function written in common in iOS as Swift Extension

I have a Kotlin Multiplatform project setup with Android & cocoapods for iOS.
I have a file Extensions.kt in commonMain/src with the following function:
fun String.isValidEmail(): Boolean {
return validate(ValidatorRegex.EMAIL)
}
I am able to access this function in Android as a String extension:
"abcd#gmail.com".isValidEmail()
But in iOS using Swift, I need to call it as a static method of another class:
ExtensionsKt.isValidEmail("abcd#gmail.com")
It should convert that commonMain/src method to a Swift extension of String instead of a class with a static method.
Am I missing any configuration?
You're doing everything right here. Unfortunately, this option is not available for now. Extensions conversion may be performed correctly for some classes, but Swift's String is not the one. This is a known inconvenience, and K/N team is working hard to make it better.

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

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.

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.

I want to call a class function written in Swift from the AppDelegate (which is in Objective C). What am I doing wrong?

I have a swift file "SomeController.swift" it is like this:
import Foundation
func performSomeStuff() {
println("Performing stuff")
}
Now in the app delegate, I am trying to do this: (note that the swift bridging header is imported)
[SomeController performSomeStuff]
But its not working.
I have also tried this:
import Foundation
class SomeController:NSObject {
class func performSomeStuff() {
println("Performing stuff")
}
}
But it still fails.
What is the correct way?
Add:
#objc
before the class keyword in your swift code so it will be:
#objc class SomeStuff: NSObject {
}
Also add #obj in front of any function that you want to call.
Then in your app delegate make sure to use #import "projectName-Swift.h"
Are you able to access "SomeController" class in objective-c, if not then you firstly need to add "${ProjectName}-Swift.h file and add Swift compilation support in Build settings as:
And for accessing methods from Swift to Objective-C, add
import Foundation
class SomeStuff:NSObject {
#objc class func performSomeStuff() {
println("Performing stuff")
}
}
before functions name.
In some cases, you need finer grained control over how your Swift API is exposed to Objective-C. You can use the #objc attribute if your Swift class doesn’t inherit from an Objective-C class, or if you want to change the name of a symbol in your interface as it’s exposed to Objective-C code.
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html
Ok, So I figured it out. First, here's the correct way. Its like some of you said:
In the swift file, I have it like this:
class MyController:NSObject{
class func performTask {
// Here my task is running.
}
}
Then in the app delegate, I just import the swift header file. And do this:
[MyController performTask];
Now for the part I had wrong. It was an error on my part, but maybe it'll be useful to someone else out there.
When I first created the first swift file, I had placed it inside a folder within the my Source folder. But when I started having multiple swift files, I moved the bridging header outside that folder and into the main Source folder (just for organising).
The problem was, it did not give me a direct error to tell me what was the problem. I had to check the issue navigator to identify the problem.
Hope this helps someone out there.

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

Resources