美文网首页
1.Foundation框架的学习

1.Foundation框架的学习

作者: 峰子1994 | 来源:发表于2017-10-28 14:47 被阅读1次

    前言

    1.Foundation基础
    2.framework框架

    1.Foundation框架的结构关系:

    image.png image.png

    2.Foundation基础:

    OC和C之间的转换:

     1 int main(int argc, const char * argv[]) {
     2     @autoreleasepool {
     3         //C语言字符串
     4         char *ch = "Hello objective-c";
     5         NSString *ns = @"Hello c";
     6         //OC、C语言之间的转换
     7         //C语言->OC语言
     8         NSString *oc = [NSString stringWithUTF8String:ch];
     9         NSLog(@"转为oc语言的字符串:%@",oc);
    10         //OC语言->C语言
    11         const char *c = [ns UTF8String];
    12         NSLog(@"转为c语言的字符串:%s",c);
    13     }
    14     return 0;
    15 }
    

    NSString的操作:

     1  int main(int argc, const char * argv[]) {
     2     @autoreleasepool {
     3         //创建字符串
     4         NSString *str1 = @"Hello world1!";
     5         NSString *str2 = [[NSString alloc] init];
     6         str2 = @"Hello world2";
     7         //格式化字符串
     8         int a = 10;
     9         int b = 20;
    10         NSLog(@"%@",[NSString stringWithFormat:@"a=%d, b=%d",a,b]);
    11         //拼接字符串
    12         NSString *str3 = [str1 stringByAppendingString:str2];
    13         NSLog(@"%@",str3);
    14         //大小写转换
    15         NSString *str4 = @"ABCDEF";
    16         NSString *str5 = [str4 lowercaseString];
    17         NSLog(@"%@",str5);
    18         NSString *str6 = [str5 uppercaseString];
    19         NSLog(@"%@",str6);
    20         //前缀后缀的判断
    21         NSString *str7 = @"www.goodboy.heyang";
    22         BOOL hasPreFix = [str7 hasPrefix:@"www"];
    23         if (hasPreFix) {
    24             NSLog(@"str7有 www 的前缀。");
    25         }
    26         BOOL hasSufFix = [str7 hasSuffix:@"heyang"];
    27         if (hasSufFix) {
    28             NSLog(@"str7有 heyang 后缀。");
    29         }
    30     }
    31     return 0;
    32 }
    

    NSString比较字符串:

     1 int main(int argc, const char * argv[]) {
     2     @autoreleasepool {
     3         //判断两个字符串是否相同
     4         NSString *str1 = @"Hello";
     5         NSString *str2 = @"Hello";
     6         if ([str1 isEqualToString:str2]) {
     7             NSLog(@"两个字符是一致的。");
     8         }else{
     9             NSLog(@"不一致!");
    10         }
    11         
    12         //比较字符串
    13         //NSComparisonResult需要自己去查阅相关的资料
    14         
    15         //分割,按照指定字符分割字符串
    16         NSString *str3 = @"a,b,c,d,e,f,g";
    17         NSArray *strArray = [str3 componentsSeparatedByString:@","];
    18         for(NSString *str in strArray){
    19             NSLog(@"str = %@",str);
    20         }
    21         /*验证正则表达式失败
    22         NSString *str4 = @"1+2-3/9";
    23         NSArray *strArray2 = [str4 componentsSeparatedByString:@"[\+\-\*\/]"];
    24         for(NSString *str in strArray2){
    25             NSLog(@"str = %@",str);
    26         }
    27         */
    28         //按照范围截取字符串,注意这里是结构体实例,所以不需要指针
    29         NSRange range = NSMakeRange(1, 5);//前面是截取的起点,后面是长度,于是截取的是1~4
    30         NSString *str5 = [str3 substringWithRange:range];
    31         NSLog(@"str5 = %@",str5);
    32         //从某一位开始截取后面的字符串
    33         NSString *str6 = [str3 substringFromIndex:2];
    34         NSLog(@"str6 = %@",str6);
    35         //从开始截取到某一位
    36         NSString *str7 = [str3 substringToIndex:2];
    37         NSLog(@"str7 = %@",str7);
    38         
    39         //将字符串拆分为每一个字符
    40         for (int i = 0; i<[str3 length]; i++) {
    41             NSLog(@"%c ",[str3 characterAtIndex:i]);
    42         }
    43     }
    44     return 0;
    45 }
    

    相关文章

      网友评论

          本文标题:1.Foundation框架的学习

          本文链接:https://www.haomeiwen.com/subject/tncspxtx.html