Ravi
Ravi

Reputation: 4019

How to call a method on .mm file from a objective c class

I am working on an iphone app. I need to call a method on a .mm file. Here is simplified version of the problem:

ViewHelper.h

- (void)testMtd;

ViewHelper.mm (notice this is .mm)

- (void)testMtd{
   NSLog(@"Call reached mm");
}

SomeViewController.m (import to ViewHelper.h omitted for clarity)

- (void)someCallerMtd{
   NSLog(@"before");
   [viewHelper testMtd]; //call does not work
   NSLog(@"after");
}

I see "before" and "after" in the log, but "Call reached mm" never gets printed. Are there special rules to call obj c methods in a .mm file? What am I missing here?

Upvotes: 1

Views: 2697

Answers (2)

HelmiB
HelmiB

Reputation: 12333

First, it has nothing to do with .mm file, it is still objective-c clss. Second, Your mistake is not allocating ViewHelper.

The solutions is either alloc your ViewHelper or make (void)testMtd publicly. depend on what your need.

either change your SomeViewController.m:

- (void)someCallerMtd{
   NSLog(@"before");
   viewHelper = [[ViewHelper alloc] init];
   [viewHelper testMtd]; 
   [viewHelper release];
   NSLog(@"after");
}

or change your ViewHelper :

//ViewHelper.h
+ (void)testMtd;

//ViewHelper.mm
+ (void)testMtd{
   NSLog(@"Call reached mm");
}

- (void)someCallerMtd{
       NSLog(@"before");
       [ViewHelper testMtd]; //remember to use ViewHelper class. not viewhelper.
       NSLog(@"after");
    }

Upvotes: 2

Caleb
Caleb

Reputation: 125037

The most likely reasons that your -testMtd method never gets called is that viewHelper is nil. Make sure that it points to a valid instance of the ViewHelper class. It's legal in Objective-C to send a message to a nil pointer, but no method will be called in that case.

Upvotes: 0

Related Questions