Skip to content

Commit 141270e

Browse files
committed
Merge pull request #286 from seabaylea/nsstring-init
[SR-929] Implement NSString.init() with locale
2 parents fc1ef5d + 8e278bb commit 141270e

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: CVarArg...) {

TestFoundation/TestNSString.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class TestNSString : XCTestCase {
7373
("test_stringByTrimmingCharactersInSet", test_stringByTrimmingCharactersInSet),
7474
("test_initializeWithFormat", test_initializeWithFormat),
7575
("test_initializeWithFormat2", test_initializeWithFormat2),
76+
("test_initializeWithFormat3", test_initializeWithFormat3),
7677
("test_stringByDeletingLastPathComponent", test_stringByDeletingLastPathComponent),
7778
("test_getCString_simple", test_getCString_simple),
7879
("test_getCString_nonASCII_withASCIIAccessor", test_getCString_nonASCII_withASCIIAccessor),
@@ -617,6 +618,35 @@ class TestNSString : XCTestCase {
617618
XCTAssertEqual(string, "4B")
618619
}
619620

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

0 commit comments

Comments
 (0)