OC基础-Foundation框架-0321-常见结构体和NSString

www.va======
//Foundation知识:常见结构体。。。NSString
</pre><pre name="code" class="objc">
<pre name="code" class="objc"><pre name="code" class="objc">#import <Foundation/Foundation.h>

int main()
{
/*
 NSRange(location length)
 NSPoint == CGPoint
 NSSize\CGSize
 NSRect\CGRect (CGPint CGSize)
 */
    
    // 使用这些CGPointEqualToPoint、CGRectContainsPoint等函数的前提是添加CoreGraphics框架
    //
    
    // NextStep  Foundation 
    
    
    // 比较两个点是否相同(x、y)
    BOOL b = CGPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10));
    //CGRectEqualToRect(<#CGRect rect1#>, <#CGRect rect2#>)
    //CGSizeEqualToSize(<#CGSize size1#>, <#CGSize size2#>)
    
    
    // x (50, 150) y (40 , 90)
    BOOL b2 = CGRectContainsPoint(CGRectMake(50, 40, 100, 50), CGPointMake(60, 45));
    
    NSLog(@"%d", b2);
    
    return 0;
}

void point()
{
    CGPoint p1 = NSMakePoint(10, 10);
***********************
    NSPoint p2 = CGPointMake(20, 20);// 最常用,类推其他结构体。
***********************
    
    NSSize s1 = CGSizeMake(100, 50);
    NSSize s2 = NSMakeSize(100, 50);
    CGSize s3 = NSMakeSize(200, 60);
    
    CGRect r1 = CGRectMake(0, 0, 100, 50);
    
    CGRect r2 = { {0, 0}, {100, 90}};
    
    CGRect r3 = {p1, s2};
    
    // 使用CGPointZero等的前提是添加CoreGraphics框架
    CGRect r4 = {CGPointZero, CGSizeMake(100, 90)};
    
    
    // CGSizeZero
    // CGRectZero
    
    // 表示原点,常量
    // CGPointZero == CGPointMake(0, 0)
    
    
    // 将结构体转为字符串(在开发中常常需要打印结构体。)
    //NSString *str = NSStringFromPoint(p1);
    
    //NSString *str = NSStringFromSize(s3);
    
    NSString *str = NSStringFromRect(r1);
    
    NSLog(@"%@", str);
    
    
    // NSLog(@"x=%f, y=%f, width=%f, height=%f", r1.origin.x, r1.origin.y, r1.size.width, r1.size.height);
}

//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;
//    
//    return rect;
//}

void range()
{
    // @"i love oc"  // love的范围
    
    //NSRange r1 = {2, 4}; // 不用
    //NSRange r2 = {.location = 2, .length = 4};// 不用
    //NSRange r3 = NSMakeRange(2, 4); // 掌握
    NSString *str = @"i love oc";
    
    // 查找某个字符串在str中的范围
    // 如果找不到,length=0,location=NSNotFound==-1
    NSRange range = [str rangeOfString:@"java"];
    NSLog(@"loc = %ld, length=%ld", range.location, range.length);
}
</pre><pre name="code" class="objc">
</pre><pre name="code" class="objc">
</pre><pre name="code" class="objc">
</pre><pre name="code" class="objc">
</pre><pre name="code" class="objc">
</pre><pre name="code" class="objc">
</pre><pre name="code" class="objc">
 
 
 ------------------------------------------------------------------------------------------------------------------------- 
<span style="font-family: Arial, Helvetica, sans-serif;">#import <Foundation/Foundation.h></span>
/*
 NSString : 不可变字符串
 
 NSMutableString : 可变字符串
 */

int main()
{
    
    NSMutableString *s1 = [NSMutableString stringWithFormat:@"my age is 10"];
    // 拼接内容到s1的后面
    [s1 appendString:@" 11 12"];
    
    // 获取is的范围
    NSRange range = [s1 rangeOfString:@"is"];
    [s1 deleteCharactersInRange:range];
    
    NSString *s2 = [NSString stringWithFormat:@"age is 10"];
    
    NSString *s3 = [s2 stringByAppendingString:@" 11 12"];
    
    
    NSLog(@"s1=%@, s2=%@", s1, s2);
    
    return 0;
}

void stringExport()
{
    // 字符串的导出
    [@"Jack\nJack" writeToFile:@"/Users/apple/Desktop/my.txt" atomically:YES 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];
}

void stringCreate()
{
    /*
     1.字符串的创建
     */
    NSString *s1 = @"jack";//最简单,直接创建
    
    //NSString *s2 = [[NSString alloc] initWithString:@"jack"];//几乎不用
    
    NSString *s3 = [[NSString alloc] initWithFormat:@"age is %d", 10];//按照格式创建%@
    
    // C字符串 --> OC字符串
    NSString *s4 = [[NSString alloc] initWithUTF8String:"jack"];
    // OC字符串 --> C字符串
    const char *cs = [s4 UTF8String];
    
    // NSUTF8StringEncoding 用到中文就可以用这种编码
    //获取文件中的内容
    NSString *s5 = [[NSString alloc] initWithContentsOfFile:@"/Users/apple/Desktop/1.txt" encoding:NSUTF8StringEncoding error:nil];
</pre><pre name="code" class="objc">
<pre name="code" class="objc"> <pre name="code" class="objc">    <span style="font-family: Arial, Helvetica, sans-serif;">//获取下面方法的参数值,类型为NSURL </span>
    // NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/apple/Desktop/1.txt"];//使用url,前面都要有协议
    NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/1.txt"];//NSURL类方法,这样默认是file协议,参数不用写协议。<pre name="code" class="objc"><pre name="code" class="objc">    //获取URL(资源文件)中的全部内容,返回值NSString 
 
 
 
 
<pre name="code" class="objc">    NSString *s6 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

 
</pre><pre name="code" class="objc">--------------------------------------NSString的6种创建方式----------------------------------------------------------------
    
    
    // URL : 资源路径
    // 协议头://路径
    // file://
    // ftp://
    // http://weibo.com/a.png
    
    
    // http://www.baidu.com
    

    //获取URL(资源文件)中的全部内容,返回值NSString
    // NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/apple/Desktop/1.txt"];//使用url,前面都要有协议
    NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/1.txt"];//NSURL类方法,这样默认是file协议,参数不用写协议
    
    NSString *s6 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"s6=\n%@", s6);
    
    
    /*
     一般都会有一个类方法跟对象方法配对
     [NSURL URLWithString:<#(NSString *)#>];//@"http://baidu.com"
     [NSString stringWithFormat:@""];//@"person's address is %@",Person;
     [NSString stringWithContentsOfFile:<#(NSString *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError *__autoreleasing *)#>];
<pre name="code" class="objc">     [NSString stringWithContentsOfURL:<#(NSURL *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError *__autoreleasing *)#>];
*/}
 
<pre name="code" class="objc">#import <Foundation/Foundation.h>

int main()
{
/*
 NSRange(location length)
 NSPoint\CGPoint
 NSSize\CGSize
 NSRect\CGRect (CGPint CGSize)
 */
    
    // 使用这些CGPointEqualToPoint、CGRectContainsPoint等函数的前提是添加CoreGraphics框架
    //
    
    // NextStep  Foundation 
    
    
    // 比较两个点是否相同(x、y)
    BOOL b = CGPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10));
    //CGRectEqualToRect(<#CGRect rect1#>, <#CGRect rect2#>)
    //CGSizeEqualToSize(<#CGSize size1#>, <#CGSize size2#>)
    
    
    // x (50, 150) y (40 , 90)
    BOOL b2 = CGRectContainsPoint(CGRectMake(50, 40, 100, 50), CGPointMake(60, 45));
    
    NSLog(@"%d", b2);
    
    return 0;
}

void point()
{
    CGPoint p1 = NSMakePoint(10, 10);
    NSPoint p2 = CGPointMake(20, 20);// 最常用
    
    NSSize s1 = CGSizeMake(100, 50);
    NSSize s2 = NSMakeSize(100, 50);
    CGSize s3 = NSMakeSize(200, 60);
    
    CGRect r1 = CGRectMake(0, 0, 100, 50);
    
    CGRect r2 = { {0, 0}, {100, 90}};
    
    CGRect r3 = {p1, s2};
    
    // 使用CGPointZero等的前提是添加CoreGraphics框架
    CGRect r4 = {CGPointZero, CGSizeMake(100, 90)};
    
    
    // CGSizeZero
    // CGRectZero
    
    // 表示原点
    // CGPointZero == CGPointMake(0, 0)
    
    
    // 将结构体转为字符串
    //NSString *str = NSStringFromPoint(p1);
    
    //NSString *str = NSStringFromSize(s3);
    
    NSString *str = NSStringFromRect(r1);
    
    NSLog(@"%@", str);
    
    
    // NSLog(@"x=%f, y=%f, width=%f, height=%f", r1.origin.x, r1.origin.y, r1.size.width, r1.size.height);
}

//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;
//    
//    return rect;
//}

void range()
{
    // @"i love oc"  // love的范围
    
    //NSRange r1 = {2, 4}; // 不用
    //NSRange r2 = {.location = 2, .length = 4};// 不用
    //NSRange r3 = NSMakeRange(2, 4); // 掌握
    NSString *str = @"i love oc";
    
    // 查找某个字符串在str中的范围
    // 如果找不到,length=0,location=NSNotFound==-1
    NSRange range = [str rangeOfString:@"java"];
    NSLog(@"loc = %ld, length=%ld", range.location, range.length);
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值