NSRangeFromString 测试

官网文档

Returns a range from a textual representation.

Declaration

SWIFT

func NSRangeFromString(aStringString) -> NSRange

OBJECTIVE-C

NSRange NSRangeFromString NSString *aString );

Discussion

Scans aString for two integers which are used as the location and length values, in that order, to create anNSRange struct. If aString only contains a single integer, it is used as the location value. If aString does not contain any integers, this function returns an NSRange struct whose location and length values are both 0.

Availability

Available in iOS 2.0 and later.

 今天发现一个由 NSRangeFromString 引起的bug,主要看这命名吧,对这个方法的理解错误引起的。很惭愧的说,我自以为,这货应该返回的是字符串的开始位置和长度。-_-#! 测试后,又看了文档,才发现,不是这么回事!

 

NSString *str1 = @"abcdef";
NSString *str2 = @"1-105";
NSString *str3 = @"13";
NSString *str4 = @"13 34";
NSString *str5 = @"15 32 77";
NSRange range = NSRangeFromString(str1);
NSLog(@"\nlocation:%ld  length:%ld", range.location, range.length);
//location:0  length:0

range = NSRangeFromString(str2);
NSLog(@"\nlocation:%ld  length:%ld", range.location, range.length);
//location:1  length:105

range = NSRangeFromString(str3);
NSLog(@"\nlocation:%ld  length:%ld", range.location, range.length);
//location:13  length:0

range = NSRangeFromString(str4);
NSLog(@"\nlocation:%ld  length:%ld", range.location, range.length);
//location:13  length:34

range = NSRangeFromString(str5);
NSLog(@"\nlocation:%ld  length:%ld", range.location, range.length);
//location:15  length:32

  

在使用一个api之前要看文档,看文档,看文档

 

 

转载于:https://www.cnblogs.com/warrior/p/5103788.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值