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
lcl22hope
V2EX  ›  iDev

在 iOS7 里面,weak,unsafe_unretained 等修饰符的作用去掉了么?为什么自己测试时和书上看到的结果不一样呢?还是我测试的有问题呢?麻烦大家给指点一下了

  •  
  •   lcl22hope · 2014-08-28 08:33:45 +08:00 · 4510 次点击
    这是一个创建于 3548 天前的主题,其中的信息可能已经有所发展或是发生改变。
    NSString* test = [NSString stringWithFormat:@"test1"];
    NSString __unsafe_unretained *c = test;
    NSString __weak *d = test;
    test = @“test2”;
    NSLog(@"%@",c);
    NSLog(@"%@",d);
    /////////////////////////////////////////// 不应该是空值么?
    2014-08-28 08:14:43.583 TestOnline[191:60b] test1
    2014-08-28 08:14:44.665 TestOnline[191:60b] test1
    8 条回复    2014-08-28 10:11:55 +08:00
    lcl22hope
        1
    lcl22hope  
    OP
       2014-08-28 08:54:06 +08:00
    原来是不应该用NSString来做实验,但确实网上好多帖子都是用NSString来举例的啊,很怀疑他们自己都测没测过,自己写的东西。

    // Do any additional setup after loading the view, typically from a nib.
    NSObject* test = [[NSObject alloc] init];
    NSObject __unsafe_unretained *c = test;
    NSObject __weak *d = test;
    test = nil;

    NSLog(@"%@",[c description]);//bad access
    NSLog(@"%@",[d description]);//d 已经是nil
    lcl22hope
        2
    lcl22hope  
    OP
       2014-08-28 08:55:47 +08:00
    但是问题来了,为什么NSString不行呢?求指教
    cielpy
        3
    cielpy  
       2014-08-28 09:12:30 +08:00   ❤️ 1
    @lcl22hope Google一下 关键字 NSString 内存 应该有你想了解的。
    likid
        4
    likid  
       2014-08-28 09:16:40 +08:00   ❤️ 1
    编译器会对 NSString 做优化,NSString Class 方法生成的变量会被存储在某个地方(具体忘了在哪里),所以不会被立即释放。
    另,这种问题不应该来这么问吧。Stackoverflow 更好
    lcl22hope
        5
    lcl22hope  
    OP
       2014-08-28 09:20:48 +08:00
    @likid 谢谢回答,后来是在stackoverflow上得到启示,具体我在google下,多谢指点
    lcl22hope
        6
    lcl22hope  
    OP
       2014-08-28 09:21:41 +08:00
    @cielpy 好的,谢谢指点,我去谷歌了解下
    dopcn
        7
    dopcn  
       2014-08-28 09:38:30 +08:00   ❤️ 1
    @likid
    我看过的是说 NSString 的常量@"test"因为编译器的优化不会立刻释放
    而[NSString stringWithFormat:@"test1"];
    等价于
    [[[NSString alloc] initWithString:@"test1"] autorelease];
    这种情况下必须等到 autoreleasepool 干掉才会释放

    总之用 NSString 做这个确实不合适
    66450146
        8
    66450146  
       2014-08-28 10:11:55 +08:00   ❤️ 1
    String literals are optimized that they can be saved in a static memory area that won't be changed.

    记住字符串常量的特殊性就好了,从 c-string 到 NSString 都是这样,字符串常量因为使用广泛而且优化效果明显(静态内存区),基本上所有的编译器都会去做这个工作。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1011 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 19:43 · PVG 03:43 · LAX 12:43 · JFK 15:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.