swift call oc class getter method in oc project - ios

oc project;
I has class like this
#interface MyObject : NSObject
#property(noatomic,getter=getNode)id node;
#end
and in swift file
var node = MyObject().getNode
and getNode method not found ?
anyone helps

Related

Error while assigning Swift delegate to Objective-C object

This is my Swift class :
class MyClass : NSObject {
public var inAppMessagesController: MPInAppMessagesController!
fun myFunction() {
self.inAppMessagesController.inAppInteractionDelegate = self // Error in this line - Cannot assign value of type 'MyClass' to type 'MPInAppMessageControllerDelegate?'
}
}
extension MyClass : MPInAppMessageControllerDelegate {
// Functions
}
As stated in comments, this is the error -
Cannot assign value of type 'MyClass' to type
'MPInAppMessageControllerDelegate?'
inAppInteractionDelegate in Objective-C class MPInAppMessagesController :
#interface MPInAppMessagesController : NSObject
#property (nonatomic, weak, nullable) id <MPInAppMessageControllerDelegate> inAppInteractionDelegate;
#end
MPInAppMessageControllerDelegate declared in MPInAppMessagesController.h :
#protocol MPInAppMessageControllerDelegate<NSObject>
// Functions
#end
The only missing part is you need to include this class inside the bridging file
#import "MPInAppMessagesController.h"
Look here to a SwiftObjc

Person<Person>, what is <Person>?

I cloned an iOS project from git repository. The project is written in Objective-C.
I saw this code in a header file:
#interface Employee: Person<Person> {
...
}
What does Person <Person> mean? I know it indicates Employee is a subclass of Person but what is the special meaning of <Person> ?
Employee is a subclass of a class called Person and also conforms to a protocol called Person.
#interface MySubclass : MySuperclass <MyProtocol> { ... }

Swift - Bridging header failed. '*' does not have a member named '*'

I develop an app that using CommonCrypto library. The problem is I can create an instance in Swift file. My object created using Objective- C. It seems can't create bridging header very well.
Error message
/Users/MNurdin/Documents/iOS/xxxxx/Models/Main.swift:15:9: 'CustomObject' does not have a member named 'encrypt'
CustomObject.h
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCrypto.h>
#import "GTMBase64.h"
#interface CustomObject : NSObject
+ (NSString*)encrypt:(NSString*)plainText withKey:(NSString*)key;
#end
CustomObject.m
#import "CustomObject.h"
#implementation CustomObject
+ (NSString*)encrypt:(NSString*)plainText withKey:(NSString*)key{
/*--*/
return result;
}
#end
Global.swift
var instanceOfCustomObject: CustomObject = CustomObject()
println(instanceOfCustomObject.encrypt("p#$$w0rd","12345678"))
The initial + in the declaration indicates that
+ (NSString*)encrypt:(NSString*)plainText withKey:(NSString*)key;
is a class method in Objective-C. You have to call it on the
class (or type in Swift linguage) itself, not on an instance:
let encrypted = CustomObject.encrypt("p#$$w0rd", withKey: "12345678")

Swift instance variable with protocol

I have to translate the following lines of Objective-c code into swift. This is a sample from the Objective-c JSONModel-Framework where the Optional protocol provided by the Framework is applied to an instance variable of type NSString. I found a related post but i didn't managed to achieve it. With my MYModel.swift implementation Xcode complains Cannot specialize non-generic type NSString
thx for your help!
MYModel.swift
#objc(MYModel) public class MYModel : JSONModel {
...
public var name : NSString<Optional>
...
}
MYModel.h
#interface MYModel : JSONModel
...
#property (strong, nonatomic) NSString<Optional>* name;
...
JSONModel.h
...
/**
* Protocol for defining optional properties in a JSON Model class. Use like below to define
* model properties that are not required to have values in the JSON input:
*
* #property (strong, nonatomic) NSString<Optional>* propertyName;
*
*/
#protocol Optional
#end
...
The < and > are not for conforms to protocol. It is for Types with generics like Array:
Array<T>
so you can write var a: Array<String>.
You want something else, a variable should be a Type String and conform to the protocol
You can extend String with the protocol and add the needed functions yourself.
Since your Optional protocol is empty, it is enough to write:
extension NSString: Optional {} // you can use String if you like
To create the protocol write in Swift:
protocol Optional {}
You can Objective-C create the protocol, too.
You should not use Optional, because there is already one, but because Swift has namespacing, it works.
You could of course write something like that:
extension NSString: JsonOptProtocol {}
protocol JsonOptProtocol {} // or create that in Objective-C like you did
Documentation link.
Optional is a type declared in the standard library of Swift, at the moment JSONModel is not compatible with Swift because of this.

NS_ENUM error when declared before class instance variables

Example .h file:
#interface MyClass : NSObject
typedef NS_ENUM(int, myType) {
Something,
SomethingElse,
SomethingElseElse,
YetAnotherSomethingElse
};
{ //Error On This Line: Expected Identifier or '('
int aInstanceVariable;
}
//Some Methods go here
#end
Why am I getting that error (see the comment in the code above)? It works fine when below the class instance variable declaration, but I would like to use it as the type for one of my instance variables.
Thanks to #CarlVeazey, I discovered that the answer was simple: Move the typedef declaration to above #interface. The reason for this is that types cannot be owned by a class or an instance of a class, and therefore cannot be in the interface for a class.

Resources