gpt4 book ai didi

iOS - 引用计数问题

转载 作者:行者123 更新时间:2023-11-28 20:03:59 25 4
gpt4 key购买 nike

我正在做一些关于增加引用计数的工作。下面是示例。

.h 文件。

@property (nonatomic, retain) NSString *s1;

.m文件

@synthesize s1;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

s1 = [[NSString alloc] init];
NSLog(@"%d",[s1 retainCount]);

[s1 retain];
NSLog(@"%d",[s1 retainCount]);

[s1 copy];
NSLog(@"%d",[s1 retainCount]);
}

当我找到引用计数时,所有引用计数都显示为 -1,对此我有点困惑,请帮助我。

最佳答案

它打印 -1 因为您使用了错误的字符串格式。

由于 retainCount 返回一个 NSUIngeter(即无符号整数),您应该使用

NSLog(@"%lu", (unsigned long)myNSUInteger); 

除此之外,值得一提的是,您不应该永远依赖retainCount

参见 When To You Retain Count .

此外,来自 official documentation :

This method is of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

关于iOS - 引用计数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22756623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com