I'm trying to load classes from a framework dynamically but the application crashes with conversion sending SIGABRT signal:
let newClassType = NSClassFromString("MyFramework.CustomClass") as! BaseClass.Type
With classes declared in MyApp it works fine as well as with framework's classes but without conversion.
BaseClass.swift (MyApp.xcodeproj and copied to MyFramework):
public class BaseClass{
...
}
CustomClass.swift(MyFramework.xcodeproj):
public class CustomClass: BaseClass{
...
}
What might be the problem?
From The question and the comments I suggest to use typealias
import MyFramework
typealias FrameworkBaseClass = MyFramework.BaseClass
typealias FrameworkCustomClass = MyFramework.CustomClass
Then if you want to use FrameworkBaseClass, FrameworkCustomClass in other .swift files, you do not have to import MyFramework
Related
I'm merging my Swift project with already existing Objective-C code. I need to call some important methods of Swift class from objective C app delegate. I tried all methods given in net, but it was no use. Can any one help me out?
Yes, it's possible but with some limitations.
You can use only classes which inherited from NSObject, with public attribute and marked with #objc. At Objective-C code you should import "ProductModuleName-Swift.h" file which generated by compiler.
Here is an example of Swift class:
import Foundation
#objc public class ExampleClass: NSObject {
#objc public var someInstanceProperty = "Property"
#objc public func someFunction() {
print("Some function")
}
}
Notice that this class inherited from NSObject and have #objc and public attributes. After command+B you can take a look at generated bridge header through Assistance editor:
Then you should import the bridge header at your Objective-C class.
#import "ProductModuleName-Swift.h"
And then you can use your Swift class at Objective-C code like any other Objective-C class:
__auto_type const someClass = [ExampleClass new];
[someClass someFunction];
NSLog(#"%#", someClass.someInstanceProperty);
Here is an additional information from Apple:
Importing Swift into Objective-C
In my project i have many swift classes that extend generic classes. And Now i'm in a need to send data from objective-c class to these swift classes. Is there any possible way to do this ?
I have tried using #objc before class as shown below
but it didn't allow me to do this.
I followed this answer stack link. But i checked when i run my app with this code it dint create the variable in the Project-swift class as shown in below image
as the variable is not created here so it gives error while calling the variable in objective-c class.
Kindly help out with better solution.
I got my solution by creating another class which contain a protocol and i indirectly call the generic extended class.
class EntityDataTable :BaseData<EntitySw> {
}
this is the generic extended class
and the another class i made to indirectly access this class in objective-c is
#objc class EntityService : NSObject {
var entityProtocol:EntityDataProtocol?
override init() {
super.init()
entityProtocol = returnDataObject()
}
func returnDataObject() -> EntityDataTable {
return EntityDataTable()
}
}
#objc protocol EntityDataProtocol{
func addFromService(source: CWEntity)
func getAll() -> [EntitySw]
}
Then i imported the Project-swift.h file in objective c class and made the object of swift class
I have the following set of files in my project:
The IAPHelper class uses the objective-c through a bridging header. I was wondering if I could use the IAPHelper class in both my app and its app extension like this:
(Setting IAPHelper's target membership)
Without making KeychainWrapper being a member of both classes.
No it does not seem to work.
I made TestClassOne
import Foundation
class TestClassOne {
static internal func addOneOne() -> Int {
return 2;
}
}
TestClassOne is only part of one target.
I made TestClassTwo
import Foundation
class TestClassTwo {
internal static func callTestClassOne() -> Int {
let i = TestClassOne.addOneOne()
return i
}
}
TestClassTwo belonged too both targets. I would keep on getting the error TestClassOne not recognized when the TestClassOne file was set to only one target but once I set the TestClassOne file to both targets that error stopped appearing.
In Xcode, when I create a new file extends NSManagedObjectContext class, add methods in this extension file, compile the project and there is no problem.
NSManagedObjectContext+BaseModel.swift
import Foundation
import CoreData
import CoreDataStack
extension NSManagedObjectContext {
public func insertObject<T: NSManagedObject>() -> T {
guard let object = NSEntityDescription.insertNewObjectForEntityForName(T.modelName(), inManagedObjectContext: self) as? T
else {
fatalError("Invalid Core Data Model")
}
return object
}
}
However, when I use it in the ViewController.swift, like context.insertObject(), the compiler reports an error saying:
Value of type 'NSManagedObjectContext' has no member 'insertObject'
After searching in StackOverflow, I found a post "Using extensions in separate .swift file". As the post author said, the above problem disappeared when I put the extension NSManagedObjectContext code in my custom CoreDataStack.swift. But why?? Should not extension Apple's class be in a separate file??
This is so weird. Normally I could understand a class not being declared, but this is claiming the Object class itself is not declared. NSObject works, but the way my project is setup I need it to be a pure Swift Object. My class header looks like this:
import UIKit
import Foundation
class Person: Object {
I know foundation isn't really necessary, I just added it because I wasn't sure if that was causing me issues. The issue is occurring in both actual projects and playgrounds as well as in Xcode 6 and the latest Xcode 7 beta with Swift 2.0
if you are using Realm then import
import RealmSwift
Complete syntax of class.
import UIKit
import RealmSwift
class User: Object {
}
If you face any difficulty to implement this code. Watch below video
Link: https://youtu.be/5Z8tBKwk6-c
Just remove : Object. Pure Swift classes don't need to inherit from a superclass
Pure Swift object doesn't inherit from a superclass:
class Person {}
You can read Classes and Structures
For RealmSwift
import Foundation
import RealmSwift
class Note: Object{
#objc dynamic var id = 0
#objc dynamic var title = ""
#objc dynamic var created = Date()
#objc dynamic var text = ""
override static func primaryKey() -> `enter code here`String? {
return "id"
}
}