我遇到以下代码的问题:
NSString *strValue=@"??";
char temp[200];
strcpy(temp, [strValue UTF8String]);
printf("%s", temp);
NSLog(@"%s", temp);
Run Code Online (Sandbox Code Playgroud)
在代码的第一行,两个汉字是双引号.问题是printf函数可以正确显示汉字,但NSLog不能.
NSString *strValue=@"??";
char temp[200];
strcpy(temp, [strValue UTF8String]);
printf("%s", temp);
strcpy(temp, [strValue cStringUsingEncoding:NSUTF16LittleEndianStringEncoding]);
NSLog(@"%S", temp);
Run Code Online (Sandbox Code Playgroud) 我遇到了NSString的问题.
NSString* str = [[NSString alloc] initWithString:@"Hello world"];
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我是否需要释放对象str?根据规则,此对象是使用alloc创建的,因此应使用release方法显式释放.但是,当我没有明确释放它时,我在仪器中找不到任何内存泄漏.当NSString被NSNumber替换时,会发生内存泄漏.
谁能给我一些建议?谢谢.
NSNumber* n = [[NSNumber alloc] initWithInt:100];
NSNumber* n1 = n;
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,为什么n的retainCount的值设置为2?在代码的第二行,我没有使用retain来增加retainCount的数量.
我发现了一个奇怪的情况.实际上retainCount取决于初始数量:
NSNumber *n = [[NSNumber alloc] initWithInt:100];
// n has a retainCount of 1
NSNumber *n2 = [[NSNumber alloc] initWithInt:11];
// n has a retainCount of 2
Run Code Online (Sandbox Code Playgroud)