How to access an Objective-C class method from Swift language - ios

In my Swift app, I need to access a class method called weibo() as below from Objective-C
#interface Weibo : NSObject
+ (Weibo*)weibo;
#end
I have configured the bridging header and tried the following statement
let w = Weibo.weibo() as Weibo
It doesn't work.
UPDATE:
I have forked the repo and fixed this issue as below.
let w = Weibo.getWeibo() as Weibo // the method has been changed.
The reason why it didn't work because Swift treats + (Weibo*)weibo; as a convenience constructor. Since weibo is same as the Class name Weibo although the case is different. I need to change the name to getWeibo to fix this issue to support Swift.
Thanks for every one contributing to this answer. Special thanks to #Anil and #David
Jake

+ (Weibo*)weibo; is the class method of your class Weibo. You could access it in swift like
let w:Weibo = Weibo.weibo()
But it gives me error when i tried('weibo' is unavailable: use object construction 'Weibo()') may be because of method name and class name are same. When i change the method name error goes
let w:Weibo = Weibo.getWeibo() // works: method name changed

That's a class method not a property.
So you would access it like...
let w = Weibo.weibo()
... I think.
Type would be inferred but you could do it as...
let w:Weibo = Weibo.weibo() as Weibo
I believe, and it would still work.

I have the same problem by change code
+(instancetype _Nonnull)manager;
you should change it like this
#property(nonatomic, class, strong, readonly) CKKNetManager *_Nonnull manager;
then you can call in Swift like this
CKKNetManager.manager.get(kGetCompanyInfo, success: {[unowned self] (obj) in
self.hideEmptyView()
self.info = CKKCorpInfo.init(dictionary: obj as! [String : AnyObject])
}) {[unowned self] (errorString) in
self.showEmptyView(in: self.view, with: EmptyViewType.pageLoadFailed)
}

In my situation, my Obj-c class method returned a pointer to a new class not added to my bridging header so the compiler did not make that class method accessible.
You need both the class with the class method you want to call and any class in the class method's description to be included in your bridging header.

In case anyone has the same problems as I had in the past, the reason why I could not access the function from swift is because it was missing the function declaration in the .h (header) file.
So it was only in the .m file and not accessible from my swift file.

Related

Swift init from unknown class which conforms to protocol

