【问题标题】:Can't use a string in .m不能在 .m 中使用字符串
【发布时间】:2012-04-11 04:38:15
【问题描述】:

我正在制作一个应用程序,其中包含不同的东西:图像、视频、viewController(.h 和 .m)和另一个文件(我们称之为 file2,也包含 .h 和 .m)

问题是我想在file2.m 中使用viewController.h 中的字符串,但我不能。我试过这个:

// ViewController.h

{
  NSString *string;
}
@property (nonatomic, retain) NSString *string;


//ViewController.m

 @synthesize string;

    // file2.m

#import"viewController.h"

- (IBAction)changestring:(id)sender {
  string = @"something";
}

所以当我尝试更改字符串时,Xcode 会返回一个错误。我究竟做错了什么? 谢谢!

【问题讨论】:

  • 您需要指定 XCode 在您的帖子中返回的错误。
  • 使用未声明的标识符“字符串”
  • 请发布正确的最小示例。因为它不会编译。在头文件中显示一个简单的接口声明和只使用方法 changeString 的最简单实现。
  • 好的!一会我就改!

标签: objective-c ios xcode macos


【解决方案1】:

像这样在界面外的 viewController.h 中为您的字符串添加一个属性

viewController.h

{
NSString *string;
}
@property (nonatomic, retain) NSString *string; 

并在

中合成
viewController.m

@synthesize string;

这样您就可以在导入 viewController.h 的其他文件中访问该字符串

编辑:

在 file2 中为您的 viewController.h 创建一个对象并像这样访问

file2.h
   {
viewController *viewCont;
}
    file2.m

 #import"viewController.h"

-(IBAction)changestring:(id)sender
  {
      viewCont.string=@"something";
}

【讨论】:

  • 使用未声明的标识符“字符串”
  • 我已经在 viewController.h 中声明了
猜你喜欢
  • 2020-09-15
  • 1970-01-01
  • 2018-04-23
  • 2012-05-12
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多