`
jishublog
  • 浏览: 865761 次
文章分类
社区版块
存档分类
最新评论

ios之NSString的用法

 
阅读更多

关于NSString的用法,,具体见以下代码,该注释的地方都已经注释了。

//
//  main.m
//  NSString
//
//  Created by mj on 13-4-5.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

#pragma mark NSString的创建
void stringCreate() {
    // char *s = "A String!"; // C语言中的字符串
    
    // 这种方式创建出来的字符串是不需要释放的
    NSString *str1 = @"A String!";
    
    NSString *str2 = [[NSString alloc] init];
    str2 = @"A String!";
    [str2 release];
    
    NSString *str3 = [[NSString alloc] initWithString:@"A String!"];
    [str3 release];
    // 静态方法,不需要管理内存
    str3 = [NSString stringWithString:@"A String!"];
    
    
    NSString *str4 = [[NSString alloc] initWithUTF8String:"A String!"];
    [str4 release];
    str4 = [NSString stringWithUTF8String:"A String!"];
    
    NSString *str5 = [[NSString alloc] initWithFormat:@"My age is %i and height is %.2f", 19, 1.55f];
    
    // 这句代码放在中间会造成2个错误:
    // 1.前面创建的字符串没有被释放
    // 2.后面创建的字符串会释放过度,造成野指针错误
    // str5 = [NSString stringWithFormat:@"My age is %i and height is %.2f", 19, 1.55f];
    
    NSLog(@"str5:%@", str5);
    [str5 release];
    
    str5 = [NSString stringWithFormat:@"My age is %i and height is %.2f", 19, 1.55f];
}

void test(NSString **str) {
    *str = @"123";
    // s = @"123";
}

void stringCreate2() {
    // 从文件中读取文本
    NSString *path = @"/Users/apple/Desktop/test.txt";
    // 这个方法已经过期,不能解析中文
    // NSString *str1 = [NSString stringWithContentsOfFile:path];
    
    // 定义一个NSError变量
    NSError *error;
    // 指定字符串编码为UTF-8: NSUTF8StringEncoding,当读取失败时,,系统会把错误信息写入error变量中,所以要传入error的地址。
    NSString *str1 = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    if (error == nil) { // 没有错误信息
        NSLog(@"读取文件成功:%@", str1);
    } else {
        NSLog(@"读取文件失败:%@", error);
    }
    
    //用URL方式读取文件,注意file:后面是3个斜杠
    NSURL *url = [NSURL URLWithString:@"file:///Users/apple/Desktop/test.txt"];
    NSString *str2 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@", str2);
    
    //远程URL方式读取文件
    NSURL *url2 = [NSURL URLWithString:@"http://www.baidu.com"];
    NSString *str3 = [NSString stringWithContentsOfURL:url2 encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@", str3);
}

#pragma mark 字符串的导出
void stringExport() {
    NSString *str = @"123456我是字符串!!!!";
    // 如果文件不存在,会自动创建文件
    // 如果文件夹不存在,会直接报错
    NSString *path = @"/Users/apple/Desktop/abc.txt";
    
    NSError *error;
    // 编码指定错误也会报错
    // YES代表要进行原子性操作,也就是会创建一个中间的临时文件
    [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        // [error localizedDescription]会返回主要的错误信息
        NSLog(@"写入失败:%@", [error localizedDescription]);
    } else {
        NSLog(@"写入成功");
    }
}

//void test2(int *p) {
//    *p = 9;// a = 9;
//}

int main(int argc, const char * argv[])
{

    @autoreleasepool {
//        int a = 10;
//        test2(&a);
//        NSLog(@"%i", a);
        
        stringExport();
        
//        NSString *s = @"456";
//        
//        test(&s);
//        
//        NSLog(@"%@", s);
    }
    return 0;
}




分享到:
评论

相关推荐

    iOS单利用法举例

    iOS单利用法举例 { NSString *_str; } @property(nonatomic,copy)NSString *str;

    ZYChangeCode:iOS简繁转换代码

    iOS简繁转换ZYChangeCode ###用法:简体转繁体: NSString *traditionalChineseString=[@"简体中文" totoTraditionString];繁体转简体: NSString *simplifiedChineseString=[@"繁體中文" totoTraditionString];根据...

    IOS 中runtime使用方法整理

    IOS 中runtime使用方法整理 做iOS的朋友都知道或听说runtime,这个东西很像java的反射机制,但功能远胜于java的反射。通过runtime我们可以动态的向一个类中添加属性、成员变量、方法,以及对其进行读写访问。 新建两...

    iOS下json解析工具

    iOS下解析json数据,自己亲自使用过,挺好用的。 使用JSONKit的解析方法:(需导入包:#import "JSONKit/JSONKit.h") - (void)printJson{ //如果json是“单层”的,即value都是字符串、数字,可以使用...

    ios WKWebView 的使用和交互

    本demo是WKWebView的基本使用和交互 ,实现了原生调用js的方法、js调用原生的方法、通过拦截进行交互的方法;修改内容 加入沙盒 / /加载沙盒 不带参数 // NSArray * paths = NSSearchPathForDirectoriesInDomains...

    iOS开发教程之APP内部切换语言的实现方法

    国际化都会走到NSBundle的- (NSString *)localizedStringForKey:(NSString *)key value:(nullable NSString *)value table:(nullable NSString *)tableName方法,使用自定义Bundle替换NSBundle,通过切换根控制器来...

    详解IOS宏与常量的使用(define,const)

    小编给大家整理了关于IOS中宏(define)与常量(const)的正确使用方法,有助于大家更加深入的理解这方面的内容。 当我们想全局共用一些数据时,可以用宏、变量、常量 宏: #define HSCoder @”汉斯哈哈哈” 变量: ...

    ios-AlertView.zip

    用法: 将封装的View导入。然后按照demo中的写就可以。 下方主要使用代码: 这是添加的方法: ConfigueAlertView *alertView = [[ConfigueAlertView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ...

    ios-带输入栏的键盘.zip

    使用方法:LTInputAccessoryView* view = [LTInputAccessoryView new]; switch (sender.tag) { case 0:{ //打开默认键盘 [view showBlock:^(NSString *contentStr) { [self showAlertStr:contentStr]; }]...

    ios开发小技巧

    iOS开发之UIlabel多行文字自动换行 (自动折行) UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(10, 100, 300, 180)]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, ...

    iOS之加载Gif图片的方法

    但是iOS竟然没有现成的支持加载和播放Gif的类。 简单地汇总了一下,大概有以下几种方法: 一、加载本地Gif文件 1、使用UIWebView // 读取gif图片数据 UIWebView *webView = [[UIWebView alloc] initWithFrame:...

    GTMBase64编解码

    使用方法: 1)丢入ios项目。 【注意】开启ARC的同学注意 解决方法:-fno-objc-arc 2)在要使用GTMBase64的地方#import "GTMBase64.h"引入头文件 3)下面详细说明: 常用的方法,有下面几个: (NSString*)md5_...

    IOS 静态方法与动态方法详解

    IOS 静态方法与动态方法详解 1、问题提出  iOS中有静态方法与动态方法,那么两种方法的异同是什么? 2、问题分析  因为每个对象都由相应的数据结构与方法相构成,一个程序可能有多个属于同一个类的对象,而每个...

    YYAsyncLayer, 用于异步呈现和显示的iOS实用工具类.zip

    YYAsyncLayer, 用于异步呈现和显示的iOS实用工具类 YYAsyncLayer 用于异步呈现和显示的iOS实用工具类。( 它被 YYText 使用)简单用法@interface YYLabel : UIView@property NSString *

    ios xpath html 解析 lib包

    下面为方法,xpath路径可专门另研究。 NSData *htmlData = [[response dataUsingEncoding:NSUTF8StringEncoding] retain]; TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData]; NSArray *...

    ios常用动画封装类

    * 当上面的预置不能满足你的需求的时候,你可以使用下面的两个方法来自定义你的timingFunction * 具体参见下面的URL * * @see ...

    MGFormatter:在iOS上运行的JSON字符串格式化程序

    要求iOS 8.0以上Xcode 9.0以上用法只需创建一个FormatterView并设置要设置格式的代码即可。 formatterView. format ( string : utf8Text, style : . jsonDark )关于风格可以按照开发者的要求定制样式。 格式化程序...

    ios-网络加载器.zip

    使用方法 只有hud隐藏后,才会去创建新的hud,否则一直复用一个hud,至于那个- (void)showTitle:(NSString *)title navigationBar:(BOOL)hiden; 是为了自己项目里加,防止位置错乱 /** * 只带文字提示 * * @...

    ios-封装系统的alert--好用的alert.zip

    使用方法:case 0: [LTAlertView showTitle:@"LTAlert" message:@"我是普通alert" ButtonTitles:@[@"确认",@"取消"] OnTapBlock:^(LTAlertView* alert,NSInteger num) { NSLog(@"点击了第%d个按钮",num); }]; ...

    iOS评论弹框封装

    使用方法: [[MSCommentView sharedManager] show]; // 点击发送按钮block [[MSCommentView sharedManager] ms_commentViewSendMessageBlock:^(NSString *contentStr) { NSLog(@"发送内容为:%@",contentStr)...

Global site tag (gtag.js) - Google Analytics