我想知道你-retainCount到目前为止在什么情况下使用过,最后使用它可能发生的问题.
谢谢.
我对内存管理技术的要求还不是很高,并且想知道是否有人可以向我解释这种奇怪的行为.考虑一下我测试的这3段代码:
DofView* dofView = [[DofView alloc] initWithNibName:@"DofView" bundle:nil];
NSLog(@"dof retain count = %d", [dofView retainCount]);
Run Code Online (Sandbox Code Playgroud)
此日志:保留计数= 1.这很好.
DofView* dofView = [[DofView alloc] initWithNibName:@"DofView" bundle:nil];
[dofView release];
NSLog(@"dof retain count = %d", [dofView retainCount]);
Run Code Online (Sandbox Code Playgroud)
这个日志:保留计数= 1.不应该是0 ??
DofView* dofView = [[DofView alloc] initWithNibName:@"DofView" bundle:nil];
[self.navigationController pushViewController:dofView animated:YES];
NSLog(@"dof retian count = %d", [dofView retainCount]);
Run Code Online (Sandbox Code Playgroud)
这个日志:保留计数= 5.我不知道为什么它的五个?
任何人都对此有所了解吗?我担心每次创建新视图时我都会吃掉内存.
谢谢!
只是几天我正在研究一个项目,我必须看看什么是一个字符串的保留计数.
但它总是让我回复"2147483647",为什么会如此呢?
查看此代码以自行检查.
NSString *str = [[NSString alloc] initWithString:@"Hello World"];
NSLog(@"String Retain Count: %i", [str retainCount]);
Run Code Online (Sandbox Code Playgroud)
所以我的问题是为什么它没有返回1像其他对象返回,为什么我得到"2147483647"
提前致谢.
我有以下示例类:
Test.h:
@interface Test : UIButton {
NSString *value;
}
- (id)initWithValue:(NSString *)newValue;
@property(copy) NSString *value;
Run Code Online (Sandbox Code Playgroud)
Test.m:
@implementation Test
@synthesize value;
- (id)initWithValue:(NSString *)newValue {
[super init];
NSLog(@"before nil value has retain count of %d", [value retainCount]);
value = nil;
NSLog(@"on nil value has retain count of %d", [value retainCount]);
value = newValue;
NSLog(@"after init value has retain count of %d", [value retainCount]);
return self;
}
Run Code Online (Sandbox Code Playgroud)
其中产生以下输出:
2008-12-31 09:31:41.755 Concentration[18604:20b] before nil value has retain count of 0
2008-12-31 09:31:41.756 …Run Code Online (Sandbox Code Playgroud)