Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从Objective-C调用Swift扩展函数的语法_Ios_Objective C_Swift_Extension Methods - Fatal编程技术网

Ios 从Objective-C调用Swift扩展函数的语法

Ios 从Objective-C调用Swift扩展函数的语法,ios,objective-c,swift,extension-methods,Ios,Objective C,Swift,Extension Methods,我有一个既有objective-c又有swift的项目。所有的东西都连接好了,所以我通常可以毫无疑问地调用类的扩展。但是,在本例中,我必须将一个参数传递给扩展,并且在语法上遇到了麻烦 这是Swift 3分机 extension Double { /// Rounds the double to decimal places value func rounded(toPlaces places:Int) -> Double { let divisor = po

我有一个既有objective-c又有swift的项目。所有的东西都连接好了,所以我通常可以毫无疑问地调用类的扩展。但是,在本例中,我必须将一个参数传递给扩展,并且在语法上遇到了麻烦

这是Swift 3分机

extension Double {
    /// Rounds the double to decimal places value
    func rounded(toPlaces places:Int) -> Double {
        let divisor = pow(10.0, Double(places))
        return (self * divisor).rounded() / divisor
    }
}
从斯威夫特你可以用

let x = Double(0.123456789).rounded(toPlaces: 4)
以下内容在Objective-C中不起作用:我已经使用过它,但无法获得正确的语法:

double test = 0.123456789;
double roundedtot = test.roundedToPlaces:2;
具体错误是

'成员引用基类型'double'不是结构或联合'


我认为这是一个c错误,但由于您可以在swift中调用该函数,因此似乎应该有一种在Objc-c中调用它的方法,您需要在扩展定义中添加
@Objc
,以便从Objective-c调用它


另外,需要注意的是,从Objective-C只能访问类的扩展(不适用于结构或枚举)。

您尝试执行的操作是不可能的
double
是一种C基元类型。它不能附加方法(这将违反C)。