Answers:
我认为这可以帮助您:
let timeNow = time(nil)
let aStr = String(format: "%@%x", "timeNow in hex: ", timeNow)
print(aStr)
结果示例:
timeNow in hex: 5cdc9c8d
NSString
于Foundation
框架。因此,您必须import Foundation
使其正常工作。否则,表达式将调用String.init<T>(T)
,并将产生类似的"(\"%@%x %x\", 10)"
代替。
%@%x
符号是什么意思?您能否将我指向一个资源,在这里我可以了解更多信息。
let str = "\(INT_VALUE), \(FLOAT_VALUE), \(DOUBLE_VALUE), \(STRING_VALUE)"
更新:我在Swift String(format:)
添加到它的API 之前写了这个答案。使用顶部答案给出的方法。
String(format:arguments:)
格式更适合添加格式
-stringWithFormat:
格式化的内容。在Swift中,String(format:arguments:)
将是Obj-C的Swift版本-stringWithFormat:
NSString
方法尚未在Swift的中实现String
。
不需要NSString
!
String(format: "Value: %3.2f\tResult: %3.2f", arguments: [2.7, 99.8])
要么
String(format:"Value: %3.2f\tResult: %3.2f", 2.7, 99.8)
首先阅读Swift语言的官方文档。
答案应该是
var str = "\(INT_VALUE) , \(FLOAT_VALUE) , \(DOUBLE_VALUE), \(STRING_VALUE)"
println(str)
这里
1)默认为任何浮点值 double
EX.
var myVal = 5.2 // its double by default;
->如果要显示浮点值,则需要显式定义诸如
EX.
var myVal:Float = 5.2 // now its float value;
这要清楚得多。
let INT_VALUE=80
let FLOAT_VALUE:Double= 80.9999
let doubleValue=65.0
let DOUBLE_VALUE:Double= 65.56
let STRING_VALUE="Hello"
let str = NSString(format:"%d , %f, %ld, %@", INT_VALUE, FLOAT_VALUE, DOUBLE_VALUE, STRING_VALUE);
println(str);
自发布以来,我已经知道很多时间了,但是我陷入了类似的境地,创建了一个简单类来简化我的生活。
public struct StringMaskFormatter {
public var pattern : String = ""
public var replecementChar : Character = "*"
public var allowNumbers : Bool = true
public var allowText : Bool = false
public init(pattern:String, replecementChar:Character="*", allowNumbers:Bool=true, allowText:Bool=true)
{
self.pattern = pattern
self.replecementChar = replecementChar
self.allowNumbers = allowNumbers
self.allowText = allowText
}
private func prepareString(string:String) -> String {
var charSet : NSCharacterSet!
if allowText && allowNumbers {
charSet = NSCharacterSet.alphanumericCharacterSet().invertedSet
}
else if allowText {
charSet = NSCharacterSet.letterCharacterSet().invertedSet
}
else if allowNumbers {
charSet = NSCharacterSet.decimalDigitCharacterSet().invertedSet
}
let result = string.componentsSeparatedByCharactersInSet(charSet)
return result.joinWithSeparator("")
}
public func createFormattedStringFrom(text:String) -> String
{
var resultString = ""
if text.characters.count > 0 && pattern.characters.count > 0
{
var finalText = ""
var stop = false
let tempString = prepareString(text)
var formatIndex = pattern.startIndex
var tempIndex = tempString.startIndex
while !stop
{
let formattingPatternRange = formatIndex ..< formatIndex.advancedBy(1)
if pattern.substringWithRange(formattingPatternRange) != String(replecementChar) {
finalText = finalText.stringByAppendingString(pattern.substringWithRange(formattingPatternRange))
}
else if tempString.characters.count > 0 {
let pureStringRange = tempIndex ..< tempIndex.advancedBy(1)
finalText = finalText.stringByAppendingString(tempString.substringWithRange(pureStringRange))
tempIndex = tempIndex.advancedBy(1)
}
formatIndex = formatIndex.advancedBy(1)
if formatIndex >= pattern.endIndex || tempIndex >= tempString.endIndex {
stop = true
}
resultString = finalText
}
}
return resultString
}
}
以下链接发送到完整的源代码:https : //gist.github.com/dedeexe/d9a43894081317e7c418b96d1d081b25
该解决方案基于本文:http : //vojtastavik.com/2015/03/29/real-time-formatting-in-uitextfield-swift-basics/
尝试成功:
var letters:NSString = "abcdefghijkl"
var strRendom = NSMutableString.stringWithCapacity(strlength)
for var i=0; i<strlength; i++ {
let rndString = Int(arc4random() % 12)
//let strlk = NSString(format: <#NSString#>, <#CVarArg[]#>)
let strlk = NSString(format: "%c", letters.characterAtIndex(rndString))
strRendom.appendString(String(strlk))
}