为什么我必须在Objective-C中指定(NSMutableString *)

时间:2021-04-25 19:58:08
NSMutableString *stringA = [[NSMutableString alloc] init];
NSMutableString *stringB = [[NSMutableString alloc] init];
stringB = (NSMutableString *)stringA;

From now on, stringB points to the same address as string A. I don't understand why do I have to write this :

从现在开始,stringB指向与字符串A相同的地址。我不明白为什么我要写这个:

stringB = (NSMutableString *)stringA;

instead of this:

而不是这个:

stringB = stringA

since both of them are already declared as NSMutableString pointers.

因为它们都已经被声明为NSMutableString指针。

1 个解决方案

#1


1  

You don't understand it, because it is nonsense.

你不明白,因为这是无稽之谈。

You created one mutable string and stored a pointer in stringA.

您创建了一个可变字符串并在stringA中存储了一个指针。

You created another mutable string and stored a pointer in stringB.

您创建了另一个可变字符串并在stringB中存储了一个指针。

Then you stored the first pointer in the second variable, which will get rid of the second mutable string. The second alloc/init was just pointless, nothing but a waste of code and time. And the cast is absolutely not necessary.

然后将第一个指针存储在第二个变量中,这将删除第二个可变字符串。第二个alloc / init只是毫无意义,只是浪费代码和时间。演员是绝对没有必要的。

#1


1  

You don't understand it, because it is nonsense.

你不明白,因为这是无稽之谈。

You created one mutable string and stored a pointer in stringA.

您创建了一个可变字符串并在stringA中存储了一个指针。

You created another mutable string and stored a pointer in stringB.

您创建了另一个可变字符串并在stringB中存储了一个指针。

Then you stored the first pointer in the second variable, which will get rid of the second mutable string. The second alloc/init was just pointless, nothing but a waste of code and time. And the cast is absolutely not necessary.

然后将第一个指针存储在第二个变量中,这将删除第二个可变字符串。第二个alloc / init只是毫无意义,只是浪费代码和时间。演员是绝对没有必要的。