NSRange:
NSRange的定义
typedef struct _NSRange {
NSUInteger location;
NSUInteger length;
} NSRange;
typedef NSRange *NSRangePointer;
NS_INLINE NSRange NSMakeRange(NSUInteger loc, NSUInteger len) {
NSRange r;
r.location = loc;
r.length = len;
return r;
}
NSString *str = @"LiuMingChuan";
NSRange range1 = NSMakeRange(0, 3);
NSLog(@"%@",[str substringWithRange:range1]);
NSRange range2 = [str rangeOfString:@"Ming"];
NSLog(@"range of Ming location is %lu length is %lu",range2.location,range2.length);
NSRange range3 = [str rangeOfString:@"o"];
NSLog(@"range of Ming location is %lu length is %lu",range3.location,range3.length);
// 当没有匹配时返回NSNotFound
// NSNotFound的定义
// static const NSInteger NSNotFound = NSIntegerMax;
// #define NSIntegerMax LONG_MAX
// 当上面location找不到时,其实返回的数值是-1 因为是无符号整数 我们打印的是最大值(使用最高位作为符号位)
// 所以NSNotFound的值是-1
NSLog(@"NSNotFound is %ld",(long)NSNotFound);
NSLog(@"NSNotFound:%d \nNSIntegerMax:%ld \nLONG_MAX:%ld \n",NSNotFound,NSIntegerMax,LONG_MAX);
输出:
<pre name="code" class="objc">2015-10-04 17:06:51.660 OCStudy[3372:2659715] Liu
2015-10-04 17:06:51.661 OCStudy[3372:2659715] range of Ming location is 3 length is 4
2015-10-04 17:06:51.661 OCStudy[3372:2659715] range of Ming location is 9223372036854775807 length is 0
2015-10-04 17:06:51.662 OCStudy[3372:2659715] NSNotFound is 9223372036854775807
2015-10-04 17:06:51.662 OCStudy[3372:2659715] NSNotFound:-1
NSIntegerMax:9223372036854775807
LONG_MAX:9223372036854775807
常用的还有
CGPoint/NSPoint
CGSize/NSSize
CGRect/NSRect
// struct CGPoint {
// CGFloat x;
// CGFloat y;
// };
// typedef struct CGPoint CGPoint;
CGPoint origin = CGPointMake(0, 0);
// CGPoint origin = CGPointZero;
// CGPoint origin = {0,0};
// CGPointEqualToPoint(point1, point2);
// struct CGSize {
// CGFloat width;
// CGFloat height;
// };
// typedef struct CGSize CGSize;
CGSize size = CGSizeMake(100, 100);
// CGSize size = CGSizeZero;
// CGSize size = {100, 100};
// CGSizeEqualToSize(size1, size2);
<span style="color: rgb(75, 209, 87); font-family: Menlo; font-size: 11px;">
</span>// struct CGRect {
// CGPoint origin;
// CGSize size;
// };
// typedef struct CGRect CGRect;
CGRect rect = CGRectMake(0, 0, 100, 100);
// CGRect rect = CGRectZero;
// CGRect rect = {{0,0},{100,100}};
// CGRect rect = {origin,size};
// CGRectEqualToRect(rect1, rect2);
// CGRectContainsPoint(rect, point);
// CGRectContainsRect(rect1, rect2);
//结构体转为字符串
NSString *str1 = NSStringFromPoint(origin);
NSLog(@"%@",str1);
NSString *str2 = NSStringFromSize(size);
NSLog(@"%@",str2);
NSString *str3 = NSStringFromRect(rect);
NSLog(@"%@",str3);
输出:
2015-10-04 23:05:33.243 OCStudy[3780:3398073] {0, 0}
2015-10-04 23:05:33.243 OCStudy[3780:3398073] {100, 100}
2015-10-04 23:05:33.243 OCStudy[3780:3398073] {{0, 0}, {100, 100}}