Skip to main content
 首页 » 操作系统

swift之我的 Swift 类出现在自动生成的 -Swift.h 文件中,但不是它的函数定义

2024年01月01日36mayingbao

摘要:

自动生成的 -Swift.h 文件中有我的 Swift 类引用,但没有任何“func”定义。 (注意:我还有其他带有函数的 Swift 类,这些类在 auto-gen 文件中列出并且工作得很好。)请你告诉我为什么这个其他类/函数没有在 Swift.h 文件中正确描述

更多细节:

这是我自动生成的 .swift.h 文件。请注意,我的第一个名为“Shape”的类一切正常,您可以看到已为该类自动生成函数 defs。然而,对于我的另一个快速类“hiloBetFuncs”,没有一个函数定义(只有一个函数)是自动生成的。因此,在我的 objective-C 文件中,当我尝试在该类的实例上调用该函数时,当我在 Xcode 中键入时,无法识别该函数。

为什么 Swift 在 -Swift.h 中生成了类 ref 但省略了 func 定义。我也包括了下面的乐趣。

#pragma clang diagnostic ignored "-Wproperty-attribute-mismatch" 
#pragma clang diagnostic ignored "-Wduplicate-method-arg" 
@class nm_skin; 
@class nm_theFeature; 
 
SWIFT_CLASS("_TtC9Golf_Whiz5Shape") 
@interface Shape : NSObject 
- (NSString *)testFunction; 
- (void)skinDetails:(nm_skin *)skinny feature:(nm_theFeature *)feature; 
- (instancetype)init OBJC_DESIGNATED_INITIALIZER; 
@end 
 
 
SWIFT_CLASS("_TtC9Golf_Whiz12hiloBetFuncs") 
@interface hiloBetFuncs : NSObject 
- (instancetype)init OBJC_DESIGNATED_INITIALIZER; 
@end 
 
#pragma clang diagnostic pop 

这是 hiloBetFuncs 类(和函数)本身。

import Foundation 
 
class hiloBetFuncs : NSObject { 
 
 
    func hilo_updateAllBetsforHole ( var hole : Int, result : Int, arrayOfBets : [nm_bet]) -> Int { 
 
        var returnCodeValue : Int = 0 
        let numberOfBetsInArray = arrayOfBets.count 
        var previousHoleBetResult : Int 
        var newHoleBetResult : Int 
 
        // we are only dealing with 9-hole bets so if on the back nine, go ahead and drop the hole value 
        // by 9 so that we can use the same holes 0-9 regardless.  (just makes things easier) 
        if hole > 8 
            {hole -= 9} 
 
        for bet in arrayOfBets { 
 
            if hole == 0 { 
 
                // hole 0 is a bit of a special case, just cause you are not building off the bet 
                // status from the previous hole.  This hole will be an autonomous 0, 1, or -1 
                // just set it to the 'result' paramater that was sent in. 
 
                bet.holeResults[hole] = result; 
                println("after one hole, the primary bet has been updated with a status of \(result)"); 
 
            } else { 
 
                //get pointer to the bet status as of the previous hole 
                previousHoleBetResult = bet.holeResults[hole - 1] as Int 
 
                //establish the bet status for the new hole, building from the previous hole's bet status 
                newHoleBetResult = previousHoleBetResult + result 
 
                // update the current hole's bet results with the newly calculated value 
                bet.holeResults[hole] = newHoleBetResult; 
 
            } 
 
            // we want to return the bet status from the last active bet - if 2 or -2 then calling function will know to start a new bet. 
            returnCodeValue = bet.holeResults[hole] as Int 
 
        } 
 
        println("ok, done the deed") 
 
        // since the home team could be 2 down, the bet status could be a negative number  
        // convert to abs before returning.  Just checking for bet status, not care about who is leading. 
        return abs(returnCodeValue) 
 
    } 
 
} 

请您参考如下方法:

带有 var 的函数参数不会暴露给 Objective-C。举个例子, Swift 类

class SwiftClass : NSObject { 
    func f1(x : Int) { } 
    func f2(var x : Int) { } 
} 

出现在桥接头中<module>-Swift.h作为

@interface SwiftClass : NSObject 
- (void)f1:(NSInteger)x; 
- (instancetype)init OBJC_DESIGNATED_INITIALIZER; 
@end 

如果f2明确标记为 Objective-C 导出然后编译失败:

@objc func f2(var x : Int) { } 
// error: method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C 

作为解决方法,您可以将参数声明为常量(这是默认值) 并制作一个变量副本:

func hilo_updateAllBetsforHole(theHole : Int, result : Int, arrayOfBets : [nm_bet]) -> Int { 
    var hole = theHole 
    // ... 
}