【问题标题】:Assignment from incompatible pointer type in my Objective-C code我的 Objective-C 代码中不兼容的指针类型的赋值
【发布时间】:2011-09-01 13:33:37
【问题描述】:

昨天,我复制并编译了下面的代码,它很好。但是今天当我编译代码时它给了我一个警告并且不会运行.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;
}

【问题讨论】:

    标签: objective-c pointers types variable-assignment


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2021-06-07
      • 2019-08-09
      • 2012-03-13
      • 2017-03-26
      • 2021-04-07
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多