2017-01-11 9 views
1

私たちはすばやいライブラリを持っており、Objective-Cベースのプロジェクトで使用しています。目的のCヘッダーファイルで素早く弱いです

弱いfileprivateするvar _axisValueFormatter:IAxisValueFormatter

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

ご覧のとおり、プライベートvarがあり、弱いように宣言:迅速で

は、このようなクラスがありますか?

valueFormatterのゲッターとセッターがあります。

しかし、等valueFormatterためのObjective-Cヘッダ生成する最新のXcode 8.2.1:

@property(非アトミック、強い)ID _Nullable valueFormatterと、

ご覧のとおり、強く、ヘッダファイルにも_axisValueFormatterが見つかります。

これは予想されますか?どうすればそれを正しくすることができますか?前もって感謝します。

答えて

0

あなたはvalueFormatterが強い特性として定義されています。 _axisValueFormatterfileprivateと定義されているため、外部からはアクセスできません。期待どおりのもの。

  1. _axisValueFormatterは弱いままになります:結論に

  2. valueFormatterのストレージ修飾子は、内部実装が弱いプライベートプロパティ_axisValueFormatterに依存しているため、無関係です。

valueFormatterは強力ですが、いつでも0になることができるweakのプロパティで戻ってきます。これは副作用のあるプロパティです。私はそのことを避けることをお勧めします。valueFormatterweakとマークして、クラスを使用する開発者の期待を調整してください。

弱く、強い参照に関するドキュメントを見てみましょう:

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-ID48

+0

その後、 '_axisValueFormatter'は迅速に弱く、どのようにObjective-Cのでは? openプロパティのaxisValueFormatterは強いようですが、基になる '_axisValueFormatter'は弱いです。客観的に弱い性質のように働くか? – Wingzero

+0

@Wingzero私の答えが – Andy

+0

に更新されました。値のプロパティvalueFormatterにweakを指定すると、ちょっと混乱します。私は_axisValueFormatterを弱く宣言すれば、何が起こるのですか? – Wingzero

関連する問題