OC 常用结构体

来源:互联网 发布:看漫画学日语知乎 编辑:程序博客网 时间:2024/04/20 06:23

NSRange\\CGRange

A structure used to describe a portion of a series—such as characters in a string or objects in an NSArray object.


Declaration
SWIFT

typealias NSRange = _NSRange


OBJECTIVE-C
typedef struct _NSRange {
      NSUInteger location;
      NSUInteger length;
} NSRange;


Discussion
Foundation functions that operate on ranges include the following:


NSEqualRanges
NSIntersectionRange
NSLocationInRange
NSMakeRange
NSMaxRange
NSRangeFromString
NSStringFromRange
NSUnionRange

eg:

NSRange range0 = NSMakeRange(2,4);
NSString *str = @"i love oc";
NSRange range = [str rangeOfString:@"java"];
NSLog(@"loc = %ld, length = %ld",range.location,range.length);
NSRange range2 = [str rangeOfString:@"oc"];
NSLog(@"loc = %ld,length = %ld",range2.location,range2.length);

NSPoint\CGPoint

Represents a point in a Cartesian coordinate system.


Declaration
SWIFT

typealias NSPoint = CGPoint


OBJECTIVE-C
typedef struct _NSPoint {
      CGFloat x;
      CGFloat y;
} NSPoint;


Import Statement
OBJECTIVE-C

@import Foundation;


SWIFT
import Foundation


eg1:
CGPoint p1 = NSMakePoint(10,10);
NSPoint p2 = CGPointMake(20,20);//usual
CGPointZero == CGPointMake(0,0);//It represents the origin


eg2:
//使用这些CGPointEqualToPoint、CGRectContainsPoint等函数的前提是添加CoreGraphics框架
BOOL b = CGPointEqualToPoint(CGPointMake(10,10),CGPointMake(10,10));

NSSize\CGSize

Represents a two-dimensional size.


Declaration
SWIFT

typealias NSSize = CGSize


OBJECTIVE-C
typedef struct _NSSize {
      CGFloat width;
      CGFloat height;
} NSSize;


Discussion
Normally, the values of width and height are non-negative(非负). The functions that create an NSSize structure do not prevent you from setting a negative value for these attributes. If the value of width or height is negative, however, the behavior of some methods may be undefined.


Import Statement
OBJECTIVE-C
@import Foundation;
SWIFT
import Foundation


eg1:
NSSize s1 = CGSizeMake(100, 50);
NSSize s2 = NSMakeSize(100, 50);
CGSize s3 = NSMakeSize(200, 60);

NSRect\CGRect

Represents a rectangle.


Declaration
SWIFT

typealias NSRect = CGRect


OBJECTIVE-C
typedef struct _NSRect {
      NSPoint origin;
      NSSize size;
} NSRect;


Import Statement
OBJECTIVE-C

@import Foundation;


SWIFT
import Foundation


eg1:
CGPoint p1 = NSMakePoint(10, 10);
NSPoint p2 = CGPointMake(20, 20);
CGRect r1 = CGRectMake(0, 0, 100, 50);   
CGRect r2 = { {0, 0}, {100, 90}};   
CGRect r3 = {p1, s2};
eg2:
// 使用CGPointZero等的前提是添加CoreGraphics框架
CGRect r4 = {CGPointZero, CGSizeMake(100, 90)};




// 将结构体转为字符串
//NSString *str = NSStringFromPoint(p1);   
//NSString *str = NSStringFromSize(s3);
NSString *str = NSStringFromRect(r1);   
NSLog(@"%@", str)


附带代码:http://download.csdn.net/detail/goodboy_wkx/8685319






























0 0