Obeject-C 结构体 NSRange NSPoint NSSize NSMakeRect

来源:互联网 发布:抢购软件哪个好 编辑:程序博客网 时间:2024/06/05 08:48

NSRange的定义

typedef struct _NSRange

{

  NSUInteger location;

  NSUInteger length;

} NSRange;

NSRange是一个结构体,其中location是一个以0为开始的indexlength是表示对象的长度。他们都是NSUInteger类型。NSRange的方法:NSEqualRanges()
NSIntersectionRange()
NSLocationInRange()
NSMakeRange()
NSMaxRange()
NSRangeFromString()
NSStringFromRange()

NSUnionRange()

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){    @autoreleasepool {               //创建 NSRange        NSRange range=NSMakeRange(0, 2);//0表示位置 2表示长度        NSLog(@"(%li,%li)",range.location,range.length);//输出 (0,2)                //也可如下输出,输出的是 NSString        NSLog(@"%@",NSStringFromRange(range));//输出 {0, 2}                //用 NSRange 截取 NSString        NSString *pStr=@"string";        NSString *pStr2=[pStr substringWithRange:range];        NSLog(@"%@",pStr2);//输出 st                //在  NSString 中查找 另一个 字符串(NSString)返回NSRange类型        NSRange range2=[pStr rangeOfString:@"st"];        NSLog(@"(%li,%li)",range2.location,range2.length);//输出 (0,2)                //如果没有该字符串则 range3.length=0        //示例        NSRange range3=[pStr rangeOfString:@"stt"];        if (range3.length==0)        {            NSLog(@"%@",NSStringFromRange(range3));            NSLog(@"没有找到");        }        else        {            NSLog(@"%li,%li",range3.location,range3.length);        }        //输出结果:{9223372036854775807, 0}  没有找到                //NSEqualRanges()判断两个范围是否相等  返回BOOL类型        //相等返回YES        BOOL Bool= NSEqualRanges(range,range2);        NSLog(@"%i",Bool);                //NSIntersectionRange()方法不常用 不知道什么意思 好像是交叉什么的        //有知道的说以一下        NSRange range4= NSIntersectionRange(range, NSMakeRange(1,2));        NSLog(@"%@",NSStringFromRange(range4));//{1, 1}            return 0;}}


另外结构体:NSPoint  NSSize  NSMakeRect 如下:

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){    @autoreleasepool {                // NSPoint的定义         /*         typedef struct _NSPoint {         CGFloat x;         CGFloat y;         } NSPoint;         */                //初始化 NSPoint 结构体        NSPoint point=NSMakePoint(3, 4);        //输出        NSLog(@"%@",NSStringFromPoint(point));//以 NSString 格式输出 {3, 4}                        //从NSPoint获取x,y坐标        int xCoord=point.x;        int  yCoord=point.y;        NSLog(@"x=%i,y=%i",xCoord,yCoord);//输出 x=3,y=4                //NSSize的定义        /*         typedef struct _NSSize {         CGFloat width;//should never be negative         CGFloat height;// should never be negative         } NSSize;         */        //初始化 NSSize (表尺寸)        NSSize size=NSMakeSize(3.5, 4.5);        // 说明:3.5是width,4.5是height        NSLog(@"%@",NSStringFromSize(size));        //输出 {3.5, 4.5}        float width=size.width;        float height=size.height;        NSLog(@"width=%f,height=%f",width,height);        //输出 width=3.500000,height=4.500000                        //NSMakeRect 初始化        NSRect rect=NSMakeRect(0, 0, 20, 10);        NSLog(@"%@",NSStringFromRect(rect)); //输出 {{0, 0}, {20, 10}}        //说明{0,0}确定位置,{20,10} 确定尺寸大小      }    return 0;}



原创粉丝点击