swiftobjective-c++

Xcode forcing me to use cameCase for Objc++ function names


I wanted to test out forking in a macOS swift command line app. Since the linux fork method is C++, there needs to be Objc++ bridge header.

Here's my sample app:

In main.swift file,

import Foundation

var gReturnValue = main ()
print("Return value = \(gReturnValue)\n")


func main () -> Int {

    var pid:Int
    
    pid = ForkNewSession.StartFork()
    
    if (pid < 0) {
        
        NSLog("Fork failed!!")
        return -1
    } else if (pid != 0) {
        
        NSLog("Parent Process!! Exiting!")
        return 1
    }
    
    NSLog("Child Process!!")
        
    return 0;
}

ForkNewSession is an ObjC++ class registered in the bridge header (thus, available in swift).

@interface ForkNewSession : NSObject

+ (NSInteger) StartFork;

@end

But Xcode build fails with this error:

enter image description here

I'm new to swift and app development with swift. I don't know why this is happening? Previously, I had used ObjC++ and C++ and there was no trouble there.

Does swift not allow functions which don't use camelCase? Or is there something else?


Solution

  • Does swift not allow functions which don't use camelCase? Or is there something else?

    I don't think it's reliably known (outside of development team responsible for that) why the interoperability generator was implemented this way. UpperCamelCase method names in Swift are neither reserved nor prohibited by Swift language, however such approach is quite inconsistent among the Swift developers. It might be related to some refactoring of Foundation or other built-in frameworks method names, but it's just an assumption.

    If you really need to keep the original name, you can explicitly give the name to the method with use of NS_SWIFT_NAME macro:

    @interface ForkNewSession : NSObject
    
    + (NSInteger)StartFork NS_SWIFT_NAME(StartFork());
    
    @end
    

    In case the method has parameters, just follow standard Swift-selector grammar to describe them:

    + (NSInteger)StartForkWithInt:(NSInteger)intValue string:(NSString *_Nonnull)stringValue NS_SWIFT_NAME(StartForkWithInt(_:string:));