Skip to main content
deleted 1 character in body
Source Link
Fattie
  • 9.8k
  • 76
  • 455
  • 769

Assuming you want a field length of 2 with leading zeros you'd do this:

import Foundation for myInt in 1 ... 3 { print(String(format: "%02d", myInt)) } 

output:

01 02 03 

This requires import Foundation so technically it is not a part of the Swift language but a capability provided by the Foundation framework. Note that both import UIKit and import Cocoa include Foundation so it isn't necessary to import it again if you've already imported Cocoa or UIKit.


The format string can specify the format of multiple items. ForFor instance, if you are trying to format 3 hours, 15 minutes and 7 seconds into 03:15:07 you could do it like this:

let hours = 3 let minutes = 15 let seconds = 7 print(String(format: "%02d:%02d:%02d", hours, minutes, seconds)) 

output:

03:15:07 

Assuming you want a field length of 2 with leading zeros you'd do this:

import Foundation for myInt in 1 ... 3 { print(String(format: "%02d", myInt)) } 

output:

01 02 03 

This requires import Foundation so technically it is not a part of the Swift language but a capability provided by the Foundation framework. Note that both import UIKit and import Cocoa include Foundation so it isn't necessary to import it again if you've already imported Cocoa or UIKit.


The format string can specify the format of multiple items. For instance, if you are trying to format 3 hours, 15 minutes and 7 seconds into 03:15:07 you could do it like this:

let hours = 3 let minutes = 15 let seconds = 7 print(String(format: "%02d:%02d:%02d", hours, minutes, seconds)) 

output:

03:15:07 

Assuming you want a field length of 2 with leading zeros you'd do this:

import Foundation for myInt in 1 ... 3 { print(String(format: "%02d", myInt)) } 

output:

01 02 03 

This requires import Foundation so technically it is not a part of the Swift language but a capability provided by the Foundation framework. Note that both import UIKit and import Cocoa include Foundation so it isn't necessary to import it again if you've already imported Cocoa or UIKit.


The format string can specify the format of multiple items. For instance, if you are trying to format 3 hours, 15 minutes and 7 seconds into 03:15:07 you could do it like this:

let hours = 3 let minutes = 15 let seconds = 7 print(String(format: "%02d:%02d:%02d", hours, minutes, seconds)) 

output:

03:15:07 
Update to latest Swift code style conventions.
Source Link
Cihat Gündüz
  • 21.7k
  • 8
  • 68
  • 84

Assuming you want a field length of 2 with leading zeros you'd do this:

import Foundation for myInt in 1 ... 3 { print(String(format: "%02d", myInt)) } 

output:

01 02 03 

This requires import Foundation so technically it is not a part of the Swift language but a capability provided by the Foundation framework. Note that both import UIKit and import Cocoa include Foundation so it isn't necessary to import it again if you've already imported Cocoa or UIKit.


The format string can specify the format of multiple items. For instance, if you are trying to format 3 hours, 15 minutes and 7 seconds into 03:15:07 you could do it like this:

let hours = 3 let minutes = 15 let seconds = 7 print(String(format: "%02d:%02d:%02d", hours, minutes, seconds)) 

output:

03:15:07 

Assuming you want a field length of 2 with leading zeros you'd do this:

import Foundation for myInt in 1...3 { print(String(format: "%02d", myInt)) } 

output:

01 02 03 

This requires import Foundation so technically it is not a part of the Swift language but a capability provided by the Foundation framework. Note that both import UIKit and import Cocoa include Foundation so it isn't necessary to import it again if you've already imported Cocoa or UIKit.


The format string can specify the format of multiple items. For instance, if you are trying to format 3 hours, 15 minutes and 7 seconds into 03:15:07 you could do it like this:

let hours = 3 let minutes = 15 let seconds = 7 print(String(format: "%02d:%02d:%02d", hours, minutes, seconds)) 

output:

03:15:07 

Assuming you want a field length of 2 with leading zeros you'd do this:

import Foundation for myInt in 1 ... 3 { print(String(format: "%02d", myInt)) } 

output:

01 02 03 

This requires import Foundation so technically it is not a part of the Swift language but a capability provided by the Foundation framework. Note that both import UIKit and import Cocoa include Foundation so it isn't necessary to import it again if you've already imported Cocoa or UIKit.


The format string can specify the format of multiple items. For instance, if you are trying to format 3 hours, 15 minutes and 7 seconds into 03:15:07 you could do it like this:

let hours = 3 let minutes = 15 let seconds = 7 print(String(format: "%02d:%02d:%02d", hours, minutes, seconds)) 

output:

03:15:07 
Rollback to Revision 6
Source Link
vacawama
  • 155.1k
  • 32
  • 283
  • 315

Assuming you want a field length of 2 with leading zeros you'd do this:

import Foundation for numbermyInt in 1...3 { print(String(format: "%02d", numbermyInt)) } 

output:

01 02 03 

This requires import Foundation so technically it is not a part of the Swift language but a capability provided by the Foundation framework. Note that both import UIKit and import Cocoa include Foundation so it isn't necessary to import it again if you've already imported Cocoa or UIKit.


The format string can specify the format of multiple items. For instance, if you are trying to format 3 hours, 15 minutes and 7 seconds into 03:15:07 you could do it like this:

let hours = 3 let minutes = 15 let seconds = 7 print(String(format: "%02d:%02d:%02d", hours, minutes, seconds)) 

output:

03:15:07 

Assuming you want a field length of 2 with leading zeros you'd do this:

import Foundation for number in 1...3 { print(String(format: "%02d", number)) } 

output:

01 02 03 

This requires import Foundation so technically it is not a part of the Swift language but a capability provided by the Foundation framework. Note that both import UIKit and import Cocoa include Foundation so it isn't necessary to import it again if you've already imported Cocoa or UIKit.


The format string can specify the format of multiple items. For instance, if you are trying to format 3 hours, 15 minutes and 7 seconds into 03:15:07 you could do it like this:

let hours = 3 let minutes = 15 let seconds = 7 print(String(format: "%02d:%02d:%02d", hours, minutes, seconds)) 

output:

03:15:07 

Assuming you want a field length of 2 with leading zeros you'd do this:

import Foundation for myInt in 1...3 { print(String(format: "%02d", myInt)) } 

output:

01 02 03 

This requires import Foundation so technically it is not a part of the Swift language but a capability provided by the Foundation framework. Note that both import UIKit and import Cocoa include Foundation so it isn't necessary to import it again if you've already imported Cocoa or UIKit.


The format string can specify the format of multiple items. For instance, if you are trying to format 3 hours, 15 minutes and 7 seconds into 03:15:07 you could do it like this:

let hours = 3 let minutes = 15 let seconds = 7 print(String(format: "%02d:%02d:%02d", hours, minutes, seconds)) 

output:

03:15:07 
added 2 characters in body
Source Link
Rudolf Adamkovič
  • 31.5k
  • 13
  • 107
  • 119
Loading
Updated with a second example with a more complicated format string.
Source Link
vacawama
  • 155.1k
  • 32
  • 283
  • 315
Loading
added 187 characters in body
Source Link
vacawama
  • 155.1k
  • 32
  • 283
  • 315
Loading
println is now print
Source Link
vacawama
  • 155.1k
  • 32
  • 283
  • 315
Loading
deleted 258 characters in body
Source Link
vacawama
  • 155.1k
  • 32
  • 283
  • 315
Loading
added 131 characters in body
Source Link
vacawama
  • 155.1k
  • 32
  • 283
  • 315
Loading
Source Link
vacawama
  • 155.1k
  • 32
  • 283
  • 315
Loading