如果无法比较多个textField.keyboardType;
BOOL b = textField.keyboardType == (UIKeyboardTypeDefault || UIKeyboardTypeASCIICapable || UIKeyboardTypeEmailAddress);
我在UIKeyboardTypeEmailAddress
上获得了“使用逻辑与常量操作数”?
答案 0 :(得分:1)
正确地说,它应该是
BOOL b = (textField.keyboardType == UIKeyboardTypeDefault)
|| (textField.keyboardType == UIKeyboardTypeASCIICapable)
|| (textField.keyboardType == UIKeyboardTypeEmailAddress);
检查keyboardType
是否等于3个值中的一个。
答案 1 :(得分:0)
如果要测试keyboardType是否为列出的值之一,请使用
BOOL b = textField.keyboardType == UIKeyboardTypeDefault || textField.keyboardType == UIKeyboardTypeASCIICapable || textField.keyboardType == UIKeyboardTypeEmailAddress;
答案 2 :(得分:0)
最接近你的尝试,理论上可以完成你想要做的事情(但实际上不会起作用):
BOOL b = textField.keyboardType & (UIKeyboardTypeDefault | UIKeyboardTypeASCIICapable | UIKeyboardTypeEmailAddress);
您只需要使用一个|
来执行按位,或者因为||
执行逻辑运算,或者无法实现您想要的功能。然后你需要对该值进行按位和(&
),只有当两者有共同的位时才能产生非零值。
然而,问题是UIKeyboardType
enum的值不是2的幂,允许你这样做:
typedef NS_ENUM(NSInteger, UIKeyboardType) {
UIKeyboardTypeDefault, // Default type for the current input method.
UIKeyboardTypeASCIICapable, // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
UIKeyboardTypeNumbersAndPunctuation, // Numbers and assorted punctuation.
UIKeyboardTypeURL, // A type optimized for URL entry (shows . / .com prominently).
UIKeyboardTypeNumberPad, // A number pad (0-9). Suitable for PIN entry.
UIKeyboardTypePhonePad, // A phone pad (1-9, *, 0, #, with letters under the numbers).
UIKeyboardTypeNamePhonePad, // A type optimized for entering a person's name or phone number.
UIKeyboardTypeEmailAddress, // A type optimized for multiple email address entry (shows space @ . prominently).
UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1), // A number pad with a decimal point.
UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0), // A type optimized for twitter text entry (easy access to @ #)
UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0), // A default keyboard type with URL-oriented addition (shows space . prominently).
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
};
由于它们不是2的幂,每个都没有唯一的位,这意味着按位 - 或然后按位 - 并且不能保证工作,并且大多数时间不会。
因此,您必须使用相等性单独比较每个:
BOOL b = (textField.keyboardType == UIKeyboardTypeDefault) || (textField.keyboardType == UIKeyboardTypeASCIICapable) || (textField.keyboardType == UIKeyboardTypeEmailAddress);