小编Hen*_*sta的帖子

Swift中的Getter和Setter - 使用WillSet和DidSet是否有意义?

我正在研究我们应该为我们的属性使用Get和Set的原因.

我注意到它的3个主要原因

  1. 如果您想在实际设置属性之前执行/检查某些内容
  2. 当你想拥有一个只能从中获取的属性时(可能出于安全目的,我猜?),或者给它不同的访问级别.
  3. 在使用替代表示公开属性的同时隐藏属性的内部表示.(这对我来说没有多大意义,因为我可以使用Set函数在错误的地方访问它)

下面的代码是一个如何在Swift中为属性实现Get和Set的示例,利用我提到的这3点:

class Test
{
    private var _testSet:String!
    private var _testGetOnly:String
    var testSet:String{
        get{
            return _testSet
        }
        set{
            _testSet = newValue + "you forgot this string"
        }
    }
    var testGetOnly:String!{
        get{
            return _testGetOnly
        }
    }

    init(testSet:String, testGetOnly:String)
    {
        _testSet = testSet
        _testGetOnly = testGetOnly
    }
}
Run Code Online (Sandbox Code Playgroud)

但是下面的另一个例子也利用了提到的那些点,但是不使用另一个计算属性来返回私有属性值,我只使用了willSet和didSet观察者

class Test
{
    var testGet:String {
        willSet{
            fatalError("Operation not allowed")
        }
    }
    var testWillSet:String!{
        didSet{
            self.testWillSet = self.testWillSet + "you forgot this string"
        }
    }
    init(testGet:String, testWillSet:String)
    {
        self.testGet …
Run Code Online (Sandbox Code Playgroud)

oop get set swift computed-properties

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

Android Fullscreen Fragment对话框错误

我创建了一个"全屏"DialogFragment,其中我添加了一个透明黑色背景.最终结果可以在下面的截图中看到:

在此输入图像描述

这是我如何做到的:

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }



    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // if >= LOLLIPOP, then I color the the statusbar
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            dialog.getWindow().setStatusBarColor(SOME_COLOR_HERE);
        }

    }

@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogFullScreen);
    }
Run Code Online (Sandbox Code Playgroud)

这是我在setStyle()上设置的主题:

<style name="DialogFullScreen">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowContentOverlay">@null</item>
</style>
Run Code Online (Sandbox Code Playgroud)

这是我为片段扩充的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-fragments nexus-4

5
推荐指数
1
解决办法
560
查看次数