zoukankan      html  css  js  c++  java
  • iOS-Foundation各种NS

    1.1 NSRange

    NSRange range = NSMakeRange(2, 4);//location=2,len=4

       NSString *str = @"i love oc";
    //查找对应的字符串的位置location,length
    range = [str rangeOfString:@"love"];
    4.2 NSMutableString

    // NSMutableString可变字符串

    NSMutableString *s1 = [NSMutableString stringWithFormat:@"age is 10"];

    [s1 appendString:@"love"];

     NSRange range = [s1 rangeOfString:@"love"];

    [s1 deleteCharactersInRange:range];

    4.3 NSString

    //类方法构造字符串

    NSString *str = [NSString stringWithFormat:@"age is %d",10];

    NSString *str1 = [NSString stringWithContentsOfURL:url 
    encoding:NSUTF8StringEncoding error:nil];

     //对象方法构造字符串

    NSString *str1 = [[NSString alloc]initWithContentsOfFile:@"/Users/zhangct/Desktop/1.txt"encoding:NSUTF8StringEncoding error:nil];

     NSString *url = @"file:///Users/zhangct/Desktop/1.txt";

    NSString *str2 = [[NSString alloc]initWithContentsOfURL:urlencoding:NSUTF8StringEncoding error:nil];

     //讲字符串写入文件

    [str2 writeToFile]

     //常用属性
    [s length]
    [s characterAtIndex:i]
     //结构体类型转换成字符串
    NSStringFromCGRect(<#CGRect rect#>)
    NSStringFromCGPoint(<#CGPoint point#>)
    4.4 NSString (NSExtendedStringDrawing)
    //根据字体计算字符串的显示宽高
    NSString *nikeName = @"内涵段子";
    NSDictionary *attri = @{NSFontAttributeName: [UIFont systemFontOfSize:13]};
    CGSize nikeSize = [nikeName boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) 
    options:NSStringDrawingUsesLineFragmentOrigin attributes:attri context:nil].size;
    4.5 NSBundle
    //获取文件绝对路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
    //加载xib试图文件,返回对象数组
    NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"HMAppView" owner:nil options:nil];
    // infoDictionary获取info.plist的字典信息
    NSDictionary *dict = [[NSBundle mainBundle] infoDictionary];
    // objectForInfoDictionaryKey获取info.plist字典中某一个键值
    NSString *versonKey = (__bridge NSString *)kCFBundleVersionKey;
    NSString *verson = [[NSBundle mainBundle] objectForInfoDictionaryKey:versonKey];
    1.2 NSArray和NSMutableArray
    //读取文件内容填充NSArray集合
    NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
     //向数组中每个元素发送SEL消息
    array makeObjectsPerformSelector:@selector(<#selector#>)
    [self.answerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    1.3 NSSet
    //获取集合个数
    NSLog(@"set1 count :%d", set1.count);
    //以数组的形式获取集合中的所有对象
    NSArray *allObj = [set2 allObjects];
    NSLog(@"allObj :%@", allObj);
    //获取任意一对象
    NSLog(@"anyObj :%@", [set3 anyObject]);
    //是否包含某个对象
    NSLog(@"contains :%d", [set3 containsObject:@"obj2"]);
    //是否有交集
    NSLog(@"intersect obj :%d", [set1 intersectsSet:set3]);
    //是否完全匹配
    NSLog(@"isEqual :%d", [set2 isEqualToSet:set3]);
    //set2是否是set1的子集合
    NSLog(@"isSubSet :%d", [set2 isSubsetOfSet:set1]);
    1.4 NSMutableSet
    //集合元素相减
    [mutableSet1 minusSet:mutableSet2];
    //只留下相等元素
    [mutableSet1 intersectSet:mutableSet3];
    //合并集合
    [mutableSet2 unionSet:mutableSet3]
    //删除指定元素
    [mutableSet2 removeObject:@"a"];
    //删除所有数据
    [mutableSet2 removeAllObjects];
    1.5 NSDictionary和NSMutableDictionary

    NSDictionary *dict = @{@"name": @"zhangct", @"age": @20};

     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:@"zhangct" forKey:@"name"];
    1.6 NSNumber

    NSNumber *num1 = [NSNumber numberWithInt:10];

    [num1 intValue];

     NSNumber *num2 = [NSNumber numberWithFloat:1.0];

    [num2 floatValue];

     NSNumber *num3;

    int age = 20;

    num3 = @(age);

    num3 = @100;

    1.7 NSValue

    CGPoint p = CGPointMake(10, 20);

    NSValue *value = [NSValue valueWithPoint:p];

     NSLog(@"%@", NSStringFromPoint([value pointValue]));
    1.8 NSDate

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    formatter.dateFormat = @"yyyy年MM月dd日 HH时mm分ss秒";

     NSDate *date = [NSDate date];
    NSLog(@"%@", [formatter stringFromDate:date]);
    4.6 NSObject
    4.6.1 NSObject

    [self performSelector:@selector(onClick:value1:) withObject:nil withObject:nil];

    performSelector是执行self对象中的onClick:value1:方法,最多支持传递2个参数.

    4.6.2 NSObject(NSDelayedPerforming)

    //播放完毕后延迟1秒执行释放内存

    [self.tom performSelector:@selector(setAnimationImages:) 
    withObject:nil afterDelay:self.tom.animationDuration + 1.0f];

    4.6.3 NSObject(NSKeyValueCoding)
    //单个赋值
    [self setValue:dict[@"answer"] forKey:@"answer"];

    [self setValue:dict[@"title"] forKey:@"title"];

     //ForKeyPath取值,返回Person的car name
    [p valueForKeyPath:@"car.name"]
     //ForKeyPath取值返回title数组
    carsGroups valueForKeyPath:@"title"
     
    //字典转模型,使用此方法要保证属性名称和字典的key同名
    [self setValuesForKeysWithDictionary:dict];
     //模型转字典
    NSDictionary *dict = [p dictionaryWithValuesForKeys:@[@"name", @"age"]];
    4.6.4 NSObject(UINibLoadingAdditions)

    //当xib文件加载完毕后执行

    - (void)awakeFromNib;
     
    4.8 NSIndexPath

    //根据分组和行号初始化对象

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:10 inSection:0];

     //返回分组

    indexPath.section

     //返回行号
    indexPath.row
  • 相关阅读:
    Alpha项目冲刺_博客链接合集
    项目系统设计
    项目需求分析
    项目选题
    项目展示
    原型设计 + 用户规格说明书
    测试与优化
    结对作业1
    MathExam6317
    js入门基础
  • 原文地址:https://www.cnblogs.com/linxiu-0925/p/5058210.html
Copyright © 2011-2022 走看看