在另一个类中使用 NSString

发布于 2025-01-02 10:31:44 字数 358 浏览 1 评论 0原文

我只想从第二堂课的第一堂课访问 NSString 。

我在我的第一个类中使用了属性,这:

NSLog(@"The text is: %@", self.fileText);

在我的第一个类中返回正确的字符串。

但在我的第二堂课中,这个:

FirstViewController* controller = [[FirstViewController alloc] init];
NSLog(@"text: %@", controller.fileText);

返回(null)。

我正确导入了该类。 我可能做错了什么?

I simply want to access a NSString from my first class in my second class.

I used properties in my first class and this:

NSLog(@"The text is: %@", self.fileText);

returns the correct string in my first class.

But in my second class, this:

FirstViewController* controller = [[FirstViewController alloc] init];
NSLog(@"text: %@", controller.fileText);

returns (null).

I imported the class correctly.
What could I have done wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

━╋う一瞬間旳綻放 2025-01-09 10:31:44

使用这个

//in your first class
NSUserDefaults *strinToSave= [NSUserDefaults standardUserDefaults];
[strinToSave setObject:self.fileText forKey:@"filTextString"];

并检索第二个类中保存的数据,使用这个:

NSString *yourString=[[NSUserDefaults standardUserDefaults] valueForKey:@"filTextString"];
//use yourString however you want

NSLog(@"%@",yourString);

Use this

//in your first class
NSUserDefaults *strinToSave= [NSUserDefaults standardUserDefaults];
[strinToSave setObject:self.fileText forKey:@"filTextString"];

and to retrieve saved data in the second class, use this:

NSString *yourString=[[NSUserDefaults standardUserDefaults] valueForKey:@"filTextString"];
//use yourString however you want

NSLog(@"%@",yourString);
爱格式化 2025-01-09 10:31:44

考虑到第二个类要么是第一个类的子类,要么只是该类的一个实例(这似乎更有可能)。

确保您在 FirstViewController 的 init 块中执行的操作,因为这就是第二个实现所调用的全部内容,如果您尚未在那里初始化字符串,则该值将为 null。是的,检查那里。

Considering the second class is either a sub-class of the first class or jus an instance of the class (which seems more likely).

Be sure what you are doing in your init block of the FirstViewController, because that's all that's being called for the second implementation and if you have not initialized your string there, the value will be null. So yep, check there.

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