Search code examples
iosobjective-cswiftuipickerviewstringwithformat

StringWithFormat "%.2ld" objc to swift


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


Solution

  • 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:...)