分配时是否必须使用 initWithString ?

发布于 2024-11-26 16:49:37 字数 255 浏览 1 评论 0原文

NSString *str = [[NSString alloc]init];
str = [str initWithString:@""];

这段代码崩溃了。如果我像下面这样使用,就可以了。

NSString *str = [[NSString alloc]initWithString:@""];

分配后我不能使用initWithString吗?

NSString *str = [[NSString alloc]init];
str = [str initWithString:@""];

This code crashes. If I use like below, it is OK.

NSString *str = [[NSString alloc]initWithString:@""];

Can't I use initWithString after allocation?

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

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

发布评论

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

评论(4

鸢与 2024-12-03 16:49:37

您正在重新初始化内存,该内存已通过在第一行调用-init 的方式初始化。

NSString 对象是不可变的,这意味着它们的内部字符缓冲区不能以任何方式修改。如果您需要此功能,请使用 NSMutableString

因此,您的代码对我来说毫无意义,因为无法修改字符串,为什么用空字符串创建它?

我建议创建一个 NSMutableString 对象,如下所示:

NSMutableString *str = [[NSMutableString alloc] init];

然后,您可以使用各种方法(-setString:replaceCharactersInRange:withString: 等)

我没有看到任何其他原因为什么您要创建一个用空缓冲区初始化的字符串(您拥有)。

You are re-initializing memory which was already initialized by way of your invocation of -init on the first line.

NSString objects are immutable, which means that their internal character buffer cannot be modified in any way. If you need this functionality, use NSMutableString.

Therefore, your code makes no sense to me because there is no way to modify the string, why create it with an empty string?

I suggest creating an NSMutableString object like so:

NSMutableString *str = [[NSMutableString alloc] init];

Then, you can muck around with the string with the various methods (-setString:, replaceCharactersInRange:withString:, etc.)

I don't see any other reason why you'd ever create a string (that you own no less), that is initialized with an empty buffer.

难得心□动 2024-12-03 16:49:37

您的问题表明您不想在同一行代码上分配和初始化对象。当您有某种逻辑可以使用不同的字符串对其进行初始化并且您希望将该逻辑放在分配和初始化之间时,就会出现这种情况。

+alloc方法是分配对象的方法,-init初始化它。尽管这是一个常见的约定,但并不要求一起调用这些方法。因此,以下代码是有效的,并允许您在分配和初始化之间放置代码:

NSString *str = [NSString alloc];
str = [str initWithString:@""];

一种更常见的不同解决方案是将指针声明放在分配之前,因此以下代码也是有效的。

NSString *str = nil;
str = [[NSString alloc] initWithString:@""];

从技术上讲,这仍然会一起分配和初始化对象,如果您出于某种原因实际上需要在两个单独的行上分配和初始化对象,我会推荐我的第一个示例。

Your question suggests that you don't want to allocate and initialize the object on the same line of code. That would be the case when you have some kind of logic that would initialize it with different strings and that you want to put that logic between the allocation and the initialization.

The +alloc method is the method that allocates the objects, -init initializes it. There's no requirement that those methods are called together, although that's a common convention. Therefore, the following code is valid and allows you to put code between the allocation and the initialization:

NSString *str = [NSString alloc];
str = [str initWithString:@""];

A different solution that is fairly more common is to place the declaration of the pointer before the allocation, therefore the following is also valid.

NSString *str = nil;
str = [[NSString alloc] initWithString:@""];

Technically this will still allocate and initialize the object together, if you for some reason actually needs the object to be allocated and initialized on two separate lines I would recommend my first example.

千秋岁 2024-12-03 16:49:37

这是正确的:

 NSString *str = [[NSString alloc] initWithString:@""];

您不应该对对象调用两次 init 方法,就像您在代码中所做的那样。所以,你只需在分配时初始化即可。

This is correct:

 NSString *str = [[NSString alloc] initWithString:@""];

You are not supposed to call twice an init method on an object, like you are doing in your code. So, you just init when allocating.

两相知 2024-12-03 16:49:37
NSString *str = [[NSString alloc] initWithString:@""];

是正确的。另请记住,每当您使用 alloccopynew 时,您都拥有该对象,并且必须在某个时刻释放它

[str release];
NSString *str = [[NSString alloc] initWithString:@""];

is correct. Also remember that whenever you use alloc, copy or new you own the object and have to release it at some point with

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