Cocoa:NSMutableString 给出 stringValue 警告

发布于 2024-08-18 12:12:26 字数 599 浏览 9 评论 0原文

这是有效的:

NSString *myVar = @"whatever"; 

NSDecimalNumber *myNum = [NSDecimalNumber decimalNumberWithString:@"10"];

myVar = [myNum stringValue];

这个带有可变字符串的版本会产生警告“来自不同 Objective-C 类型的分配”:

NSMutableString *myVar = [NSMutableString stringWithString:@"whatever"];  //UPDATE: CORRECTED CODE

NSDecimalNumber *myNum = [NSDecimalNumber decimalNumberWithString:@"10"];

myVar = [myNum stringValue];

在这两种情况下 stringValue 都返回 NSCFString。不可变的 NSString 变量并不关心,可变的 NSMutableString 会抱怨。

PS 有人请为 NSMutableString 和 stringValue 添加标签。

This works:

NSString *myVar = @"whatever"; 

NSDecimalNumber *myNum = [NSDecimalNumber decimalNumberWithString:@"10"];

myVar = [myNum stringValue];

This version with mutable string produces warning "assignment from distinct Objective-C type":

NSMutableString *myVar = [NSMutableString stringWithString:@"whatever"];  //UPDATE: CORRECTED CODE

NSDecimalNumber *myNum = [NSDecimalNumber decimalNumberWithString:@"10"];

myVar = [myNum stringValue];

In both cases stringValue is returning an NSCFString. The immutable NSString variable doesn't care, the mutable NSMutableString complains.

P.S. someone please add tags for NSMutableString and stringValue.

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

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

发布评论

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

评论(3

最丧也最甜 2024-08-25 12:12:26

-stringValue 返回 NSString 的自动释放实例,即不可变对象。即使您将其分配给可变字符串指针,它也不会使字符串可变,并且您将无法在其上调用可变字符串方法(顺便说一句,第一个代码也是如此):

NSMutableString* tStr = @"lala";
[tStr appendString:@"lalala"]; // CRASH! Attempting to mutate immutable object

处理它的正确方法是使用便捷方法创建可变字符串:

NSMutableString* tStr = [NSMutableString stringWithString:@"lala"];
[tStr appendString:@"lalala"]; // OK 

-stringValue returns autoreleased instance of NSString, that is immutable object. Even if you assign it to the mutable string pointer it will not make the string mutable and you will not be able to call mutable string methods on it (btw, the same stays true for your 1st code):

NSMutableString* tStr = @"lala";
[tStr appendString:@"lalala"]; // CRASH! Attempting to mutate immutable object

The correct way to handle it is to create mutable string with convinience method:

NSMutableString* tStr = [NSMutableString stringWithString:@"lala"];
[tStr appendString:@"lalala"]; // OK 
天暗了我发光 2024-08-25 12:12:26

[myNum stringValue] 返回 NSString,而不是 NSMutableString,因此这将生成警告。

如果您稍后尝试操作 myVar 的实例(假设它是一个可变字符串),您将得到一个异常,因为该对象根本不是一个可变字符串。

[myNum stringValue] returns a NSString, not NSMutableString, so this will generate the warning.

If you would try to manipulate the instance of myVar later on (assuming it's a mutable string), you would get an exception, because the object is not a mutable string at all.

背叛残局 2024-08-25 12:12:26

您不能仅通过将不可变字符串分配给 NSMutableString * 类型的变量来将其变为可变字符串。您所做的本质上是:

NSString *immutableStr = @"Mayonnaise";
NSMutableString *mutableStr = immutableStr;

本例中的两个变量都指向完全相同的常量字符串对象(指针比较相等)。您还会收到警告,因为您尝试使用 NSString * 类型的不兼容值设置 NSMutableString * 类型的变量。它是不兼容的,因为 NSMutableString * 提供了 NSString * 不提供的方法和行为,因此当您尝试使用 NSMutableString 的行为时,您会出现运行时错误,因为变量指向的实际对象不是 NSMutableString 。

You cannot make an immutable string into a mutable one simply by assigning it to a variable of type NSMutableString *. What you're doing is essentially:

NSString *immutableStr = @"Mayonnaise";
NSMutableString *mutableStr = immutableStr;

Both variables in this case point to exactly the same constant string object (the pointers will compare equal). You will also get a warning because you are attempting to set a variable of type NSMutableString * with an incompatible value of type NSString *. It is incompatible because NSMutableString * provides methods and behaviour that NSString * doesn't, so when you try to use NSMutableString's behaviour, you will get runtime errors because the actual object being pointed to by the variable is not an NSMutableString.

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