先看一段代码
bool a1 = [@"yes" boolValue];
bool a2 = [@"Yes" boolValue];
bool a3 = [@"YEs" boolValue];
bool a4 = [@"YeS" boolValue];
bool a5 = [@"YES" boolValue];
bool b1 = [@"no" boolValue];
bool b2 = [@"NO" boolValue];
bool b3 = [@"No" boolValue];
bool b4 = [@"nO" boolValue];
bool o1 = [@"true" boolValue];
bool o2 = [@"True" boolValue];
bool o3 = [@"TRUE" boolValue];
bool o4 = [@"TRue" boolValue];
bool o5 = [@"trUE" boolValue];
bool o6 = [@"tasdfasdfasdfasdf" boolValue];
bool p1 = [@"false" boolValue];
bool p2 = [@"FALSE" boolValue];
bool p3 = [@"False" boolValue];
bool p4 = [@"FAlse" boolValue];
bool p5 = [@"fALSE" boolValue];
bool o7 = [@"fasdfasdfasdf" boolValue];
bool o8 = [@"a" boolValue];
bool o9 = [@"a123123" boolValue];
bool j = [@"1" boolValue];
bool k = [@"1231231" boolValue];
bool l = [@"-1" boolValue];
bool m = [@"0" boolValue];
上面运行结果如何?
针对yes,true,false,no,0,1这几种服务器都有可能返回的情况, 我们在if else 是否有担心逻辑错误?
其实,担心是多余的, 看苹果的注释
// Skips initial space characters (whitespaceSet), or optional -/+ sign followed by zeroes. Returns YES on encountering one of "Y", "y", "T", "t", or a digit 1-9. It ignores any trailing characters.
可以看到,非常简单, 粗暴
+,-正负、空白字符开头的跳过忽略,
第一个字符是 y,Y,T,t,1~9 就是真
除此之外, 全是假
如果是number类型的呢? 一样简单粗暴, 0总是假,非0就是真
A 0 value always means NO, and any nonzero value is interpreted as YES.
网友评论