Objective-C 代码中不兼容指针类型的赋值

发布于 2024-11-09 11:04:45 字数 612 浏览 0 评论 0原文

昨天,我复制并编译了下面的代码,没有问题。但今天,当我编译代码时,它给了我一个警告,并且不会运行 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚如初 2024-11-16 11:04:45

NSString 文字前面必须有 @ 符号:

testString = @"Here's a test string in testString!";

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

NSString *testString = @"Here's a test string in testString!";

NSString literals must have @ symbol before them:

testString = @"Here's a test string in testString!";

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:

NSString *testString = @"Here's a test string in testString!";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文