在Objective-C代码中从不兼容的指针类型分配

min*_*und 0 types pointers objective-c variable-assignment

昨天,我复制并编译了下面的代码,很好.但今天当我编译代码时,它给了我一个警告,不会运行.exe.我是Objective-C的新手,我在窗口上使用GNUstep.

testString.m: In function 'main':
testString.m:5:13: warning: assignment from incompatible pointer type
** testString.m:5:13 it front of (=)
Run Code Online (Sandbox Code Playgroud)

这是代码.

//testString.m
#import <Foundation/Foundation.h>
int main (int argc,  const char * argv[])
{
NSString *testString = [[NSString alloc] init ];
testString = "Here's a test string in testString!";
NSLog(@"testString: %@", testString);

return 0;
}
Run Code Online (Sandbox Code Playgroud)

Vla*_*mir 6

NSString文字必须在它们之前有@符号:

testString = @"Here's a test string in testString!";
Run Code Online (Sandbox Code Playgroud)

您的代码的另一个问题是,在第1行中,您创建了一个NSString实例,您在第二行中覆盖它 - 所以它只是泄漏.直接在其声明中为testString赋值:

NSString *testString = @"Here's a test string in testString!";
Run Code Online (Sandbox Code Playgroud)