Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
swift中的薄弱环节在objective-C头文件中消失_Objective C_Swift - Fatal编程技术网

swift中的薄弱环节在objective-C头文件中消失

swift中的薄弱环节在objective-C头文件中消失,objective-c,swift,Objective C,Swift,所以我们有一个swift库,我们在基于Objective-C的项目中使用它 在swift中,有这样一个类: /// Base class for all axes @objc(ChartAxisBase) open class AxisBase: ComponentBase { public override init() { super.init() } /// Custom formatter that is used instead of

所以我们有一个swift库,我们在基于Objective-C的项目中使用它

在swift中,有这样一个类:

/// Base class for all axes
@objc(ChartAxisBase)
open class AxisBase: ComponentBase
{
    public override init()
    {
        super.init()
    }

    /// Custom formatter that is used instead of the auto-formatter if set
    weak fileprivate var _axisValueFormatter: IAxisValueFormatter?


    /// Sets the formatter to be used for formatting the axis labels.
    /// If no formatter is set, the chart will automatically determine a reasonable formatting (concerning decimals) for all the values that are drawn inside the chart.
    /// Use `nil` to use the formatter calculated by the chart.
    open var valueFormatter: IAxisValueFormatter?
    {
        get
        {
            if _axisValueFormatter == nil ||
                (_axisValueFormatter is DefaultAxisValueFormatter &&
                    (_axisValueFormatter as! DefaultAxisValueFormatter).hasAutoDecimals &&
                    (_axisValueFormatter as! DefaultAxisValueFormatter).decimals != decimals)
            {
                let df = DefaultAxisValueFormatter(decimals: decimals)
                _axisValueFormatter = df
                NSLog("found nil vf, assigning to \(_axisValueFormatter)")
            }
            NSLog("returning \(_axisValueFormatter)")
            return _axisValueFormatter
        }
        set
        {
            _axisValueFormatter = newValue //?? DefaultAxisValueFormatter(decimals: decimals)
        }
    }
    ...
}
如您所见,有一个声明为弱的私有变量:

弱fileprivate var\u axisValueFormatter:IAxisValueFormatter

对于
valueFormatter
,还有一个getter和setter

但是,最新的Xcode 8.2.1将为
valueFormatter
生成Objective-C头,如:

@属性(非原子,强)id _可为空的valueFormatter

正如您所看到的,它很强大,我也可以在头文件中找到任何
\u axisValueFormatter


这是预期的吗?我怎样才能使它正确?提前感谢。

您已将
valueFormatter
定义为强属性<代码>\u axisValueFormatter定义为
fileprivate
,因此无法从外部访问。一切如期而至

总结如下:

  • \u axisValueFormatter
    将保持较弱
  • valueFormatter
    的存储修饰符不相关,因为内部实现依赖于弱私有属性
    \u axisValueFormatter
  • 您声称
    valueFormatter
    是强的,但是您使用
    weak
    属性来支持它,该属性可以随时变为零。这是一个有副作用的属性。我建议避免这种情况,只需将
    valueFormatter
    标记为
    weak
    ,以调整将使用您的类的开发人员的期望值

    查看有关弱引用和强引用的文档:


    您已将
    值格式化程序
    定义为强属性<代码>\u axisValueFormatter定义为
    fileprivate
    ,因此无法从外部访问。一切如期而至

    总结如下:

  • \u axisValueFormatter
    将保持较弱
  • valueFormatter
    的存储修饰符不相关,因为内部实现依赖于弱私有属性
    \u axisValueFormatter
  • 您声称
    valueFormatter
    是强的,但是您使用
    weak
    属性来支持它,该属性可以随时变为零。这是一个有副作用的属性。我建议避免这种情况,只需将
    valueFormatter
    标记为
    weak
    ,以调整将使用您的类的开发人员的期望值

    查看有关弱引用和强引用的文档:


    那么,
    \u axisValueFormatter
    在swift中很弱,在objective-c中如何?虽然开放属性axisValueFormatter看起来很强大,但底层的
    \u axisValueFormatter
    很弱。它会像objective-c中的弱属性一样工作吗?我有点困惑,如果我能为属性valueFormatter指定弱属性,那么,关于私有变量,如果我声明_axisValueFormatter不是弱属性,会发生什么?@Wingzero它基本上会充当强属性。在您的示例中,valueFormatter将值存储在私有的_axisValueFormatter中,因此从技术上讲,您可以假设valueFormatter是_axisValueFormatter的代理。是否有充分的理由使用弱axisValueFormatter?因为弱者不会保留价值。不,我们决定坚持。我只是好奇,因为swift可以在存储属性和普通变量上声明弱,所以不同的组合有2x2种情况。那么,
    \u axisValueFormatter
    在swift中弱,在objective-c中如何?虽然开放属性axisValueFormatter看起来很强大,但底层的
    \u axisValueFormatter
    很弱。它会像objective-c中的弱属性一样工作吗?我有点困惑,如果我能为属性valueFormatter指定弱属性,那么,关于私有变量,如果我声明_axisValueFormatter不是弱属性,会发生什么?@Wingzero它基本上会充当强属性。在您的示例中,valueFormatter将值存储在私有的_axisValueFormatter中,因此从技术上讲,您可以假设valueFormatter是_axisValueFormatter的代理。是否有充分的理由使用弱axisValueFormatter?因为弱者不会保留价值。不,我们决定坚持。我只是好奇,因为swift可以在存储属性和正常变量上声明弱,所以对于不同的组合有2x2种情况。