分配时是否必须使用 initWithString ?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在重新初始化内存,该内存已通过在第一行调用
-init
的方式初始化。NSString
对象是不可变的,这意味着它们的内部字符缓冲区不能以任何方式修改。如果您需要此功能,请使用NSMutableString
。因此,您的代码对我来说毫无意义,因为无法修改字符串,为什么用空字符串创建它?
我建议创建一个 NSMutableString 对象,如下所示:
然后,您可以使用各种方法(
-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, useNSMutableString
.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: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.
您的问题表明您不想在同一行代码上分配和初始化对象。当您有某种逻辑可以使用不同的字符串对其进行初始化并且您希望将该逻辑放在分配和初始化之间时,就会出现这种情况。
+alloc方法是分配对象的方法,-init初始化它。尽管这是一个常见的约定,但并不要求一起调用这些方法。因此,以下代码是有效的,并允许您在分配和初始化之间放置代码:
一种更常见的不同解决方案是将指针声明放在分配之前,因此以下代码也是有效的。
从技术上讲,这仍然会一起分配和初始化对象,如果您出于某种原因实际上需要在两个单独的行上分配和初始化对象,我会推荐我的第一个示例。
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:
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.
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.
这是正确的:
您不应该对对象调用两次
init
方法,就像您在代码中所做的那样。所以,你只需在分配时初始化即可。This is correct:
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.是正确的。另请记住,每当您使用
alloc
、copy
或new
时,您都拥有该对象,并且必须在某个时刻释放它is correct. Also remember that whenever you use
alloc
,copy
ornew
you own the object and have to release it at some point with