【问题标题】:iOS Objective C Override Property SetteriOS Objective C 覆盖属性设置器
【发布时间】:2017-08-29 20:08:15
【问题描述】:

在我的课堂上@property BOOL clearCookies;

我想设置它,以便在设置项目时我可以做一些额外的事情。

在我的代码中,我可以做类似classInstance.clearCookies = YES;

然后在 setter 方法中类似这个来自 android 的 java 示例:

public void setClearCookies(boolean clearCookies) {
        _instance.clearCookies = clearCookies;
        _instance.doOtherThings();
    }

在 Objective C 中实现这一目标的最佳方法是什么?基本上我想设置布尔属性,然后还做一些其他的事情(实际上是从我的UIWebView 清除 cookie)

【问题讨论】:

    标签: android ios objective-c


    【解决方案1】:
    -(void)setClearCookies:(BOOL)clearCookies
    {
        @synchronized(self)
        {
           _clearCookies = clearCookies;
           [self doOtherThings];
       } 
    }
    
    - (BOOL)clearCookies
    {
       @synchronized (self)
       {
           return _clearCookies;
       }
    }
    

    【讨论】:

    • 看起来很完美!感谢您的快速响应!
    • 看起来它不喜欢合成吸气剂:writable atomic property cannot pair a synthesized getter with a user defined setter。你介意提供一个如何为此编写吸气剂的例子吗? :)
    • 最好将属性设为非原子而不是将其保留为原子但以非原子方式实现 getter/setter。
    • @TomHammond 我更新了我的答案。此代码将保持 setter 和 getter 原子
    【解决方案2】:

    你可以这样做:

    -(void)setClearCookies:(BOOL)clearCookies {
            _clearCookies = clearCookies;
           [self doOtherThings];
        }
    

    【讨论】:

    • 那不是java吗? :)
    • @TomHammond haha​​ 更新了语法 - java/swift 混淆 - 终于更正了:D
    【解决方案3】:

    另一种避免线程冲突的方法(替代@synchronized())是使用Grand Central Dispatch(在大多数情况下是faster而不是前者):

    @property (assign, nonatomic) BOOL clearCookies;
    
    // Later in code
    static dispatch_queue_t clearCookiesQueue;
    
    - (void)setClearCookies:(BOOL)clearCookies {
        [self initializeClearCookiesQueue];
    
        dispatch_async(clearCookiesQueue, ^{ // Note, this is "async"
            _clearCookies = clearCookies;
        });
    }
    
    
    - (BOOL)clearCookies {
        [self initializeClearCookiesQueue];
    
        __block BOOL clearCookies;
        dispatch_sync(clearCookiesQueue, ^{ // Note, this is "sync"
            clearCookies = _clearCookies;
        });
    
        // As "sync" waits until the operation is finished, you can define
        // your other operations here safely.
        [self doOtherStuff];
    
        return clearCookies;
    }
    
    
    /**
        Checks if the queue for the clearCookies property is initialized and,
        if it's not, initializes it.
    */
    - (void)initializeClearCookiesQueue {
        if ( ! clearCookiesQueue ) {
            // We'll use a serial queue to avoid conflicts reading/writing the ivar
            clearCookiesQueue = dispatch_queue_create("com.yourapp.yourviewcontroller.someUniqueIdentifier", DISPATCH_QUEUE_SERIAL); // Note, this is a C string, not NSString
        }
    }
    

    如果你不关心线程,或者你不需要它,只需覆盖你的 setter 就足够了:

    - (void)setClearCookies:(BOOL)clearCookies {
        _clearCookies = clearCookies;
        [self doOtherStuff];
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 2014-03-11
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多