skip to Main Content

Working on a legacy hybrid iOS project. Created one new Swift util class in ConsentManager.swift, like below,

import Foundation

public class ConsentManager: NSObject {
    @objc static let sharedInstance = ConsentManager()
    @objc private override init() {}
    @objc public func isDataPermissionConsentRequired() -> Bool
    {
        …
        return value; // based on logic
    }
}

Called the method from another objc class, ConsentChecker.m like,

@interface ConsentChecker ()
{
}
@end

@implementation ConsentChecker
-(void)checkConsent {
    
    // GETTING ERROR IN THE FOLLOWING LINE
    if (ConsentManager.sharedInstance.isDataPermissionConsentRequired())  { 
        …
    }
}
@end

Getting compiler error:

Called object type ‘BOOL’ (aka ‘bool’) is not a function or function pointer

Why and how to resolve it?

3

Answers


  1. The Obj-C syntax for executing methods is different from Swift’s dot syntax.

    This is the correct syntax:

    if ([ConsentManager.sharedInstance isDataPermissionConsentRequired])  {
    
    Login or Signup to reply.
  2. The reason you’re hitting this is that methods in Objective-C which take no arguments may be called implicitly using dot syntax similar to Swift’s, but not exactly like it. A method declared like

    // Inside of SomeClass
    - (BOOL)someMethod { /* return something */ }
    

    can be called either as

    SomeClass *instance = ...
    
    // Traditional Obj-C syntax:
    BOOL value = [instance someMethod];
    

    or

    // Dot syntax accessor:
    BOOL value = instance.someMethod;
    

    Note that the dot syntax version does not use parentheses to denote the call. When you add parentheses like you would in Swift, Obj-C determines that you are trying to call the returned value from the method as if it were a function:

    instance.someMethod();
    
    // equivalent to:
    
    BOOL value = [instance someMethod];
    value(); // <- Called object type 'BOOL' (aka 'bool') is not a function or function pointer
    

    You cannot call a BOOL like you can a function, hence the error.

    @Dávid offers the more traditional Obj-C syntax for calling this method, but alternatively, you can simply drop the parentheses from your call:

    if (ConsentManager.sharedInstance.isDataPermissionConsentRequired) {
    

    Objective-C-ism note:

    Dot syntax is most idiomatically used for method calls which appear like properties (e.g. boolean accessors like your isDataPermissionConsentRequired), even if the method might need to do a little bit of work to return that value (think: computed properties in Swift).

    For methods which perform an action, or which return a value but might require a significant amount of work, traditional method call syntax is typically preferred:

    // Prefer:
    [instance doTheThing];
    NSInteger result = [instance performSomeExpensiveCalculation];
    
    // over:
    instance.doTheThing;
    NSInteger result = instance.performSomeExpensiveCalculation;
    
    Login or Signup to reply.
  3. If u want to call swift function on obj-c class you use to obj-c syntax

    Correct Syntax is:

    if ([ConsentManager.sharedInstance isDataPermissionConsentRequired])  {
        // Write logic here
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search