黑马程序员----OC Foundation框架--结构体、NSString

                                          ------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------


一、结构体集合

1.NSRange:范围对字符串内容进行验证操作。

代码观之:

NSString *sr = @"i love oc";

//想输出love在字符串的范围

NSRange r = {2,4};//不用,可读性不好

NSRange r2 = { .location = 2,.length =4};//太冗余

NSRange r3 = NSMakeRange(2,4);//通常这样写

 

NSString *sr = @"i love oc";

NSRange range = [srtr rangOfString:@"love"];//查找某个字符串在str中的具体位置

NSLog(@"loc = %ld,length =%ld",range.location,rage.length);

//如果找不到length=0  location=NSNotFound==-1

 

2.NSPoint\CGPoint;点

使用这些CGPointEqualToPoint、CGRectContainsPoint等函数的前提是添加CoreGraphics框架

CGPoint p1 = NSMakePoint(10,10);

CGPoint p2 = CGPointMake(10,10);//常用

// 比较两个点是否相同(x、y)

BOOL b =CGPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10));

 

 

3.NSSize\CGSize;

用来表示UI的长 宽

CGSize p3 = NSMakeSize(100,30);

CGSize p4 = CGSizeMake(100,30);

 

4.NSRect\CGRect;

点尺寸

CGRect p5 = NSMakeSize(0,0,100,30);

CGRect p6 = NSMakeSize(p1,p3);

CGRect p7 = {{0,0},{100,80}}; 

//使用这些函数的前提是添加CoreGraphics框架

 #import <CoreGaphicd/CoreGraphice.h>

//表示原点

CGPointZero == CGPointMake(0,0); 

CGSizeZero == CGSizeMake(0,0);

CGRectZero=={0,0,0,0}

 

二.字符串

 

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];

 

2.NSMutableString(可变字符串) 继承 NSString (不可变)

 NSMutableString *s1=[NSMutableStringstringWithFormat:@"age is 10"];

[s1 appendFormat:@"11 12"];

//拼接内容到s1后面

[s1 deleteCharactersInRange(4,2)];//在s1字符串中第4个字符开始删除2个字符

//删除固定的字符串"is"

NSRange range = [s1rangeOfString:@"is"];

[s1 deleteCharacterInRange:range]

 

NSString *s2=[NSStringstringWithFormat:@"age is 10"];

//s2不可变字符串中就没有appendFormat方法;

NSString *s3 = [s2stringByAppendingString:@"11 12"];

//s2的内容复制一份拼接上 11 12,而且新创建的字符串给s3、s2内容不会改变的

 

NSSrring *s4 = [[NSString alloc] initWithFormat:@"ageis %d",10];

//c字符串---------》oc字符串

NSString *s5 = [ [NSString alloc]initWithUTF8String:"jack"];

//oc字符串-----------------------》c字符串

const char *s6=[s5 UTF8String];

 

//NSUTF8StringENcoding 用到中文就可以用这种编码

NSString *s7 = [[NSString alloc] initWithContentOfFile:@"路径"encoding:NSUTF8StringEncoding error:nil];

 

 

3.字符串的导出

    [@"Jack\nJack"writeToFile:@"/Users/apple/Desktop/my.txt" atomically:YESencoding:NSUTF8StringEncoding error:nil];

      

   NSString *str = @"4234234";

   NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/my2.txt"];

   [str writeToURL:url atomically:YES encoding:NSUTF8StringEncodingerror:nil];

}

 

//URL:资源路径

协议头://路径

//file://本地文件

//ftp://ftp服务器资源

 

NSURL *url = [[NSURL alloc]initWithString:@"file://.........."];

NSString *ss = [[NSString alloc]initWithContentOfURL:url encoding:NSUTF8StringEncoding error:nil];

这个方法什么资源都可以读取

一般都会有一个类方法跟对象方法配对

NSString *SS = [NSStringstringWithContentOfURL:[NSURL URLWithPath:@"file://.........." ]encoding:NSUTF8String error:nil]

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值