V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
adow
V2EX  ›  iDev

关于 obj-c 中的单实例模式,静态分析器总给我报一个奇怪的提示

  •  
  •   adow ·
    adow · 2014-04-22 16:24:33 +08:00 · 4140 次点击
    这是一个创建于 3628 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近我在用Xcode 静态分析器时发现我所有单实例代码中的

    +(id)allocWithZone:(NSZone *)zone

    都有警告:

    Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected

    意思是allocWithZone返回的object必须是retain+1的,而单实例中的-(release)是直接返回self的,所以没有retain。

    我记得以前版本的Xcode中是没有这个提示的,我不知道是不是有别的实现方法了吗?

    我现在的写法类似下面这样:

    @implementation MySingleton

    static MySingleton *sSharedSingleton = nil;

    + (MySingleton *)sharedSingleton
    {
    if (sSharedSingleton == nil)
    {
    sSharedSingleton = [[super allocWithZone:NULL] init];
    }
    return sSharedSingleton;
    }

    + (id)allocWithZone:(NSZone *)zone
    {
    return [[self sharedSingleton] retain];
    }

    - (id)copyWithZone:(NSZone *)zone
    {
    return self;
    }

    - (id)retain
    {
    return self;
    }

    - (unsigned)retainCount
    {
    return NSUIntegerMax; //denotes an object that cannot be released
    }

    - (oneway void)release
    {
    //do nothing
    }

    - (id)autorelease
    {
    return self;
    }

    @end
    17 条回复    1970-01-01 08:00:00 +08:00
    chisj
        1
    chisj  
       2014-04-22 17:48:18 +08:00   ❤️ 1
    我们的单例模式是这么写的,是我最喜欢的写法。给LZ一个参考:

    static id shareInstance = nil;

    +(void)initialize {
    if (self == [EUHttpCache class]) {
    shareInstance = [[self alloc] init];
    }
    }

    +(EUHttpCache *)shareInstance {
    return shareInstance;
    }
    fly2never
        2
    fly2never  
       2014-04-22 17:56:58 +08:00   ❤️ 2
    用 dispatch_once吧,标准写法
    DropperY
        3
    DropperY  
       2014-04-22 18:00:11 +08:00   ❤️ 1
    是因为retaincount 写死了 NSUIntegerMax 的原因吧,预期的retaincount应该比原来大1
    alexrezit
        4
    alexrezit  
       2014-04-22 18:00:59 +08:00 via iPhone   ❤️ 1
    這是什麼鬼寫法...
    chchwy
        5
    chchwy  
       2014-04-22 19:04:47 +08:00   ❤️ 1
    樓主的寫法已經過時了。
    ultragtx
        6
    ultragtx  
       2014-04-22 19:28:53 +08:00 via iPad   ❤️ 1
    不能吐槽更多 这都是在哪学的奇葩写法
    satanwoo
        7
    satanwoo  
       2014-04-22 20:08:14 +08:00   ❤️ 1
    @ultragtx objetive-c设计模式里面好像就是这么写的。。。。
    adow
        8
    adow  
    OP
       2014-04-22 20:17:27 +08:00 via iPhone
    这样的写法我是从一本obj-c设计模式书上看来的,今天我google了一圈,现在的确都是用 dispatch_once 的写法了,我也改成了这样。不过这个问题在Xcode5.2下依旧。我想应该不是初始化时的问题吧。
    ylovesy
        9
    ylovesy  
       2014-04-22 20:28:39 +08:00   ❤️ 1
    上面的单例不好,应该这样:

    +(instancetype)sharedInstance
    {
    static dispatch_once_t once;
    static MyObject *sharedInstance = nil;

    dispatch_once(&once,^{
    sharedInstance = [[MyObject alloc] init];
    });

    return sharedInstance;
    }
    ultragtx
        10
    ultragtx  
       2014-04-22 20:39:08 +08:00
    这不只是单例的问题 你这里面除了sharedSingleton以外的所有method都不需要
    jesse0628
        11
    jesse0628  
       2014-04-22 20:48:18 +08:00   ❤️ 1
    @ultragtx 他想达到的目的是,把单例当做正常非单例使用,但不管你alloc还是retain、release,永远只存在一份真正的内存对象。
    satanwoo
        12
    satanwoo  
       2014-04-22 20:54:21 +08:00   ❤️ 1
    楼上正解。而且这样的写法还要考虑到单例中的继承问题。。
    ultragtx
        13
    ultragtx  
       2014-04-22 21:27:26 +08:00
    @jesse0628 按我理解这么写的动机是为了弥补语法层面不支持而加上的保护措施吧

    但这么做也就违反了内存管理的规则 而且在ARC下也没法这么写
    hengxin196
        14
    hengxin196  
       2014-04-22 23:13:24 +08:00   ❤️ 1
    `#import "__class__.h"

    @implementation __class__

    + (__class__ *)__accessor__
    {
    static dispatch_once_t once;
    static __class__ *__singleton__;
    dispatch_once(&once, ^ { __singleton__ = [[__class__ alloc] init]; });
    return __singleton__;
    }

    @cursor

    @end`
    wangccddaa
        15
    wangccddaa  
       2014-04-23 15:38:49 +08:00   ❤️ 1
    + (id)sharedList
    {
    static id _sharedInstance = nil;
    static dispatch_once_t once;

    dispatch_once(&once, ^{
    _sharedInstance = [[self alloc] init];
    });
    return _sharedInstance;
    }

    上一份我们的代码 把类名改成 self 是不是就是万能单例了,哈哈~
    krafttuc
        16
    krafttuc  
       2014-04-23 16:45:10 +08:00   ❤️ 1
    https://github.com/mattt/Xcode-Snippets/blob/master/singleton.m

    目前大家都趋向用 `dispatch_onc` 了
    msching
        17
    msching  
       2014-04-24 09:47:22 +08:00
    lz这个写法多线程环境下会生成多个实例。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4600 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 10:04 · PVG 18:04 · LAX 03:04 · JFK 06:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.