標籤:des style http color io ar 使用 java for
結構體
NSRange:
用來表示範圍
建立
NSRange r1 = {2, 4}
NSRange r2 = {.location = 2, .length = 4}
NSRange r3 = NSMakeRange(2, 4)
尋找某個字串在str中的範圍
NSString *str = @"i love oc";
NSRange range = [str rangeOfString:@"java"]
NSLog(@"location = %ld, length=%ld", range.location, range.length)
如果找不到,length=0,location=NSNotFound==-1
NSPoint:
用來表示一個點的座標
建立
CGPoint p1 = NSMakePoint(10, 10);
NSPoint p2 = CGPointMake(20, 20);
表示原點
CGPointZero == CGPointMake(0, 0)
NSSize:
表示二維平面的尺寸
NSSize s1 = CGSizeMake(100, 50);
NSSize s2 = NSMakeSize(100, 50);
CGSize s3 = NSMakeSize(200, 60);
CGRect:
表示二維平面具體的一個尺寸和位置
CGRect r1 = CGRectMake(0, 0, 100, 50);
CGRect r2 = { {0, 0}, {100, 90}};
CGRect r3 = {p1, s2};
定義CGRect的另外方法
CGRect myRect(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
CGRect rect;
rect.origin.x = x;
rect.origin.y = y;
rect.size.width = width;
rect.size.height = height;
}
將結構體轉為字串
NSString *str = NSStringFromPoint(p1);
NSString *str = NSStringFromSize(s3);
NSString *str = NSStringFromRect(r1);
比較兩個點是否相同(x、y)
BOOL b = CGPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10))
判斷一個點是否在某個地區
BOOL b2 = CGRectContainsPoint(CGRectMake(50, 40, 100, 50), CGPointMake(60, 45))
使用這些CGPointEqualToPoint、CGRectContainsPoint等函數的前提是添加CoreGraphics架構
NSString
字串的建立
NSString *s1 = @"jack";
NSString *s2 = [[NSString alloc] initWithString:@"jack"];
NSString *s3 = [[NSString alloc] initWithFormat:@"age is %d", 10];
C字串 > C字串
const char *c = [s4 UTF8String];
NSUTF8StringEncoding 用到中文就可以用這種編碼
NSString *s5 = [[NSString alloc] initWithContentsOfFile:@"/Users/apple/Desktop/1.txt" encoding:NSUTF8StringEncoding error:nil];
字串的匯出
NSString *str = @"4234234";
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/my2.txt"];
[str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
或者
[@"Jack\nJack" writeToFile:@"/Users/apple/Desktop/my.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil]
NSMutableString
可變字串
NSMutableString *s1 = [NSMutableString stringWithFormat:@"my age is 10"];
拼接內容到s1的後面
[s1 appendString:@" 11 12"];
擷取is的範圍
NSRange range = [s1 rangeOfString:@"is"];
刪除is
[s1 deleteCharactersInRange:range];
不可變字串也可以將兩個字串拼接
NSString *s2 = [NSString stringWithFormat:@"age is 10"];
NSString *s3 = [s2 stringByAppendingString:@" 11 12"];
補充知識:NSURL
file:// 本地資源協議頭
http:// 網路資源協議頭
建立一個資源路徑
NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/apple/Desktop/1.txt"] 本地資源
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/1.txt"] 本地資源
將資源路徑轉化為字串
NSString *s6 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]
黑馬程式員_ Objective-c 之Foundation筆記(一)