如何将此扩展功能快速转换为目标c

时间:2018-08-09 03:17:38

标签: objective-c swift

如何转换

function calRound() -> String {

        if self > 1000 {

            let number02 = ((self/1000)*10).rounded()/10
            let someD = number02.format(f: ".1")
            return  "\(someD) km."

        }else{
            let someD = self.format(f: ".0")
            return  "\(someD) m."
        }

    }

3 个答案:

答案 0 :(得分:1)

这是等效的方法。不确定应该放在哪里,可能在实用程序class中。

- (NSString*) formatValue: (double) value {
    NSString* formatted;
    if (value > 1000) {
        double someD = round(((value/1000)*10))/10;
        formatted = [NSString stringWithFormat:@"%.1f km.", someD];
    } else {
        formatted = [NSString stringWithFormat:@"%.0f km.", value];
    }
    return formatted;
}

答案 1 :(得分:0)

我可以通过此代码转换代码功能

  • (NSString *)calcRound:(double)number {

如果(数字> 1000){

    double number02 =  round(((number/1000)*10))/10;


    someD =   [NSString stringWithFormat:@" %.1f km.", number02];


    return  someD;  //["\(%@) km.", someD ];


}else{

    someD =   [NSString stringWithFormat:@" %.0f m.", number];

    return  someD;  //["\(%@) m.", someD ];

}

}

答案 2 :(得分:0)

如果您正在谈论NSNumber扩展名,这可能会有所帮助吗?

@interface NSNumber (roundOf)
- (NSString *)roundOf;
@end

@implementation NSNumber (roundOf)
-(NSString *)roundOf{
    if (self.doubleValue > 1000) {
        double value = round(((self.doubleValue/1000)*10))/10;
        return [NSString stringWithFormat:@"%.1f km.", value];
    } else {
        return [NSString stringWithFormat:@"%.0f km.", self.doubleValue];
    }
}
@end