【问题标题】:My Swift class appears in the auto-generated <target>-Swift.h file but not its function definitions我的 Swift 类出现在自动生成的 <target>-Swift.h 文件中,但没有出现在它的函数定义中
【发布时间】:2014-12-10 05:54:29
【问题描述】:

摘要:

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

更多细节:

这是我自动生成的 .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)

    }

}

【问题讨论】:

  • nm_bet 是如何定义的?
  • nm_bet 是一个 objc 类,它被定义为 NSObject 的子类。这是一个非常简单的类,只有几个变量,没有方法。

标签: swift ios8


【解决方案1】:

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

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

出现在桥接头&lt;module&gt;-Swift.h as

@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
    // ...
}

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      相关资源
      最近更新 更多