Cocoa:NSMutableString 给出 stringValue 警告
这是有效的:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
-stringValue
返回 NSString 的自动释放实例,即不可变对象。即使您将其分配给可变字符串指针,它也不会使字符串可变,并且您将无法在其上调用可变字符串方法(顺便说一句,第一个代码也是如此):处理它的正确方法是使用便捷方法创建可变字符串:
-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):The correct way to handle it is to create mutable string with convinience method:
[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.
您不能仅通过将不可变字符串分配给 NSMutableString * 类型的变量来将其变为可变字符串。您所做的本质上是:
本例中的两个变量都指向完全相同的常量字符串对象(指针比较相等)。您还会收到警告,因为您尝试使用
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: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 typeNSString *
. It is incompatible becauseNSMutableString *
provides methods and behaviour thatNSString *
doesn't, so when you try to useNSMutableString
's behaviour, you will get runtime errors because the actual object being pointed to by the variable is not anNSMutableString
.