Skip to content

Commit 8e278bb

Browse files
committed
[SR-929] Implement NSString.init() with locale
1 parent a1d15a1 commit 8e278bb

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

Foundation/NSString.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,17 @@ extension NSString {
12141214
}
12151215

12161216
public convenience init(format: String, locale: AnyObject?, arguments argList: CVaListPointer) {
1217-
NSUnimplemented()
1217+
let str: CFString
1218+
if let loc = locale {
1219+
if loc.dynamicType === NSLocale.self || loc.dynamicType === NSDictionary.self {
1220+
str = CFStringCreateWithFormatAndArguments(kCFAllocatorSystemDefault, unsafeBitCast(loc, to: CFDictionary.self), format._cfObject, argList)
1221+
} else {
1222+
fatalError("locale parameter must be a NSLocale or a NSDictionary")
1223+
}
1224+
} else {
1225+
str = CFStringCreateWithFormatAndArguments(kCFAllocatorSystemDefault, nil, format._cfObject, argList)
1226+
}
1227+
self.init(str._swiftObject)
12181228
}
12191229

12201230
public convenience init(format: NSString, _ args: CVarArgType...) {

TestFoundation/TestNSString.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class TestNSString : XCTestCase {
7070
("test_stringByTrimmingCharactersInSet", test_stringByTrimmingCharactersInSet),
7171
("test_initializeWithFormat", test_initializeWithFormat),
7272
("test_initializeWithFormat2", test_initializeWithFormat2),
73+
("test_initializeWithFormat3", test_initializeWithFormat3),
7374
("test_stringByDeletingLastPathComponent", test_stringByDeletingLastPathComponent),
7475
("test_getCString_simple", test_getCString_simple),
7576
("test_getCString_nonASCII_withASCIIAccessor", test_getCString_nonASCII_withASCIIAccessor),
@@ -612,6 +613,35 @@ class TestNSString : XCTestCase {
612613
XCTAssertEqual(string, "4B")
613614
}
614615

616+
func test_initializeWithFormat3() {
617+
let argument: [CVarArg] = [1000, 42.0]
618+
619+
withVaList(argument) {
620+
pointer in
621+
let string = NSString(format: "Default value is %d (%.1f)", locale: nil, arguments: pointer)
622+
XCTAssertEqual(string, "Default value is 1000 (42.0)")
623+
}
624+
625+
withVaList(argument) {
626+
pointer in
627+
let string = NSString(format: "en_GB value is %d (%.1f)", locale: NSLocale.init(localeIdentifier: "en_GB"), arguments: pointer)
628+
XCTAssertEqual(string, "en_GB value is 1,000 (42.0)")
629+
}
630+
631+
withVaList(argument) {
632+
pointer in
633+
let string = NSString(format: "de_DE value is %d (%.1f)", locale: NSLocale.init(localeIdentifier: "de_DE"), arguments: pointer)
634+
XCTAssertEqual(string, "de_DE value is 1.000 (42,0)")
635+
}
636+
637+
withVaList(argument) {
638+
pointer in
639+
let loc: NSDictionary = ["NSDecimalSeparator" as NSString : "&" as NSString]
640+
let string = NSString(format: "NSDictionary value is %d (%.1f)", locale: loc, arguments: pointer)
641+
XCTAssertEqual(string, "NSDictionary value is 1000 (42&0)")
642+
}
643+
}
644+
615645
func test_stringByDeletingLastPathComponent() {
616646
do {
617647
let path: NSString = "/tmp/scratch.tiff"

0 commit comments

Comments
 (0)