gpt4 book ai didi

ios - 为什么不分配这些 NSString 实例变量?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:11 25 4
gpt4 key购买 nike

第一次发帖。我之前正在阅读 Objective-C 教程,我看到他们制作了几个 NSString 实例变量,如下所示:

@implementation MyViewController {
NSString *stringOne;
NSString *stringTwo;
NSString *stringThree;
NSString *stringFour;
NSString *stringFive;
}

然后像这样简单地在 ViewDidLoad 中使用它们:

- (void)viewDidLoad
{
[super viewDidLoad];

stringOne = @"Hello.";
stringTwo = @"Goodbye.";
stringThree = @"Can't think of anything else to say.";
stringFour = @"Help...";
stringFive = @"Pheww, done.";
}

他们是如何在不实例化字符串的情况下做到这一点的?为什么这行得通?当然,在您可以简单地执行 stringOne= @"Hello. ";.

抱歉,如果这是一个愚蠢的问题,但我发现这些小事情让我很困惑。

谢谢,迈克

最佳答案

来自Apple String Programming Guide :

Creating Strings

The simplest way to create a string object in source code is to use the Objective-C @"..." construct:

NSString *temp = @"Contrafibularity";

Note that, when creating a string constant in this fashion, you should use UTF-8 characters. Such an object is created at compile time and exists throughout your program’s execution. The compiler makes such object constants unique on a per-module basis, and they’re never deallocated. You can also send messages directly to a string constant as you do any other string:

BOOL same = [@"comparison" isEqualToString:myString];

关于ios - 为什么不分配这些 NSString 实例变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15732399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com