I want to convert bellow code to objective-c code:
import Foundation
#objcMembers
final class RNBridgeInstanceHolder : NSObject {
static let sharedInstance = RNBridgeInstanceHolder()
var bridge: RCTBridge?
var rctRootView: RCTRootView?
}
I tried like this header file:
#import <Foundation/Foundation.h>
#interface RNBridgeInstanceHolder: NSObject
#property (nonatomic, strong) RCTBridge *bridge;
#property (nonatomic, strong) RCTRootView *rootView;
#end
main file below
#import <React/RCTBridge.h>
#import "RNBridgeInstanceHolder.h"
#import <React/RCTRootView.h>
#implementation RNBridgeInstanceHolder : NSObject
RCTBridge *bridge;
RCTRootView *rootView;
#end
Anyone please help me to convert this code!
Related
My project mixes Objective-C and Swift
I need to implement flutter eventChannel in Objective-C class
how should I do it?
I try to add a callback in Objective-C and return the result to the eventChannel in AppDelegate.swift. But it seems something is wrong... I am not familiar with Objectiv-C, who can help me
myController.h
typedef void(^NfcBlock)(bool isSuccess); // the nfc and ble service is from a static library.
typedef void(^BleBlock)(bool isSuccess); // i need return connect result to flutter view.
#interface MyController : NSObject<NfcDelegate, BleServiceDelegate,CBCentralManagerDelegate>
#property (nonatomic,strong) BlePassService *blePassService;
#property (nonatomic,strong) NfcPassService *nfcService;
#property (nonatomic,strong) MobilePass *mobilePass;
#property (nonatomic,strong) NfcBlock nfcBlock;
#property (nonatomic,strong) BleBlock bleBlock;
AppDelegate.swift
func nfcCallback(isSuccess:Bool){
// add event sink
}
case CallMethod.initState:
if passKit == nil { return }
passKit.initial()
passKit.setNfcBlock(nfcCallback)
result(String("success"))
Instead of methodChannel I should use FlutterBasicMessageChannel . It can communicate bidirectionally
#import <Flutter/Flutter.h>
#import <Foundation/Foundation.h>
API_AVAILABLE(ios(14.0))
#interface PassKit : NSObject<NfcDelegate, BleServiceDelegate,CBCentralManagerDelegate>
#property (nonatomic,strong) FlutterBasicMessageChannel *msgChannel;
.m File
- (void)sendData:(NSString *)data{
if(_msgChannel==nil) {
[_msgChannel sendMessage:[NSString stringWithFormat:#"error"]];
return;
};
[_msgChannel sendMessage:[NSString stringWithFormat:#"%#",data]];
}
AppDelegate.swift
let twoWayMethodChannel = FlutterBasicMessageChannel(name: ChannelName.twoWayName, binaryMessenger: controller.binaryMessenger);
passKit.setChannel(twoWayMethodChannel)
I have read question after question about people getting the same error as me, but I simply do not understand them, so before you go searching for duplicate questions, maybe someone can explain to me what I am doing wrong with this subclassing deal.
I have a subclass of UIImageView called swapView that I want to subclass to override the method -(void)count for special cases. I went to subclass this as I have any pre-existing UIKit class, but when I tried to build and run the project, I get this error:
Attempting to use the forward class 'swapView' as superclass of 'coinView'
I have tried putting both the #import statement of swapView and #class swapView in coinView.h and I've tried putting the import statement in coinView.m, but it refuses to build because of this continued error. If I move the import statement into the .m file, all references to the superclass's methods and properties, such as #property (nonatomic) int max; cause errors as well.
What am I doing wrong?
swapView.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
#class ViewController;
#interface swapView : UIImageView
{
NSTimer* tmr;
}
#property (nonatomic) int current;
#property (nonatomic) int max;
#property (nonatomic, retain) UIImage* firstImage;
#property (nonatomic, retain) UIImage* secondImage;
#property (nonatomic) BOOL smallMax;
#property (nonatomic, retain) ViewController* pvc;
- (BOOL)testCollision:(CGPoint)point;
- (float)randomFloatBetween:(float)smallNumber bigNumber:(float)bigNumber;
#end
coinView.h
#import "swapView.h"
#class swapView;
#interface coinView : swapView
- (void)count;
- (void)move;
#end
For inheritance, the superclass MUST be inherited.
coinView.h
#import "swapView.h"
#interface coinView : swapView
- (void)count;
- (void)move;
#end
You're both forward declaring and importing ViewController.h in your swapView, which may cause compiler to complain.
swapView.h
#import <UIKit/UIKit.h>
#class ViewController
#interface swapView : UIImageView
.
.
.
#end
Here is my .h file:
#import <Foundation/Foundation.h>
#interface ShareOfflineItem : NSObject
#property (strong, nonatomic) id shareService;
#property (strong, nonatomic) NSString *title, *url;
#end
#interface ShareOfflineQueue : NSObject
+(ShareOfflineQueue *)sharedQueue;
-(void)addItemToQueue:(ShareOfflineItem *) item;
-(void)flushQueue;
-(void)saveQueueToDisk;
#end
As you can see nothing more special. But when I import my .h file and try to instantiate ShareOfflineItem I get a linking error:
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_ShareOfflineItem", referenced from:
Any ideas?
Use
#class ShareOfflineItem;
#class ShareOfflineQueue;
after the import. This might help.
I have the following UIViewController class:
FILE: ResultsDataViewController.h
#interface ResultsDataViewController : UIViewController
#property (nonatomic, strong) NSString* name;
#end
FILE : ResultsDataViewController.m
#import "ResultsDataViewController.h"
#interface ResultsDataViewController ()
#end
#implementation ResultsDataViewController
#synthesize data;
Then I have my UIView with class name Plot.h/m
Plot.h:
#interface PlotGraph : UIView <CPTBarPlotDataSource, CPTBarPlotDelegate>
#property (nonatomic, strong) NSString* receivedName;
#end
Plot.m:
#import "Plot.h"
#interface Plot()
#end
#implementation PlotGraph
#synthesize receivedName;
...
=====
Question:
HOW do I pass the value of name from my UIViewController and assign it to the variable receivedName in my UIView?
The UIView Plot is a view in my UIViewController DataViewController
(Note: #synthesize data; in your question is probably a typo. I'll assume you meant #synthesize name;. Also, #interface Plot() should probably be #interface PlotGraph().)
First, you need to have a reference to a PlotGraph object either as something you create in code or something that you load into an IBOutlet variable from your xib file or storyboard. Once you have that, either where you create it or in viewDidLoad:, it's simply something like:
plotView.receivedName = self.name;
MPPopoverControllerDelegate.h file
#import <Foundation/Foundation.h>
#class MPPopoverController;
#protocol MPPopoverControllerDelegate <NSObject>
#optional
- (void)popoverControllerDidDismissPopover:(MPPopoverController *)popoverController;
#end
MPPopoverController.h file
#import <UIKit/UIKit.h>
#protocol MPPopoverControllerDelegate;
#interface MPPopoverController : UIViewController <MPPopoverControllerDelegate>
#property (nonatomic, assign) id<MPPopoverControllerDelegate> delegate;
#end
MPPopoverController.m file
#import "MPPopoverController.h"
#import "MPPopoverControllerDelegate.h"
#implementation MPPopoverController
#end
#property (nonatomic, assign) id<MPPopoverControllerDelegate> delegate; : this line has warning
Cannot find protocol definition for 'MPPopoverControllerDelegate'
What is wrong? And how to fix this warning?
if replace '#protocol MPPopoverControllerDelegate'; with '#import "MPPopoverControllerDelegate.h', everything will be ok. But link - in Referring to Other Protocols you can see that apple says to use #protocol
Is there an absolute need for your protocol declaration to be in a different header file? Unless it's quite a large protocol definition (which yours isn't), I would suggest declaring it below your interface declaration.
MPPopoverController.h
#import <UIKit/UIKit.h>
#protocol MPPopoverControllerDelegate;
#interface MPPopoverController : UIViewController
#property (nonatomic, assign) id<MPPopoverControllerDelegate> delegate;
#end
#protocol MPPopoverControllerDelegate <NSObject>
#optional
- (void)popoverControllerDidDismissPopover:(MPPopoverController *)popoverController;
#end
Compiler read your .m file, and load .h files when necessary. So it loads MPPopoverController.h first and when it read it delegate protocol is still undeclared. You could easily fix this warning just by swapping include lines. To let compiler read delegate .h file first.
#import "MPPopoverControllerDelegate.h"
#import "MPPopoverController.h"
#implementation MPPopoverController
#end
Are you sure you're ever including MPPopoverControllerDelegate.h somewhere?
Import MPPopoverControllerDelegate.h in MPPopoverController.h and MPPopoverController.h should look like:
The code copy pasted from the question and edited was removed. The following code is copy pasted from xcode.
The MPPopoverControllerDelegate.h:
#class MPPopoverController;
#protocol MPPopoverControllerDelegate <NSObject>
#optional
- (void)popoverControllerDidDismissPopover:(MPPopoverController *)popoverController;
#end
The MPPopoverController.h
#protocol MPPopoverControllerDelegate;
#interface MPPopoverController : UIViewController{
id<MPPopoverControllerDelegate> delegate;
}
#property (nonatomic, assign) id<MPPopoverControllerDelegate> delegate;
#end
The MPPopoverController.m
#implementation MPPopoverController
#synthesize delegate;
//rest of view controller class.
The problem is that in your MPPopoverController interface you specify <MPPopoverControllerDelegate>. This means that the class implements this protocol! Is wrong because the class is the owner of the protocol. So your logic is wrong in some point.