objective-c(框架)

框架指objective-c的foundation库,下面的例子中给出几个常见用的类及其方法。

  • 数值对象
        NSNumber *intNum;
        NSNumber *longNum;
        NSNumber *floatNum;
        
        intNum = [NSNumber numberWithInteger:12];
        NSLog(@"%i", [intNum integerValue]);
        
        longNum = [NSNumber numberWithLong:0x123456];
        NSLog(@"%lx", [longNum longLongValue]);
        
        floatNum = [NSNumber numberWithFloat:12.00];
        NSLog(@"%f", [floatNum floatValue]);
        
        if([intNum isEqualToNumber:floatNum] == YES){
            NSLog(@"eqaul"); //相同
        }else{
            NSLog(@"not equal");
        } 
  •  字符串
        NSString *str1 = @"hello,world";
        NSString *str2 = [NSString stringWithFormat:@"%i,%@", 5, @"fredric"];
        NSLog(@"%@%@", str1,str2);
        NSLog([str1 stringByAppendingString:str2]);
        
        NSMutableString *str3 = [NSMutableString stringWithString:@"hello"];
        [str3 appendString:@"fredric_"];
        [str3 insertString:@"word" atIndex:str3.length];
        NSLog(@"%@",str3); //hellofredric_word
        
        NSRange res = [str3 rangeOfString:@"ric"];
        if(res.location != NSNotFound){
            [str3 deleteCharactersInRange:res];
        }
        
        NSLog(@"%@",str3); //hellofred_word
  • 数组
       NSArray *array = [NSArray arrayWithObjects:@"demo1",@"demo2",@"demo3", nil];
        
        for(int i = 0; i < [array count]; i++){
            NSLog(@"%@",[array objectAtIndex:i]);
        }
        
        NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:3];
        [mArray addObject:@"demo4"];
        [mArray addObject:@"demo5"];
        [mArray addObject:@"demo6"];
        
        for(int i = 0; i < [mArray count]; i++){
            NSLog(@"%@",[mArray objectAtIndex:i]);
        }
  • 字典
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",@"valu3", @"key3", nil];
        NSString *value1 = [dic objectForKey:@"key1"];
        NSLog(@"%@", value1);
        
        NSMutableDictionary *mDic = [[NSMutableDictionary alloc]init];
        [mDic setObject:@"value1_1" forKey:@"key1"];
        NSLog(@"%@", [mDic objectForKey:@"key1"]);
原文地址:https://www.cnblogs.com/Fredric-2013/p/4297316.html