【问题标题】:How to convert NSScanner value to string?如何将 NSScanner 值转换为字符串?
【发布时间】:2011-08-20 16:32:34
【问题描述】:

大家好,你能告诉我如何从 NSScanner 获取字符串中的值吗? 感染我必须从字符串中提取 int 值,我正在使用扫描仪提取它,现在我的值在扫描仪中。现在如何获得该值?

NSString *string = @" rate 50%";
    int n;
        NSScanner * scanner = [[NSScanner alloc] initWithString:string];
        [scanner scanInt:&n];

现在我的价值在哪里以及如何使用?

【问题讨论】:

    标签: iphone nsstring int nsscanner


    【解决方案1】:

    我不会在你的实例中使用 NSScanner。

    试一试。

        NSString *string = @" rate 50%";
        NSString *digits = [string stringByTrimmingCharactersInSet:
                            [[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
        NSLog(@"Value: %i", [digits intValue]);
    

    【讨论】:

      【解决方案2】:

      我同意@Lee 的观点,在您的示例中您不必为 NSScanner 烦恼。但是在您的示例中,int n 设置为 50。这就是您将 n 的地址传递给 scanInt 的原因——因此 scanInt 可以更改其值。

      【讨论】:

        猜你喜欢
        • 2020-11-05
        • 1970-01-01
        • 2016-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        • 1970-01-01
        • 2017-06-09
        相关资源
        最近更新 更多