how to convert this extension func swift to objective c

swift extension in objective-c
use swift framework in objective-c
initialize swift class in objective-c
use swift protocol in objective-c
swift extension where clause
swift extension in separate file
use swift class in objective-c
swift extension property

how to convert it

function calRound() -> String {

        if self > 1000 {

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

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


Here is the equivalent method. Not sure where you should put this, may be somewhere in utility 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;

how to convert this extension func swift to objective c, You can work with types declared in Swift from within the Objective-C code in your project by importing an Xcode-generated header file. This file is an  For example, a function pointer that has the type int (*)(void) in C is imported into Swift as @convention(c) -> Int32. If the type of the value pointed to by a C pointer cannot be represented by Swift, as is the case with an incomplete struct type, the pointer is imported as an Opaque Pointer.

Importing Swift into Objective-C, Extensions are similar to categories in Objective-C. (Unlike Objective-C categories, as 1,000 meters, so the km computed property multiplies the value by 1_000.00 to convert into extension Int {; func repetitions(task: () -> Void) {; for _ in 0. Questions: Put simply I am trying to convert a #define macro into a native Swift data structure of some sort. Just not sure how or what kind. Details I would like to try and replicate the following #define from Objective-C to Swift.

If you're talking about an NSNumber extension maybe this would help?

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

@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];

Extensions, A piecemeal migration of an existing Objective-C class to Swift often This in turn means the property must have an Objective-C-compatible type, even if function in the Swift extension that initializes the Swift-only properties. :-) Just type in some Objective-C, click Convert and that's it. At the right part of the screen, the converted - Swift - code will appear. At the right part of the screen, the converted - Swift - code will appear.

Migrating an Objective-C class to Swift: a piecemeal approach – Ole , Here is the Rosetta Stone for Objective-C to Swift. extension String { func trimmed() -> String { return trimmingCharacters(in: . An extension can extend an existing type to make it adopt one or more protocols. To add protocol conformance, you write the protocol names the same way as you write them for a class or structure: extension SomeType: SomeProtocol, AnotherProtocol {. // implementation of protocol requirements goes here.

Objective-C to Swift conversion cheat sheet – Hacking with Swift, in Swift make an extension for Objective-C class WXSwiftTestModule @objc(​printSome:callback:) public func printSome(someThing:String,  World's #1 Objective-C to Swift Converter. For iOS / Mac OS X Developers. Swiftify saves thousands of work hours by converting source code of iOS apps to new Apple's Swift language in one click. 1 CLICK TO CONVERT. For Web Developers. We are partnered with Runtime Converter to offer other language converters - from PHP to Java, Swift and C#

Extend iOS with Swift - Weex, Since today most of the iOS apps are getting developed in the Swift, you should follow With the help of a modern Objective-C converter, Xcode allows you to The extensions available in the Objective-C language are combined into a Swift offers an easier approach when it comes to the function syntax. func get File System Representation (Unsafe Mutable Pointer<Int8>, max Length: Int) -> Bool Interprets the receiver as a system-independent path and fills a buffer with a C-string in a format and encoding suitable for use with file-system calls.

  • I'm assuming since the code does self/1000 your function is actually part of an extension to some type of number, along with rounded and format. Mind you, I'd just use MeasurementFormatter which would probably do most of what you seem to be asking
  • extension Double { func format(f: String) -> String { return String(format: "%(f)f", self) } func 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." } } }
  • I want to convert to write OBJ-C
  • can you please tell which class you have extented? NSNumber?
  • thank you. I write nearly same but code you is perfect for me haha