Home > Net >  how to share instance variable between class methods in objective c
how to share instance variable between class methods in objective c

Time:02-11

So I was building a flutter native plugin. I have two method (in xyzPlugin.m) which looks like this

  (void)registerWithRegistrar:(nonnull NSObject<FlutterPluginRegistrar> *)registrar {

and

- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {

Now, I want to share instance variable between both of them.

Suppose. I initialize meetingView like this inside registerWithRegistrar

 MeetingView *meetingView = [MeetingView new];

How can I use the same in handleMethodCall?

What have I tried?

@implementation xyzPlugin {
    MeetingView *_view;
}

and then in

- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
    _view = [MeetingView new];

but this gives following error

Instance variable '_view' accessed in class method

CodePudding user response:

Maybe you need a global variable, so you can claim your object with a static keyword like:

static MeetingView *_meetingView = ...

then you can access this object in between class methods in Objective-C.

  • Related