• [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 }
    如果一件事情你觉得难的完不成,你可以把它分为若干步,并不断寻找合适的方法。最后你发现你会是个超人。不要给自己找麻烦,但遇到麻烦绝不怕,更不要退缩。 电工查找电路不通点的最快方法是:分段诊断排除,快速定位。你有什么启示吗? 求知若饥,虚心若愚。 当你对一个事情掌控不足的时候,你需要做的就是“梳理”,并制定相应的规章制度,并使资源各司其职。
  • 相关阅读:
    Web3与智能合约交互实战
    详解 Solidity 事件Event
    iOS App迁移(App Transfer)注意点
    IDFA踩坑记录
    iOS error: -34018
    Apple 的命令行交付工具“Transporter”
    关于iOS UIWebView 加载网页,点击网页内某些控件导致 Application 'UIKitApplication:xxx.xxx.xxx' was killed by jetsam.
    苹果应用内支付详解以及如何预防刷单等行为
    iOS “弱账号” 暗转 “强账号”
    好用的敏捷开发软件推荐
  • 原文地址:https://www.cnblogs.com/wvqusrtg/p/4504000.html
Copyright © 2020-2023  润新知