• [OC Foundation框架


    A. 不可变字符串

    复制代码
     1 void stringCreate()
     2 {
     3     //Don't need to release memory by this way
     4     NSString *str1 = @"String Sample";
     5    
     6     NSString *str2 = [[NSString alloc] init];
     7     str2 = @"String sample 2";
     8     [str2 release];
     9    
    10     NSString *str3 = [[NSString alloc] initWithString:@"String sample 3"];
    11     [str3 release];
    12     //Don't need to manage memory by static function
    13     str3 = [NSString stringWithString:@"A String"];
    14    
    15     NSString *str4 = [[NSString alloc] initWithUTF8String:"String sample4"];
    16     [str4 release];
    17     //No manual memory management, too
    18     str4 = [NSString stringWithUTF8String:"String 4"];
    19    
    20    
    21     NSString *str5 = [[NSString alloc] initWithFormat:@"My age is %i and height is %.2f", 19, 1.55];
    22     NSLog(@"String 5 is %@", str5);
    23     [str5 release];
    24 }
    复制代码
     
    B. 从文件读取字符串
    复制代码
     1 void readFromFile()
     2 {
     3     NSString *path = @"/Users/hellovoidworld/Study/Foundation/NSString/file.txt";
     4    
     5     //This function cannot read Chinese, is expired
     6 //    NSString *str1 = [NSString stringWithContentsOfFile:path];
     7    
     8     NSError *error;
     9    
    10     NSString *str1 = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    11    
    12     if (error == nil)
    13     {
    14         NSLog(@"Read successfully, is %@", str1);
    15     }
    16     else
    17     {
    18         NSLog(@"Read fail, error is %@", error);
    19     }
    20 }
    复制代码
     
    C. 从URL读取
    1 void readFromUrl()
    2 {
    3     NSURL *url = [NSURL URLWithString:@"file:///Users/hellovoidworld/Study/Foundation/NSString/file.txt"];
    4     NSString *str2 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    5     NSLog(@"Read url successfully, is %@", str2);
    6 }
    如果一件事情你觉得难的完不成,你可以把它分为若干步,并不断寻找合适的方法。最后你发现你会是个超人。不要给自己找麻烦,但遇到麻烦绝不怕,更不要退缩。 电工查找电路不通点的最快方法是:分段诊断排除,快速定位。你有什么启示吗? 求知若饥,虚心若愚。 当你对一个事情掌控不足的时候,你需要做的就是“梳理”,并制定相应的规章制度,并使资源各司其职。
  • 相关阅读:
    BiliBili, ACFun… And More!【递归算法】
    【VS2015】关于VS2015如何运行的问题
    【打死树莓派】-树莓派3代jessie+Opencv-解决安装不了libgtk2.0-dev包问题
    插入排序2.0
    【C++小白成长撸】--(续)单偶数N阶魔方矩阵
    【C++小白成长撸】--(续)双偶数N阶魔阵
    安装 python-opencv
    二叉树打印
    Kotlin接口
    Kotlin 继承
  • 原文地址:https://www.cnblogs.com/wvqusrtg/p/4504000.html
Copyright © 2020-2023  润新知