How to access an Objective-C NSMutableDictionary from a Swift class? - ios

I have an Objective-C controller called linkedinlogincontroller. Inside that controller I have an NSMutableDictionary called result.
Simply, all I need to know is what is the best practice way of having access to that dictionary in a Swift controller?

Look at this link:
Using Objective C & Swift together.
Expose the ObjectiveC class in <YourProjectName>-Bridging-Header.h.
#import "linkedinlogincontroller.h"
ObjectiveC Header
// linkedinlogincontroller.h
#import <UIKit/UIKit.h>
#interface linkedinlogincontroller : UIViewController
#property (nonatomic, strong) NSMutableDictionary * result;
#end
ObjectiveC Implementation
// linkedinlogincontroller.m
#import "linkedinlogincontroller.h"
#import "<YourProjectName>-Swift.h"
#implementation linkedinlogincontroller
...
self.result = [NSMutableDictionary dictionaryWithDictionary:#{#"key":#"value"}];
NSLog(#"Print from Objc %#", self.result);
YourSwiftClass * so = [[YourSwiftClass alloc] init];
[so printFromSwift:self];
...
Swift Implementation
// YourSwiftClass.swift
import Foundation
#objc class YourSwiftClass : NSObject {
func printFromSwift(vc:linkedinlogincontroller) {
println ("Print from Swift \(vc.result)")
}
}

You should just be able to use it like a swift dictionary. To access an element in it:
linkedinlogincontroller.result[key]
should work fine.

Related

Inherit from a Swift class in Objective C

I'm successfully mixing and matching Obj-C and Swift in an Xcode 7 project. However, I can't seem to figure out how, in an Objective C class, to inherit from a Swift class (and yes I know about declaring that Swift class as #objc for visibility). In this case the desired Swift superclass MySwiftViewController is a subclass of UIViewController. For now, in Obj-C, I'm inheriting directly from UIViewController and not gaining access to the capabilities I added in MySwiftViewController.
Here's what i understand:
-- To declare an Obj-C class as inheriting from something, that must be in the .h file after the ':':
#import <UIKit/UIKit.h>
#interface RootViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#end
-- To make Swift classes visible, that is #imported:
#import "MyProject-Swift.h"
However, you cannot import the Swift auto-generated bridging header into the Obj-C .h file. You also cannot forward-declare an opaque superclass with #class. So, is this possible and how?
Unfortunately, it's not possible to subclass a Swift class in Objective-C. Straight from the docs:
You cannot subclass a Swift class in Objective-C.
See Apple's guide on interoperability for more details on what you can and cannot access with Objective-C.
As for Xcode 8.0 and earlier there is dirty-hacky solution, that probably will be fixed in the future.
If you want to subclass from swift file, you can add objc_subclassing_restricted attribute. You can make it as macro for convenience.
Code:
Swift class.
import Foundation
class SwiftClass : NSObject {
func say() {
print("hi");
}
}
Objc class:
#import <Foundation/Foundation.h>
#import "test-Swift.h"
#define SWIFT_SUBCLASS __attribute__((objc_subclassing_restricted))
SWIFT_SUBCLASS
#interface ObjcClass : SwiftClass
- (instancetype)init;
#end
#implementation ObjcClass
- (void)say {
NSLog(#"oops");
}
#end
But, as I understand, it is not supported, and you may have any sort of bugs because of it. So it is not guide to action, and more like curious thing to know.
In fact, it can be achieved by category:
Swift code
import UIKit
#objc open class TestModel: NSObject {
#objc var testName: String = String()
}
Objective C .h file code
#import <Foundation/Foundation.h>
#import "Test-Swift.h"
#interface TestModel (Add)
- (void)configTestName;
#end
Objective C .m file code
#import "TestModel+Add.h"
#implementation TestModel (Add)
- (void)configTestName {
self.testName = #"12323";
}
#end

Proxy Pattern in iOS - Swift

I need to create proxy pattern in iOS using swift
I have tried it using Objective C and here is the code
MyProtocol.h
#import <Foundation/Foundation.h>
#protocol MyProtocol <NSObject>
#required
-(void)testMessage;
#end
TestBO.h
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
#interface TestBO : NSObject <MyProtocol>
#end
TestBO.m
#import "TestBO.h"
#implementation TestBO
-(void)testMessage{
NSLog(#"Test Message");
}
#end
TestProxyHandler.h
#import <Foundation/Foundation.h>
#interface TestProxyHandler : NSProxy
#property (nonatomic, strong) id object;
- (instancetype)initWithProtocol:(Protocol *)protocol andObject:(Class)clazz;
- (void)forwardInvocation:(NSInvocation *)invocation;
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector;
#end
TestProxyHandler.m
#import "TestProxyHandler.h"
#import "TestBO.h"
#implementation TestProxyHandler
- (instancetype)initWithProtocol:(Protocol *)protocol andObject:(Class)clazz{
if ([clazz conformsToProtocol:#protocol(MyProtocol)]) {
self.object = [[clazz alloc] init];
}else{
NSLog(#"Error it does not conform to protocol");
}
return self;
}
- (void)forwardInvocation:(NSInvocation *)invocation{
NSString *selString = NSStringFromSelector(invocation.selector);
NSLog(#"Called %#",selString);
[invocation invokeWithTarget:self.object];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
return [self.object methodSignatureForSelector:selector];
}
#end
I have invoked it using
id <MyProtocol> delegate = (TestBO *)[[TestProxyHandler alloc] initWithProtocol:#protocol(MyProtocol) andObject:[TestBO class]];
[delegate testMessage];
But I am not able to make it work in Swift even the initialzier is showing that the message
TestHandler.swift
import Foundation
class TestHandler: NSProxy {
var object: AnyObject
convenience override init(`protocol`: Protocol, andObject clazz: AnyClass) {
if clazz.conformsToProtocol() {
self.object = clazz()
}
else {
NSLog("Error it does not conform to protocol")
}
}
}
Does anyone have any clue to do this in swift ??
EDIT:
In java you can create runtime implementation of a method using the Proxy.newProxyInstance call but can this be achieved in iOS ? using swift ? Any clue ?
Comparing with Objective C and Swift, Swift offers extremely limited access to runtime language access . So based on my research till now it can’t be done :(
I even tried subclassing the NSProxy class in swift but just couldn’t call the super.init and code never compiles but however same thing works in objective C
So I ended up doing this approach
I created a protocol using
#objc protocol SomeProt {
// Some method
}
Note the keyword #objc before protocol is essential else you would not be able to pass it as a variable, also adding #objc limits the usage of protocol to objective c runtime features so don’t expect to get full features of protocols in swift
public func someMethod(`protocol` : Protocol, implementation : AnyClass) {
let isImplemented : Bool = implementation.conformsToProtocol(`protocol`)
// some code
}
If you need to use it in some dictionary or places where it should conform to NSCopying class then use
NSStringFromProtocol
and
NSProtocolFromString
methods
Now I have wrote a objective c helper class to do the initialization
ObjcHelper.h
#import <Foundation/Foundation.h>
#interface ObjcHelper : NSObject
+(NSObject *)objectForClass:(Class)clazz;
#end
ObjcHelper.m
#import "ObjcHelper.h"
#implementation ObjcHelper
+ (NSObject *)objectForClass:(Class)clazz{
return [[clazz alloc] init];
}
#end
Now to use it
let prot : SomeProt = ObjcHelper.objectForClass(NSClassFromString("PROT_HANDLER_CLASS_NAME")) as! SomeProt
However in future if anyone can offer a better answer then please be sure to post it here

Can't see Swift object hierarchy in debug area when used within Objective-C class

My problem may be so simple but I'm so lost in it.
Any comment, idea, help, prediction would be so useful.
Here are my classes
TrialSwiftClass.swift
import Foundation
#objc public class TrialSwiftClass : NSObject{
var first : String?
var second : NSString?
var third : NSNumber = 0
override init(){
super.init()
}
init(data:NSArray){
self.first = data[0] as? String
self.second = data[1] as? NSString
self.third = data[2] as! NSNumber
}
}
TrialObjectiveCClass.h
#import <Foundation/Foundation.h>
#interface TrialObjectiveCClass : NSObject
#property (nonatomic, strong) NSString *first;
#property (nonatomic, strong) NSString *second;
#property (nonatomic, assign) NSNumber *third;
- (instancetype)initWithArray:(NSArray *)data;
#end
TrialObjectiveCClass.m
#import "TrialObjectiveCClass.h"
#implementation TrialObjectiveCClass
- (instancetype)initWithArray:(NSArray *)data{
self.first = data[0];
self.second = data[1];
self.third = data[2];
return self;
}
#end
Now here comes the problem.When I use these two classes in my ViewController.m which has following code in it:
#import "ViewController.h"
#import "TrialObjectiveCClass.h"
#import "Codemaster-Swift.h" //Automatically created header to use Swift code in Objective-C
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *trialArray = #[#"FirstString", #"SecondString", #99];
//First the Swift part
TrialSwiftClass *obj = [[TrialSwiftClass alloc] initWithData:trialArray];
NSLog(#"%#", obj.first);
NSLog(#"%#", obj.second);
NSLog(#"%#", obj.third);
//Now the Objective-C part
TrialObjectiveCClass *obj2 = [[TrialObjectiveCClass alloc] initWithArray:trialArray];
NSLog(#"%#", obj2.first);
NSLog(#"%#", obj2.second);
NSLog(#"%#", obj2.third);
}
If I put a breakpoint in the last NSLog in ViewController.m, here is what i see in debug area:
My logs are showing the right value of my object's properties.
Why can't i see the hierarchy of my Swift class but can see my Objective-C class? How to solve this problem?
The variables in your Swift class get exposed to ObjC as properties. At present the Locals view does not show the properties of an object, only its ivars. This is true regardless of whether the class comes from ObjC or Swift. That's why the swift variables don't show up in the locals view.
You can work around this by using the expression command in the lldb console to view the object's properties. For instance, stopped in your example, you can do:
(lldb) expr obj.first
(__NSCFConstantString *) $0 = 0x00000001002d1498 #"FirstString"
etc.
You can't see the hierarchy of your Swift class in that point. When you bridge your swift class to an obj-c file something happening and the debugger can't see anymore your properties. This is a bug or something else, I can't figure out yet. But if you would like to see your hierarchy you can see that in your Swift file.
Another solution if you create a swift project and initialize there the objective-c classes. So if you can create on the opposite way, the properties and hierarchy will appear in the debug field.
This is not an explanation for that, why your solution not working but I hope this will help you a little bit.

Calling a Method on an Objective-C Delegate from Swift

I'm writing some Swift classes that build upon functionality in our objective-c app. I have a objective-c class with a delegate that conforms to a protocol. I'm trying to call a method on that delegate from inside of a Swift class I'm simplified it down to this.
FredTestProtocol.h:
#protocol FredTestProtocol
- (void) dumbMethod;
#end
FredTestClass.h:
#import <Foundation/Foundation.h>
#import "FredTestProtocol.h"
#interface FredTestClass : NSObject <FredTestProtocol>
#property (nonatomic, weak) NSObject <FredTestProtocol> *delegate;
#end
FredTestClass.m:
#import "FredTestClass.h"
#implementation FredTestClass
- (void) dumbMethod
{
NSLog(#"Boy, this is a dumb method");
}
#end
FredSwiftClass.swift
import Foundation
class FredSwiftClass {
func test()
{
let ocObject = FredTestClass()
ocObject.delegate.dumbMethod() // Error occurs here.
}
}
The indicated line produces the error "'NSObject' does not have a method named 'dumbMethod'" I've tried a lot of ways to eliminate the error, to no avail. I'm sure I'm missing something really fundamental. Can someone tell me how I should go about calling the delegate method from Swift?
When Swift examines the property delegate it simply sees that is is an NSObject and the fact that you have noted that it implements a protocol is ignored. I can't find any specific documentation as to why this is the case.
You can address this in a couple of ways.
First, you can redefine your delegate property to use class anonymity, then Swift will just see it as some object that implements the protocol -
FredTestClass.h
#import <Foundation/Foundation.h>
#import "FredTestProtocol.h"
#interface FredTestClass : NSObject <FredTestProtocol>
#property id<FredTestProtocol> delegate;
#end
Then your Swift code will compile as written.
or you can leave your delegate definition as is and tell Swift that you want to access the delegate as an instance of an object that implements the protocol via downcast -
FredTestSwift.swift
import Foundation
class FredSwiftClass {
func test()
{
let ocObject = FredTestClass()
let theDelegate=ocObject.delegate as! FredTestProtocol
theDelegate.dumbMethod()
}
}
Pretty sure I've got it.
func test()
{
let ocObject = FredTestClass()
if let myDelegate = ocObject.delegate as? FredTestProtocol
{
myDelegate.dumbMethod()
}
}

error with property in objective-c in iOS

I have started new iOS project and have added only one property in ViewControler header file. but it gives me error:
expected specifier-qualifier-list before 'property'
here is the code:
#import <UIKit/UIKit.h>
#interface QuoteGenViewController : UIViewController {
#property (retain) NSArray *myQuotes;
}
#end
Here the general structure of a class interface
#interface Class : Superclass
{
// Instance variable declarations.
// Theirs definition could be omitted since you can declare them
// in the implementation block or synthesize them using declared properties.
}
// Method and property declarations.
#end
Since a property provides a simple way to declare and implement an object’s accessor methods (getter/setter), you need to put them in the Method and property declarations section.
I really suggest to read ocDefiningClasses doc for this.
Hope that helps.
Your code should look like this:
#import <UIKit/UIKit.h>
#interface QuoteGenViewController : UIViewController {
}
#property (retain) NSArray *myQuotes;
#end

Resources