gpt4 book ai didi

iphone - 如何使用NSNumberFormatter中的方法“decimalSeparator”

转载 作者:行者123 更新时间:2023-12-01 17:09:42 25 4
gpt4 key购买 nike

在我的应用程序中,我尝试检查用户是否根据本地设置输入了正确的小数点分隔符(如果用户输入错误,则数据不会进入CoreData数据库):

- (void)save {
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];

NSLocale *locale = [NSLocale currentLocale];

我得到用户输入:
NSString *firstForDistance = _forDistanceTextField.text;

尝试从字符串中提取小数点分隔符:
NSString *separator = [firstForDistance decimalSeparator];

而不是检查分隔符是否适合用户的本地设置或他是否打错了字。如果出现错别字或错误,我用他本地设置中的小数点分隔符替换它:
if (separator != [locale objectForKey:NSLocaleDecimalSeparator]) {
firstForDistance = [firstForDistance stringByReplacingOccurrencesOfString:separator withString:[locale objectForKey: NSLocaleDecimalSeparator]];
}

其余代码:
self.user.forDistance = [numberFormatter numberFromString:firstForDistance];

[self.delegate usersMakeViewController:self addUser:self.user];
}

那将是很好并且容易的,但是提取不起作用:
NSString *separator = [firstForDistance decimalSeparator];

方法“decimalSeparator”是NSNumberFormatter的实例方法,我从Xcode遇到错误。

我该如何解决?

最佳答案

您可以检查以下字符串是否具有小数点分隔符

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale currentLocale]];
if ([textField.text rangeOfString:numberFormatter.decimalSeparator].location != NSNotFound)
NSLog(@"YES, Give number has decimalSeparator");
else
NSLog(@"NO, dont have decimalSeparator");

关于iphone - 如何使用NSNumberFormatter中的方法“decimalSeparator”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13723440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com