I'm currently working on updating a large project from Objective-C to Swift and I'm stumped on how to mimic some logic. Basically we have a class with a protocol which defines a couple functions to turn any class into a JSON representation of itself.
That protocol looks like this:
#define kJsonSupport #"_jsonCompatible"
#define kJsonClass #"_jsonClass"
#protocol JsonProtocol <NSObject>
- (NSDictionary*)convertToJSON;
- (id)initWithJSON:(NSDictionary* json);
#end
I've adapted that to Swift like this
let JSON_SUPPORT = "_jsonCompatible"
let JSON_CLASS = "_jsonClass"
protocol JsonProtocol
{
func convertToJSON() -> NSDictionary
init(json: NSDictionary)
}
One of the functions in the ObjC class runs the convertToJSON function for each object in an NSDictionary which conforms to the protocol, and another does the reverse, creating an instance of the object with the init function. The output dictionary also contains two keys, one denoting that the dictionary in question supports this protocol (kJsonSupport: BOOL), and another containing the NSString representation of the class the object was converted from (kJsonClass: NSString). The reverse function then uses both of these to determine what class the object was converted from to init a new instance from the given dictionary.
All of the classes are anonymous to the function itself. All we know is each class conforms to the protocol, so we can call our custom init function on it.
Here's what it looks like in ObjC:
Class rootClass = NSClassFromString(obj[kJsonClass]);
if([rootClass conformsToProtocol:#protocol(JsonProtocol)])
{
Class<JsonProtocol> jsonableClass = (Class<JsonProtocol>)rootClass;
[arr addObject:[[((Class)jsonableClass) alloc] initWithJSON:obj]];
}
However, I'm not sure how to make this behavior in Swift.
Here's my best attempt. I used Swiftify to try and help me get there, but the compiler isn't happy with it either:
let rootClass : AnyClass? = NSClassFromString(obj[JSON_CLASS] as! String)
if let _rootJsonClass = rootClass as? JsonProtocol
{
weak var jsonClass = _rootJsonClass as? AnyClass & JsonProtocol
arr.add(jsonClass.init(json: obj))
}
I get several errors on both the weak var line and the arr.add line, such as:
Non-protocol, non-class type 'AnyClass' (aka 'AnyObject.Type') cannot be used within a protocol-constrained type
'init' is a member of the type; use 'type(of: ...)' to initialize a new object of the same dynamic type
Argument type 'NSDictionary' does not conform to expected type 'JsonProtocol'
Extraneous argument label 'json:' in call
Is there any way for me to instantiate from an unknown class which conforms to a protocol using a custom protocol init function?
You will likely want to rethink this code in the future, to follow more Swift-like patterns, but it's not that complicated to convert, and I'm sure you have a lot of existing code that relies on behaving the same way.
The most important thing is that all the objects must be #objc classes. They can't be structs, and they must subclass from NSObject. This is the major reason you'd want to change this to a more Swifty solution based on Codable.
You also need to explicitly name you types. Swift adds the module name to its type names, which tends to break this kind of dynamic system. If you had a type Person, you would want to declare it:
#objc(Person) // <=== This is the important part
class Person: NSObject {
required init(json: NSDictionary) { ... }
}
extension Person: JsonProtocol {
func convertToJSON() -> NSDictionary { ... }
}
This makes sure the name of the class is Person (like it would be in ObjC) and not MyGreatApp.Person (which is what it normally would be in Swift).
With that, in Swift, this code would be written this way:
if let className = obj[JSON_CLASS] as? String,
let jsonClass = NSClassFromString(className) as? JsonProtocol.Type {
arr.add(jsonClass.init(json: obj))
}
The key piece you were missing is as? JsonProtocol.Type. That's serving a similar function to +conformsToProtocol: plus the cast. The .Type indicates that this is a metatype check on Person.self rather than a normal type check on Person. For more on that see Metatype Type in the Swift Language Reference.
Note that the original ObjC code is a bit dangerous. The -initWithJSON must return an object. It cannot return nil, or this code will crash at the addObject call. That means that implementing JsonProtocol requires that the object construct something even if the JSON it is passed is invalid. Swift will enforce this, but ObjC does not, so you should think carefully about what should happen if the input is corrupted. I would be very tempted to change the init to an failable or throwing initializer if you can make that work with your current code.
I also suggest replacing NSDictionary and NSArray with Dictionary and Array. That should be fairly straightforward without redesigning your code.

How to use swift class in objective c (not an instance of the class)

All tutorials for using swift code in a primarily objective-c app goes through this process mentioned in this Apple document https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
But this methodology of importing a xyzProjectName-swift.h file and then using
MySwiftClass *swiftObject = [[MySwiftClass alloc] init];
[swiftObject swiftMethod];
required me to create an instance of that swift class. Is it possible to directly access the class itself like [MySwiftClass swiftMethod];
I have not been able to do this until now. Do I have to change my swift code in a certain way to achieve this?
Just tried the following:
class MySwiftClass: NSObject {
static func swiftMethod() -> String {
return "hello"
}
}
And in Objective-C:
NSLog(#"Received from Swift: %#", [MySwiftClass swiftMethod]);
Works for me - are you sure your swiftMethod is static, and not private? You could also try adding #objc in front of the static func – if nothing else, at least it might warn you if there is a reason it can't be accessed from Objective-C.

Make a Data Class with Swift in Watchkit Target

As simply using the sharedApplication() method is unavailable in WatchKit, utilizing the AppDelegate as a way to store data for all my interfaces/views is no longer an option in WatchKit. Is there an alternative method to do this in swift for WatchKit apps? I have tried making a singleton class like so:
import Foundation
import UIKit
class data: NSObject {
class var sharedInstance : data {
struct Example {
static let instance = data()
}
return Example.instance
}
var value:Int = 0
var increment:Int = 1
}
(This code is from another StackOverflow post, I just forgot the link.) I then try and access the Integer "value" by calling data.value in an interface controller but I get the error 'data.Type' does not have a member named 'value'. How can I fix this error? Or is there a better way to do what I am trying to achieve?
Thanks
The code data.value attempts to access a class variable or method on the class data called value. Since no such variable or method exists you get the error message.
In your definition value is defined as a property on the class data, so to use it you need a valid instance of data. Your sharedInstance method provides the instance.
Also, the convention in Swift is to capitalize class names, so I recommend using Data.

Cannot access variables in a Swift class from Objective-C file

I'm trying to continue with Swift in an existing project I had started in Objective-C. I followed the Apple documentation and I managed to access a Swift class from an Objective-C file, via including the "ProductModuleName-Swift.h" header in my Obj-C file.
At this point here's my code:
#include "Pianoconcert_App-Swift.h"
#interface ModelsVC ()
#end
#implementation ModelsVC
// And all that kind of stuff
// ...
-(IBAction)comanda:(UIButton *)sender {
ComandaTableVC *controller = (ComandaTableVC *)self.tabBarController.viewControllers[2];
// Here goes the problematic code
[self.tabBarController setSelectedIndex:2];
}
This piece of code has no problems. But now I just want to set one of the variables in the Swift class like this: controller.selectedModel = sender.tag, but Xcode just tells me the variable does not exist.
Here's an extract of my Swift class:
import UIKit
class ComandaTableVC: UITableViewController, UIPickerViewDataSource, UIPickerViewDelegate {
// Declaration of some constants and variables
// And here goes the one
var selectedModel: Int = 0
// And a bunch more of variables and functions
// ...
}
I don't know what I'm doing wrong. The class seems fully included and, actually, I can access that variable and all the others if I use the class from another Swift file, like this:
var controller: ComandaTableVC = self.tabBarController.viewControllers[2] as ComandaTableVC
controller.selectedModel = 2
It's been resolved finally, but I can't explain how.
Suddenly the compiler threw a build error telling me that the file Pianoconcert_App-Swift.h could not be found.
I changed it for PianoconcertApp-Swift.h, deleting the underscore that replaced the blank space, and now I can access all the variables and constants correctly.

I there a way, to view the exposed Objective-C header to swift

I have problems to understand, why some member functions from an imported (and complicated) set of Objective-C interface are not available in Swift.
I have a Bridging-Header File:
#import "EvernoteSDK.h"
and I can't use some member functions in my ViewController
let userStore = EvernoteUserStore()
userStore.initWithSession(session)
initWithSession is not available for the swift code, but why?
The objective-C header shows:
#interface EvernoteUserStore : ENAPI
+ (instancetype)userStore;
- (id)initWithSession:(EvernoteSession *)session;
If I could view the exposed Objective-C header, I may understand, how the mangling works
In Swift the initializer call is combined with the constructor. In other words, Objective-C's
EvernoteUserStore *userStore = [[EvernoteUserStore alloc] initWithSession:session];
becomes
let userStore = EvernoteUserStore(session:session);
The tool recognizes the initWithSomething: name of Objective-C, and converts it to
init(something something : SomeType)
In case of EvernoteUserStore the corresponding init method looks like this:
init(session session: EvernoteSession!)

Resources