iOS- 基础 [NSString boolValue]结果分析

If you don’t know NSHipster , you’re missing a real gem among Cocoa blogs. Mattt Thompson has created this journal to weekly lighten unknown Cocoa / Cocoa Touch classes. And Mattt was even on stage at the WWDC 2013, presenting some Hidden Gems in Cocoa and Cocoa Touch .

As a tribute to NSHipster, let me present [NSString boolValue] . Every tailored iOS developer should know this method that converts a BOOL from a NSString .

Reading from the doc :

boolValue
Returns the Boolean value of the receiver’s text.

- (BOOL)boolValue 

Return Value
The Boolean value of the receiver’s text. Returns YES on encountering one of “Y”, “y”, “T”, “t”, or a digit 1-9—the method ignores any trailing characters. Returns NO if the receiver doesn’t begin with a valid decimal text representation of a number.
So booValue can scan and produce these outputs from the followings strings input:

stringboolValue
YYES
NNO
TYES
FNO
tYES
fNO
1YES
0NO
YesYES
NoNO
No really noNO
trueYES
falseNO
To be or not to beYES
FalseNO
3567YES
0123456789NO

With this snippet , you can to check the results:

#import <Foundation/Foundation.h>

// clang -g -framework Foundation -o bool bool.m

int main (void)
{
    NSArray *tests = @[ @"Y", 
                        @"N", 
                        @"T", 
                        @"F", 
                        @"t", 
                        @"f", 
                        @"1", 
                        @"0", 
                        @"Yes", 
                        @"No", 
                        @"No really no", 
                        @"true", 
                        @"false", 
                        @"To be or not to be", 
                        @"False", 
                        @"3567",
                        @"0123456789"
                        ];
    NSArray *boolToString = @[@"NO", @"YES"];

    for (NSString *test in tests){
        NSLog(@"boolValue:\"%@\" => %@", test, boolToString[[test boolValue]]);
    }

    return 0;
}

Super minimalist and smart algorithm, you can also use boolValue ’s companion: doubleValue , floatValue , intValue , integerValue , longLongValue . If you didn’t learn anything, check NSHipster , I promisse you will learn a lot…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值