Object-c------Foundation中的结构体和字符串
2016-05-06 18:30
423 查看
main.m
// // main.m // Foundation // // Created by zhaogang on 16/5/6. // Copyright © 2016年 zhaogang. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { NSError *error = nil; NSString *path = @"/Users/zzzigo/Desktop/test.txt"; NSString *str = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; NSLog(@"%@", str); //同样url也可以用,同之前的写入 //[NSString alloc] initWithContentsOfURL:<#(nonnull NSURL *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError * _Nullable __autoreleasing * _Nullable)#> return 0; } void export() { NSString *str = @"jack"; NSString *path = @"/Users/zzzigo/Desktop/test.txt"; NSError *error = nil; //写入文件 //[str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]; //也是写入一个文件 //NSURL *url = [NSURL URLWithString:@"file:///Users/zzzigo/Desktop/test.txt"]; NSURL *url = [NSURL fileURLWithPath:@"/Users/zzzigo/Desktop/test.txt"]; [str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:&error]; if(error) NSLog(@"write fail! error = %@ !" , [error localizedDescription]);//error else NSLog(@"write sucess"); } void mutableStr() { //NSString是不可变字符串 //可变字符 NSMutableString *str = [[NSMutableString alloc] initWithCapacity:0]; [str appendFormat:@"123"]; NSString *str1 = @"123"; //追加一个字符串 NSString *str2 = [str1 stringByAppendingString:@"aaa"]; //找到一个字符串的范围 NSRange range3 = [str1 rangeOfString:@"3"]; //用字符串的范围删除这个字符串 [str deleteCharactersInRange:range3]; //插入字符串 [str insertString:@"aaa" atIndex:0]; //替换 [str replaceOccurrencesOfString:@"3" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, str.length) ]; NSLog(@"%@",str); } void stringis() { NSString *str = @"qq=444381246&pwd=jiuge1"; //检测是否以qq=开头 [str hasPrefix:@"qq="]; //检测是否以jiuge1结尾 [str hasSuffix:@"jiuge1"]; } void test() { //这两个是等价的 NSString *str = @"jack"; NSString *str1 = [[NSString alloc] initWithString:@"jack"]; //类似于c里面的sprintf NSString *str2 = [[NSString alloc] initWithFormat:@"%d, %f",10 , 1.f]; // http://im.qq.com?qq=123&pwd=123 NSString *qqURL = @"http://im.qq.com?qq=444381246&pwd=jiuge1"; //取得qq在url里面的范围 NSRange qqRange = [qqURL rangeOfString:@"qq="]; //或者 qqRange.location == NSNotFound if(qqRange.length) NSLog(@"len = %zd, loc = %zd",qqRange.length, qqRange.location); else NSLog(@"nothing!"); NSRange pwdRange = [qqURL rangeOfString:@"&pwd="]; //取出账号的范围 NSRange range = NSMakeRange(qqRange.location + qqRange.length, pwdRange.location - (qqRange.length + qqRange.location)); //账号 //根据range切割字符串 NSString *qqId = [qqURL substringWithRange: range]; NSLog(@"qqId = %@", qqId); //密码 //根据index切割字符串 NSString *qqPwd = [qqURL substringFromIndex:pwdRange.location + pwdRange.length]; NSLog(@"qqPwd = %@", qqPwd); //1.将字符串变为大小写字母, NSString *str3 = nil; //str =[qqPwd lowercaseString]; str = [qqPwd uppercaseString]; //2.两个字符串比较 if([str isEqualToString:@"JIUGE1"]) NSLog(@"pwd is right!"); else NSLog(@"pwd is wrong!"); } void range() { //表示一个范围,比如@"12345" NSRange range = {1, 2}; //表示字符串里的 2 3 NSRange range2 = {.location = 1, .length = 3};//表示字符串里的 2 3 4 NSRange range3 = NSMakeRange(1, 3); //表示字符串里的 2 3 4 } void point() { //NSPoint CGPoint 表示平面里面的一个点 CGPoint pt = {1, 2}; CGPoint pt1 = {.x = 1, .y = 2}; CGPoint pt2 = NSMakePoint(1, 3); } void size() { //大小,有宽和高 //NSSize CGSize CGSize size = NSMakeSize(10, 100); //前两个参数表示坐标 CGRect rect = NSMakeRect(0, 0, 100, 100); }
相关文章推荐
- 使用PHPExcel 时报错ZipArchive::getFromName(): Invalid or uninitialized Zip object
- iOS之'mutating method sent to immutable object'
- error LNK2019: 无法解析的外部符号 __imp_DeleteObject
- Programming with Objective-C(五)
- 关于List<Map<String,Object>>的通俗解释
- JS中六种数据类型(六)——Object
- 将json格式的字符串转化成object对象
- Object-c------protocol
- Object-c------block
- 解决ImportError: libmysqlclient_r.so.16: cannot open shared object file
- objective-c - 基础篇 - UIWebView
- Hibernate:deleted object would be re-saved by cascade (remove deleted object from associations): []
- Object-c字符串操作
- 【Json】关于json解析时异常org.json.JSONException: A JSONObject text must begin with '{' at character 1 of {的解决方法
- Orocos DataPort 解析: orocos lock free data object
- inside the c++ object
- Objective-C的属性和成员变量用法及关系浅析
- iPhone开发:Objective C 代码规范-iOS总结版
- JsonConvert.DeserializeObject 的用法
- Objective-C的缓存框架EGOCache在iOS App开发中的使用