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
The Obj-C syntax for executing methods is different from Swift’s dot syntax.
This is the correct syntax:
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
can be called either as
or
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:
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:
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:
If u want to call swift function on obj-c class you use to obj-c syntax
Correct Syntax is: