Objective-C 代码中不兼容指针类型的赋值
昨天,我复制并编译了下面的代码,没有问题。但今天,当我编译代码时,它给了我一个警告,并且不会运行 .exe
。我是 Objective-C 的新手,我在 window 上使用 GNUstep。
testString.m: In function 'main':
testString.m:5:13: warning: assignment from incompatible pointer type
** testString.m:5:13 it front of (=)
这是代码。
//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;
}
Yesterday, I copied and compiled the code below and it was fine. But today when I compiled the code it gave me a warning and won't run the .exe
. I'm new in Objective-C and I'm using GNUstep on window.
testString.m: In function 'main':
testString.m:5:13: warning: assignment from incompatible pointer type
** testString.m:5:13 it front of (=)
Here's the code.
//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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSString 文字前面必须有 @ 符号:
代码的另一个问题是,在第一行中创建了一个 NSString 实例,并在第二行中覆盖了该实例 - 所以它只是泄漏。直接在其声明中为 testString 赋值:
NSString literals must have @ symbol before them:
One more problem with your code is that in 1st line you create an instance of NSString which you overwrite in the 2nd line - so it just leaks. Assign value to testString in its declaration directly: