ios--基本数据类型应用(int ,float,double ,NSString,NSNumber)

本文深入探讨了Objective-C中基本数据类型的应用及字符串的处理方法,包括数值转换、数学运算、字符串分割与循环遍历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

基本数据类型应用(int ,float,double ,NSString,NSNumber)

 People *stu=[People new];
        NSLog(@"%@",stu.description);
       //define double
        NSNumber *num=[NSNumber numberWithDouble:[@"10" doubleValue]];
        NSLog(@"%@",num);
        NSString *n=@"9.000";
        double dn=[n doubleValue];
        dn=sqrt(dn);
        NSLog(@"dn:%f",dn);
        int d=[n intValue];
        NSLog(@"%d",d);
        float f=[@"9a" floatValue];
        NSLog(@"%f",f);
        NSLog(@"ok...");

 //

   long fileId=2115004;
    NSNumber *file=[[NSNumber alloc]initWithLong:fileId];
    fileId=[file longValue];

 

 

result:

2013-06-16 12:28:45.573 SDKDemo[523:c07] name:ketty,age:30
2013-06-16 12:28:45.578 SDKDemo[523:c07] 10
2013-06-16 12:28:45.579 SDKDemo[523:c07] dn:3.000000
2013-06-16 12:28:45.581 SDKDemo[523:c07] 9
2013-06-16 12:28:45.582 SDKDemo[523:c07] 9.000000
2013-06-16 12:28:45.583 SDKDemo[523:c07] ok...

 

      NSString *n=@"9.000";
        double dn=[n doubleValue];
        dn=sqrt(dn);
        NSLog(@"dn:%f",dn);
        dn=sin([@"90.0" doubleValue]);
        NSLog(@"dn:%f",dn);

 字符串分割

     NSString *str=@"<p>haha<br/></p>";
        NSCharacterSet *charset=[NSCharacterSet characterSetWithCharactersInString:@"<p/br>"];
        NSArray *array=[str componentsSeparatedByCharactersInSet: charset];
        NSLog(@"array=%@",array);
        for (NSString *string1 in array) {
            if ([string1 length]>0) {
                NSLog(@"string=%@",string1);
            }
        } 
        NSString *strhello=@"hello wor ld";
        NSCharacterSet *set2=[NSCharacterSet whitespaceAndNewlineCharacterSet];
       // set2=[NSCharacterSet characterSetWithCharactersInString:@"w"];
        NSArray *arr=[strhello componentsSeparatedByCharactersInSet:set2];
        NSMutableString        *newStr=[NSMutableString string];
        for(NSString *s in arr){
            [newStr appendString:s];
        }
        NSLog(@"newStr=%@",newStr);

 result:

2013-06-16 13:29:10.341 SDKDemo[653:c07] name:ketty,age:30
2013-06-16 13:29:10.347 SDKDemo[653:c07] array=(
    "",
    "",
    "",
    haha,
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    ""
)
2013-06-16 13:29:10.348 SDKDemo[653:c07] string=haha
2013-06-16 13:29:10.349 SDKDemo[653:c07] newStr=hello or ld
2013-06-16 13:29:10.351 SDKDemo[653:c07] ok...

 NSEnumerator:

 NSString *strhello=@"hello wor ld";
        NSCharacterSet *set2=[NSCharacterSet whitespaceAndNewlineCharacterSet];
       // set2=[NSCharacterSet characterSetWithCharactersInString:@"w"];
              NSArray *arr=[strhello componentsSeparatedByCharactersInSet:set2];

  NSEnumerator *myEnumerator=[arr objectEnumerator];
        NSString *now;
        while ((now=[myEnumerator nextObject])) {
               NSLog(@"myE-now=%@",now);
        }

 result:

2013-06-16 13:41:45.107 SDKDemo[703:c07] myE-now=hello
2013-06-16 13:41:45.109 SDKDemo[703:c07] myE-now=wor
2013-06-16 13:41:45.111 SDKDemo[703:c07] myE-now=ld

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值