目标 c:检查是否是整数/整数/数字

发布于 2024-09-14 03:56:58 字数 50 浏览 8 评论 0原文

在 Objective C 中,我如何检查字符串/NSNumber 是整数还是 int

In objective c, how can i check if a string/NSNumber is an integer or int

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

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

发布评论

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

评论(6

凉月流沐 2024-09-21 03:56:58

如果您尝试确定 NSString 是否具有数值,请尝试使用 NSNumberFormatter

-(BOOL) stringIsNumeric:(NSString *) str {
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    NSNumber *number = [formatter numberFromString:str];
    [formatter release];
    return !!number; // If the string is not numeric, number will be nil
}

If you're trying to determine whether or not an NSString has a numeric value or not, try using NSNumberFormatter.

-(BOOL) stringIsNumeric:(NSString *) str {
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    NSNumber *number = [formatter numberFromString:str];
    [formatter release];
    return !!number; // If the string is not numeric, number will be nil
}
蓝海 2024-09-21 03:56:58

您可以在 NSString 上使用 intValue 方法:

NSString *myString = @"123";
[myString intValue]; // returns (int)123

这是 Apple 文档 - 如果它不是有效整数,它将返回 0: http://developer.apple.com/mac/library/documentation/Cocoa /Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/intValue

希望有帮助!

You can use the intValue method on NSString:

NSString *myString = @"123";
[myString intValue]; // returns (int)123

Here is the Apple documentation for it - it will return 0 if it's not a valid integer: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/intValue

Hope that helps!

蒗幽 2024-09-21 03:56:58

我已将其放入我的类别 NSString (Util) 中。

- (BOOL) isInt {
    if ([self isEqualToString:@"0"])
        return YES;
    return (self.intValue != 0);
}

I've put this in my category NSString (Util).

- (BOOL) isInt {
    if ([self isEqualToString:@"0"])
        return YES;
    return (self.intValue != 0);
}
叹沉浮 2024-09-21 03:56:58

要检查 NSNumber 是否为整数,请尝试:

const char *t = [(NSNumber *)value objCType];
if (strcmp("i", t) == 0); // YES if integer

To check if a NSNumber if an integer try:

const char *t = [(NSNumber *)value objCType];
if (strcmp("i", t) == 0); // YES if integer
作死小能手 2024-09-21 03:56:58

对于 NSString,如果您使用 intValue/integerValue,则某些情况不会被处理,因为它只考虑字符串的开头。

例如 @"bl3h" 不被视为整数,但 @"3h" 给出的输出为 3

我的 NSString 解决方案:

使用:

NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:strName];

NSCharacterSet *numSet = [NSCharacterSet decimalDigitCharacterSet];

if([numSet isSupersetOfSet: charSet])
{
     // It is an integer (or it contains only digits)
}

For NSString, if you use intValue/integerValue, there are certain cases which are not handled as it takes into consideration only the beginning the string.

For example @"bl3h" was not considered an integer but @" 3h" gave an output of 3

My solution for NSString:

Use :

NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:strName];

NSCharacterSet *numSet = [NSCharacterSet decimalDigitCharacterSet];

if([numSet isSupersetOfSet: charSet])
{
     // It is an integer (or it contains only digits)
}
噩梦成真你也成魔 2024-09-21 03:56:58
if( [(NSString *)someString intValue] )
{ /* Contains an int fosho! */ }

if( [(NSNumber *)someNumber intValue] )
{ /* Contains an int wich is not 0.... :D */ }

来确定它是 NSString 还是 NSNumber

当然,您可以首先使用[Object isKindOfClass:[NSString class]] 等

... – boolValue
– 字符值
– 小数值
– 双值
– 浮点值
– intValue
– 整数值
– longLongValue
– 长值
– 短值
– unsignedCharValue
– 无符号整数值
– 无符号整数值
– unsignedLongLongValue
– 无符号长值
– unsignedShortValue

都是 NSNumber 获取其值的方法。
NSString 有一些类似的。

if( [(NSString *)someString intValue] )
{ /* Contains an int fosho! */ }

if( [(NSNumber *)someNumber intValue] )
{ /* Contains an int wich is not 0.... :D */ }

You can ofcourse first determine whether its a NSString or NSNumber by using

[Object isKindOfClass:[NSString class]] etc...

– boolValue
– charValue
– decimalValue
– doubleValue
– floatValue
– intValue
– integerValue
– longLongValue
– longValue
– shortValue
– unsignedCharValue
– unsignedIntegerValue
– unsignedIntValue
– unsignedLongLongValue
– unsignedLongValue
– unsignedShortValue

are all methods of NSNumber to get its values.
NSString got a few similar ones.

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