将 NSDecimalNumber 转换为 NSString

发布于 2024-08-27 19:15:51 字数 279 浏览 6 评论 0原文

我正在从如下所示的对象中检索键:

po obj
{
  TypeID = 3;
  TypeName = Asset;
}

键值的检索方式如下:

NSString *typeId = (NSString*)[obj objectForKey:@"TypeID"];

typeId 不是 NSString,而是 NSDecimalNumber。这是为什么?

如何将其转换为 NSString?

I'm retrieving a key from an object that looks like this:

po obj
{
  TypeID = 3;
  TypeName = Asset;
}

The key value is being retrieved like this:

NSString *typeId = (NSString*)[obj objectForKey:@"TypeID"];

Rather than typeId being an NSString, it is an NSDecimalNumber. Why is that?

How do I convert it to an NSString?

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

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

发布评论

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

评论(5

盛装女皇 2024-09-03 19:15:51

您需要使用stringWithFormat函数:

NSString *typeId = [NSString stringWithFormat:@"%@", [obj objectForKey:@"TypeID"]];

stringValue

NSString *typeId = [[obj objectForKey:@"TypeID"] stringValue];

You need to use the stringWithFormat function:

NSString *typeId = [NSString stringWithFormat:@"%@", [obj objectForKey:@"TypeID"]];

or stringValue:

NSString *typeId = [[obj objectForKey:@"TypeID"] stringValue];
十级心震 2024-09-03 19:15:51

这应该有效:

NSString *typeId = [[obj objectForKey:@"TypeID"] stringValue];

This should work:

NSString *typeId = [[obj objectForKey:@"TypeID"] stringValue];
有深☉意 2024-09-03 19:15:51

如果你想获取字符串值,只需使用“description”即可。即使返回的类型是 NSDecimalNumber、NSString 或其他类型,它也能正常工作:

NSString *typeId = [[obj objectForKey:@"TypeID"] description];

If you want to get string value, just use "description". It works fine even if returned type is NSDecimalNumber, or NSString, or whatever:

NSString *typeId = [[obj objectForKey:@"TypeID"] description];
℉服软 2024-09-03 19:15:51

由于这是顶部搜索结果,并且我没有看到有关 Swift 中 NSDecimalNumber 到 String 的问题,所以就在这里。这将显示本地化的小数分隔符和所有具有非零值的小数位。

let string = DecimalNumber.description(withLocale: Locale.current)

Since this is the top search result and I don't see a question for NSDecimalNumber to String in Swift, here it is. This will show the localized decimal separator and all decimal places with a non-zero value.

let string = decimalNumber.description(withLocale: Locale.current)

杀お生予夺 2024-09-03 19:15:51

使用它来将 NSDecimalNumber 转换为 NSString:

NSDecimalNumber* dec1;
NSString* str;

str = dec1.stringValue;

使用它来将 NSString 转换为 NSDecimalNumber:

NSString* string1 = @"Stack overflow 123";
NSDecimalNumber* dec;

dec = (NSDecimalNumber *)string1;

Use this to convert NSDecimalNumber to NSString:

NSDecimalNumber* dec1;
NSString* str;

str = dec1.stringValue;

Use this to convert NSString to NSDecimalNumber:

NSString* string1 = @"Stack overflow 123";
NSDecimalNumber* dec;

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