- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我希望能够在 Swift 中正确读取 NSUnderlineStyle 的值。 它比看起来更复杂。
首先,快速回顾一下。 NSUnderlineStyle 是具有以下值的枚举:
NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick = 0x02,
NSUnderlineStyleDouble = 0x09,
NSUnderlinePatternSolid = 0x0000,
NSUnderlinePatternDot = 0x0100,
NSUnderlinePatternDash = 0x0200,
NSUnderlinePatternDashDot = 0x0300,
NSUnderlinePatternDashDotDot = 0x0400,
NSUnderlineByWord = 0x8000
有三组不同的选项。根据Apple's documentation ,“样式、模式和可选的单词掩码通过 OR 组合在一起生成 NSUnderlineStyleAttributeName
和 NSStrikethroughStyleAttributeName
的值。”
NSUnderlineStyle 不实现新的OptionSet Protocol ,这意味着值必须与整数按位运算相结合(显然)。如果是这样,则可能会出现类似于以下内容的情况:
let operation: NSDragOperation = [.copy, .move]
if operation.contains(.copy) {
// Yay!
}
但是,遗憾的是,这不起作用。
我的理解是,要正确检查带有数字的位标志,它是 (A & B == B) 如果 B 在 A 中。
但是,这似乎不适用于 NSUnderlineStyle,我不确定为什么。我不是位掩码专家。
这是演示问题的示例代码。
let style = NSUnderlineStyle.styleSingle.rawValue |
NSUnderlineStyle.patternDashDotDot.rawValue |
NSUnderlineStyle.byWord.rawValue
if style & NSUnderlineStyle.styleSingle.rawValue == NSUnderlineStyle.styleSingle.rawValue {
NSLog("style single found")
}
if style & NSUnderlineStyle.styleDouble.rawValue == NSUnderlineStyle.styleDouble.rawValue {
NSLog("style double found")
}
if style & NSUnderlineStyle.styleThick.rawValue == NSUnderlineStyle.styleThick.rawValue {
NSLog("style thick found")
}
if style & NSUnderlineStyle.patternSolid.rawValue == NSUnderlineStyle.patternSolid.rawValue {
NSLog("pattern solid found")
}
if style & NSUnderlineStyle.patternDash.rawValue == NSUnderlineStyle.patternDash.rawValue {
NSLog("pattern dash found")
}
if style & NSUnderlineStyle.patternDot.rawValue == NSUnderlineStyle.patternDot.rawValue {
NSLog("pattern dot found")
}
if style & NSUnderlineStyle.patternDashDot.rawValue == NSUnderlineStyle.patternDashDot.rawValue {
NSLog("pattern dash dot found")
}
if style & NSUnderlineStyle.patternDashDotDot.rawValue == NSUnderlineStyle.patternDashDotDot.rawValue {
NSLog("pattern dash dot dot found")
}
if style & NSUnderlineStyle.byWord.rawValue == NSUnderlineStyle.byWord.rawValue {
NSLog("by word flag found")
}
这段代码应该产生:
style single found
pattern dash dot dot found
by word flag found
而是产生:
style single found
pattern solid found
pattern dash dot dot found
by word flag found
还有其他失败的情况。
如何正确阅读下划线样式?
任何帮助将不胜感激。此外,如果有人对失败的原因有任何见解,那也太好了。
最佳答案
“style”和“pattern”的枚举值不是位标志。例如,NSUnderlineStyleNone
和NSUnderlinePatternSolid
的值为零,并且NSUnderlineStyleSingle
和 NSUnderlineStyleDouble
有点 (0x01)共同点。
您必须提取与每个组(样式、模式、标志)对应的位,并将该值与可能的值进行比较枚举值,分别为每个组。
不幸的是,Apple 没有为每个组提供位掩码(到目前为止如我所见)所以我们要定义我们自己的。样式值(0x00、0x01、0x02、0x09)都使用位 0...3,和模式值(0x0、0x100、0x200、0x300、0x400)都使用位 8...11。这提出了以下定义:
let underlineStyleMask = 0x000F
let underlinePatternMask = 0x0F00
然后可以用作
// Check style:
switch style & underlineStyleMask {
case NSUnderlineStyle.styleNone.rawValue:
NSLog("no style")
case NSUnderlineStyle.styleSingle.rawValue:
NSLog("single style")
case NSUnderlineStyle.styleDouble.rawValue:
NSLog("double style")
case NSUnderlineStyle.styleThick.rawValue:
NSLog("thick style")
default:
NSLog("unknown style")
}
// Check pattern:
switch style & underlinePatternMask {
case NSUnderlineStyle.patternSolid.rawValue:
NSLog("solid pattern")
case NSUnderlineStyle.patternDot.rawValue:
NSLog("dot pattern")
case NSUnderlineStyle.patternDash.rawValue:
NSLog("dash pattern")
case NSUnderlineStyle.patternDashDot.rawValue:
NSLog("dash dot pattern")
case NSUnderlineStyle.patternDashDotDot.rawValue:
NSLog("dash dot dot pattern")
default:
NSLog("unknown pattern")
}
// Check flags:
if style & NSUnderlineStyle.byWord.rawValue != 0 {
NSLog("by word flag")
}
但请注意,如果 Apple 在中添加更多定义,这可能会中断 future ,例如
NSUnderlineStyleStrange = 0x22
会被错误地检测为 NSUnderlineStyleThick
。
关于ios - 检查 NSUnderlineStyle BitMask 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38688387/
关于 PHP documentation关于 JSON,它提到了位掩码这个词。维基百科将其定义为面具。我不明白位掩码或掩码或它们是如何有用的。有人可以使用外行的术语而不是行话来解释这些术语吗? 最佳答
阅读销售点系统的文档,这里是他们给出的 mask 示例,该 mask 应该告诉您选择了哪些座位。我无法弄清楚这一点。我完全理解位掩码。这个例子是不是错了? 函数 该系统变量是一个八个字符的字符串,其中
我试图从一个二进制文件中取出四个字节,并将它们转换成一个位掩码,代表数据的零和元数据的 1。 我假设我需要通过将它们组合在一起来将它们转换为 int,然后通过移位来确定是否设置了位位置。 我无法弄清楚
这是我的合作伙伴想出的代码,但出于某种原因,我无法联系到他,问他应该如何工作。我现在已经经历了很多次,但似乎无法得到我应该得到的答案。 /** * bitMask - Generate a mask
[旗帜] public enum ShowProductOn : short { HomePage = 1, SalesPage = 2, NewsLetter = 4 };
BufferedImage 类实现 Transparency,它具有三个值: OPAQUE表示不透明。 TRANSLUCENT 表示每个像素的 Alpha 值都在 0 到 1 之间。 BITMASK表
我对这个任务有疑问。 http://www.spoj.com/problems/LINEUP/看起来很简单,但我的解决方案失败了。我得到错误的结果。任何人都可以帮忙弄清楚吗?非常感谢您的帮助。 :-)
我希望能够在 Swift 中正确读取 NSUnderlineStyle 的值。 它比看起来更复杂。 首先,快速回顾一下。 NSUnderlineStyle 是具有以下值的枚举: NSUnderline
我试图避免英雄和硬币之间的碰撞,但不是英雄和草地之间的碰撞。一旦我将碰撞位掩码应用于 Hero,我的 Hero 就会穿过所有东西。我希望它穿过硬币而不是穿过草地。这是代码,有什么想法吗? let HE
所以我决定创建一个控制台类来替代 std::cout console = GetStdHandle(STD_OUTPUT_HANDLE); this->current_color =
我正在研究基于位板的国际象棋引擎,其中一项大量执行的操作是设置/清除无符号 64 位整数中的位。由于我不太了解哪些代码可以在某些处理器上“更快”地运行,因此我无法完全理解这一点。 设置和清除位是一个非
我正在尝试使用 numpy 将几个掩码( bool 数组)转换为一个位掩码,虽然理论上可行,但我觉得我做了太多操作。 例如创建我使用的位掩码: import numpy as np flags = [
例子 self.accessibilityTraits |= UIAccessibilityTraitAdjustable; 添加了 UIAccessibilityTraitAdjustable 选项
我开始使用 ini_get_all 函数来检索共享主机服务器上的所有配置选项。最后我得到了这个数组 block : [allow_call_time_pass_reference] => Array
目前我将一组项目的组合存储为单个整数(位掩码值),如下例所示: 示例: 1 - Orange 2 - Banana 4 - Cherry 8 - Apple 然后如果用户选择Orange (1) 和A
我正在使用 Python 编程并使用 SQLAlchemy 来存储营业时间。 营业时间由三部分组成: 星期几以二进制形式存储为位掩码 营业时间, 企业关闭的时间。 位掩码是这样工作的。有 7 个数字,
我对一种“扩展位”的快速方法很感兴趣,它可以定义如下: 设 B 是一个有 n 位的二进制数,即 B\in {0,1}^n 设P为B中所有1/true位的位置,即1 A 给定 B,使得 Ap[j] ==
我使用嵌入式东西 (PIC),在我见过的所有 C 代码中,如果函数采用多个标志或少量枚举值,它是作为手工制作的 int 位掩码完成的,就像这样: /* first flag */ #define M
所以,最近开始学习iOS开发和Objective-C,主要是利用斯坦福在iTunes U上的免费类(class)。 我最终偶然发现了位掩码,这是 iOS API 中广泛使用但我不是很熟悉的东西。我已经
我想将不同长度的二维数组存储为 AwkwardArray,将它们存储为 Parquet,然后再次访问它们。 问题是,从 Parquet 加载后,格式是 BitMaskedArray并且访问性能有点慢。
我是一名优秀的程序员,十分优秀!