ios - (BOOL)strstr() 在 32 位 iOS 设备中失败

标签 ios objective-c xcode6 32-bit

我用strstr判断是否包含字符串,然后将结果强转为BOOL类型,例如:

BOOL result = (BOOL)strstr(aString, "test");

在 32 位 iOS 设备中,当 aString 包含“test”字符串时,有时会出现结果为 NO。但是 strstr(aString, "test") 正确返回地址。

最佳答案

根据体系结构,BOOL 可能被typedef 转换为unsigned char。那只是8位。当您将指针转换为 BOOL 时,您可能只会从指针获取低 8 位。如果这些位全为 0,则 BOOL 为假,即使指针不是 NULL (0)。

换句话说,不要那样做。

如果你想要一个 bool 值,使用 strstr(aString, "test") != NULL 甚至 !!strstr(aString, "test")

关于ios - (BOOL)strstr() 在 32 位 iOS 设备中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32020876/

相关文章:

uitableview - rectForRowAtIndexPath 不适用于 iOS8

iOS 11 [MediaRemote] [AVOutputContext] 警告 : AVF context unavailable for MRAVOutputContextCreateIndependentRoutingContext

ios - 如何为 UITextView Swift 添加下划线/粗体/斜体能力

objective-c - 从 Swift 传递回调函数调用 Objective C 和 C

ios - UIImage 在 ObjectiveC 中编码为 NSData,然后在 Swift 中解码

ios - Swift - 分段控制 - 切换多个 View

swift - ld : framework not found AFNetworking clang: error: linker command failed with exit code 1 (use -v to see invocation)

ios - 如何删除选择单元格上 UIcollectionViewCell 特定单元格中的长手势?

ios - UISearchDisplayController 表格框架

objective-c - 如何在 NSPersistentDocument 中强制创建默认的 persistentStore/autosave