StringWithFormat "%.2ld" objc to swift

SwingerDinger picture SwingerDinger · Oct 10, 2014 · Viewed 12.7k times · Source

I have an 'pickerView' object in a swift project. I do understand the code in Objective-c but I'm not sure how to implement it in Swift.

The Objc method

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component)
    {
        case 0://Week
            return 7;
        case 1://Hour
            return 24;
        default://Minutes
            return 60;//or 7;(10 by 10) //or 13;(5 by 5)
    }
}

I'm only not sure how to implement the switch statement for the titleForRow function. So the stringWithFormat:@"%.2ld",(long)row is the problem.

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component)
    {
        case 0://Week
            return _dayOfWeek[row];
        case 1://Hour
            return [NSString stringWithFormat:@"%.2ld",(long)row];
        default://Minutes
            return [NSString stringWithFormat:@"%.2ld",(long)row];//or ,row*10] //or ,row*5]
    }
}

Can any one help me.

Greetings, dax

Answer

David Berry picture David Berry · Oct 10, 2014

You can continue to use stringWithFormat, but initializer names get mangled a little bit:

return NSString(format: "%.2ld", row)

Basically any class method that's named [{{class}} {{class}}WithXXXX:...] will be renamed to an initializer as {{Class}}(XXXX:...)