简体   繁体   English

将String强制转换为Int64会导致32位设备崩溃

[英]Cast String to Int64 causes crash on 32bit devices

I have to deal with a pretty long Int, that comes to me as a String. 我必须处理一个很长的Int,这是作为String出现的。 Calling Int64(String) works fine on 64bit devices, but I see crashes on 32bit devices. 在64位设备上调用Int64(String)可以正常工作,但是在32位设备上我会崩溃。 What is the reason for this? 这是什么原因呢?

Here is the code: 这是代码:

let predicateBarcode = NSPredicate(format: "barcode = %ld", Int64(searchTerm)!)

I cannot tell anything about the searchterm, it comes from the barcode scanner and is an ean-13. 我无法告知搜索字词,它来自条形码扫描仪,它是ean-13。 I can also not reproduce the crash, as this is only happening to my costomers. 我也无法重现崩溃,因为这仅发生在我的客户身上。

It's not the problem of Int64.init(_:) but the problem of the format given to NSPredicate . 这不是问题Int64.init(_:)但考虑到格式的问题NSPredicate

Length specifier l means its argument needs to be long or unsigned long , which are equivalent to Int or UInt in Swift. 长度说明符l表示其参数需要为longunsigned long ,这与Swift中的IntUInt等效。

String Format Specifiers 字符串格式说明符

If you want to use Int64 value as a format argument, the right length specifier is ll , meaning long long which is equivalent to Int64 in Swift. 如果要使用Int64值作为格式参数,则正确的长度说明符为ll ,这意味着long long ,这与Swift中的Int64等效。

let predicateBarcode = NSPredicate(format: "barcode = %lld", Int64(searchTerm)!)

You may need to fix some other parts, but I cannot see as you are hiding other parts. 您可能需要修复一些其他零件,但是由于您正在隐藏其他零件,所以我看不到。 (And as far as I test, I could not make my testing app crash.) In addition, are you 100%-sure about Int64(searchTerm)! (就我的测试而言,我无法使我的测试应用程序崩溃。)此外,您是否对Int64(searchTerm)!百分百确定Int64(searchTerm)! may not crash? 可能不会崩溃?

Anyway, the format string needs to be fixed at least. 无论如何,格式字符串至少需要固定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM