zoukankan      html  css  js  c++  java
  • Foundation框架: 6.NSString的创建和导出

    在前面我们学完了Foundation框架里最常用的四个结构体, 现在我们来讲讲最常用的类, 这次讲的是OC中的字符串---->NSString, 下面让我们来看看NSString的创建方式以及运用:




    例子:

    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
        // 1.最简单的字符串创建方式
        NSString *s = @"rose";
        
        // 2.使用alloc init方式创建, 但这种创建方式没有意义, 所以一般开发中是没有人会这么写.
        NSString *s1 = [[NSString alloc] initWithString:@"rose"];
        
        // 3.使用initWithFormat:创建
        NSString *s2 = [[NSString alloc] initWithFormat:@"name is %@", @"rose"];
        
        // 4.将C语言字符串转换成OC字符串
        NSString *s3 = [[NSString alloc] initWithUTF8String:"rose"];
        
        // 5.将OC字符串转成C语言字符串
        const char *cs = [s3 UTF8String];
        
        return 0;
    }

    PS: 除去第2个方法, 其他方法都是我们在实际开发中会遇到的创建方式.




    1. NSString读取本地文件的实例方法:

    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
        
        NSString *str = [[NSString alloc] initWithContentsOfFile:@"/Users/Cain/Desktop/1.txt" encoding:NSUTF8StringEncoding error:nil];
        
        NSLog(@"%@", str);
        
        return 0;
    }
    

    打印出来的结果:
    2015-02-03 19:02:46.325 2.NSString[610:18063] 123456789
    




    2. NSString读取本地文件的类方法:
    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
        // 1.最简单的字符串创建方式
        NSString *s = @"rose";
        
        // 2.使用alloc init方式创建, 但这种创建方式没有意义, 所以一般开发中是没有人会这么写.
        NSString *s1 = [[NSString alloc] initWithString:@"rose"];
        
        // 3.使用initWithFormat:创建
        NSString *s2 = [[NSString alloc] initWithFormat:@"name is %@", @"rose"];
        
        // 4.将C语言字符串转换成OC字符串
        NSString *s3 = [[NSString alloc] initWithUTF8String:"rose"];
        
        // 5.将OC字符串转成C语言字符串
        const char *cs = [s3 UTF8String];
        
        return 0;
    }

    打印出来的结果:
    2015-02-03 19:02:46.334 2.NSString[610:18063] 123456789
    




    3. NSString使用URL获取资源的方法:
    #import <Foundation/Foundation.h>
    
    
    int main(int argc, const char * argv[]) {
        // URL: 资源路径
        // 协议头: //路径
        // file://
        // ftp://
        NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/Cain/Desktop/2.txt"];
        
        NSString *str2 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
        
        NSLog(@"%@", str2);
        
        return 0;
    }

    打印出来的结果:
    2015-02-03 19:02:46.334 2.NSString[610:18063] 123456789
    




    4. NSString写入本地文件的方法:
    #import <Foundation/Foundation.h>
    
    
    int main(int argc, const char * argv[]) {
        
        NSString *str1 = @"abcdefg";
        NSError *error = nil;
        
        [str1 writeToFile:@"/Users/Cain/Desktop/2.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error];
        if (error) {
            NSLog(@"写入失败:%@", [error localizedDescription]);
        } else {
            NSLog(@"写入成功, 写入的内容是:%@", str1);
        }
        
        return 0;
    }

    打印出来的结果;
    2015-02-03 19:19:14.518 2.NSString[643:24821] 写入成功, 写入的内容是:abcdefg
    






    好了, 熟悉了上面的这些方法后, 其他的方法大家都可以慢慢摸索出来~这次就讲到这里, 下次我们继续~~~

  • 相关阅读:
    如何在一个for语句中迭代多个对象(2.7)
    yield列表反转 islice切片(2.6)
    yield和生成器, 通过斐波那契数列学习(2.5)
    python实现线程池(2.4)
    LOJ 3120: 洛谷 P5401: 「CTS2019 | CTSC2019」珍珠
    瞎写的理性愉悦:正整数幂和与伯努利数
    bzoj 3328: PYXFIB
    LOJ 3119: 洛谷 P5400: 「CTS2019 | CTSC2019」随机立方体
    洛谷 P5345: 【XR-1】快乐肥宅
    LOJ 3089: 洛谷 P5319: 「BJOI2019」奥术神杖
  • 原文地址:https://www.cnblogs.com/iOSCain/p/4282810.html
Copyright © 2011-2022 走看看