相关疑难解决方法(0)

如何在目标c中使用泛型类的swift类

我有以下带有NumericType T的swift类.

@objc class MathStatistics<T: NumericType> : NSObject {
        var numbers = [T]()

        func sum() -> T? {
            if numbers.count == 0 {
                return nil
            }

            var sum = T(0)

            for value in numbers {
                sum = sum + value
            }
            return sum
        }
    }
Run Code Online (Sandbox Code Playgroud)

在swift中,初始化类对象如下:

let statistics = MathStatistics<Double>()
Run Code Online (Sandbox Code Playgroud)

如何在Objective C中初始化相同的对象?以下行未设置数字类型T.

MathStatistics *stats = [[MathStatistics alloc] init];
Run Code Online (Sandbox Code Playgroud)

generics objective-c swift

8
推荐指数
1
解决办法
3518
查看次数

标签 统计

generics ×1

objective-c ×1

swift ×1