Objective-C基础笔记(7)Foundation中的常用结构体

一、NSRange

NSRange的定义:

typedef struct _NSRange {
    NSUInteger location;
    NSUInteger length;
} NSRange;
NSRanage的使用:

    NSRange range = NSMakeRange(8, 10);
    
    NSLog(@"location:%zi", range.location);
    NSLog(@"length:%zi", range.length);
    
    NSString *str = NSStringFromRange(range);
    NSLog(@"%@", str);
二、NSPoint

NSPoint的定义:

typedef CGPoint NSPoint;

struct CGPoint {
  CGFloat x;
  CGFloat y;
};
typedef struct CGPoint CGPoint;
NSPoint的使用:

    NSPoint p;
    p.x = 10;
    p.y = 10;
    
    p = NSMakePoint(11, 11);
    
    //常见的方式
    p = CGPointMake(12, 12);
    
    NSString * str = NSStringFromPoint(p);
    NSLog(@"%@", str);
三、NSSize

NSSize的定义:

typedef CGSize NSSize;

struct CGSize {
  CGFloat width;
  CGFloat height;
};
typedef struct CGSize CGSize;
NSSize的使用:

    NSSize s;
    s.width = 100.1;
    s.height = 100.3;
    
    s = NSMakeSize(12.3, 16.9);
    
    s = CGSizeMake(15.3, 19.2);
    
    NSString *str = NSStringFromSize(s);
    NSLog(@"%@", str);
四、NSRect

NSRect的定义:

typedef CGRect NSRect;

struct CGRect {
  CGPoint origin;
  CGSize size;
};
typedef struct CGRect CGRect;
NSRect的使用:

    NSRect r;
    //r.origin.x = 11;
    //r.origin.y = 13;
    r.origin = NSMakePoint(50, 50);
    r.size = NSMakeSize(60, 60);
    
    r = NSMakeRect(20, 20, 30, 30);

posted on 2014-11-17 22:43  岚之山  阅读(179)  评论(0编辑  收藏  举报

导航