我的Swift类出现在自动生成的<target> -Swift.h文件中,但不出现在其函数定义</target>中

时间:2014-12-10 05:54:29

标签: swift ios8

摘要:

自动生成的-Swift.h文件中包含我的Swift类引用,但没有“func”定义。 (注意:我有其他Swift类,其中包含自动生成文件中列出的func,工作得很好。)请你告诉我为什么在Swift.h文件中没有正确表征其他类/ func

更多细节:

这是我自动生成的.swift.h文件。请注意,我的第一个名为“Shape”的类一切正常,你可以看到为该类自动生成了函数defs。但是,使用我的其他swift类“hiloBetFuncs”,没有自动生成任何函数定义(只有一个func)。所以在我的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)

    }

}

2 个答案:

答案 0 :(得分:2)

具有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
    // ...
}

答案 1 :(得分:1)

您可能希望将类和方法定义都声明为public。

public class MyClass {
    public func myMethod() -> Void {
        //your method implementation
    }
}

默认情况下,Swift将访问修饰符设置为内部。 希望这有帮助。