Skip to content

Commit 1ed409c

Browse files
committed
ObjCBool: Convert to a struct to be compatible with the stdlib version
1 parent ac0014d commit 1ed409c

File tree

9 files changed

+97
-32
lines changed

9 files changed

+97
-32
lines changed

Foundation/FileManager.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ open class FileManager : NSObject {
158158
} else if let attr = attributes {
159159
try self.setAttributes(attr, ofItemAtPath: path)
160160
}
161-
} else if isDir {
161+
} else if isDir.boolValue {
162162
return
163163
} else {
164164
throw _NSErrorWithErrno(EEXIST, reading: false, path: path)
@@ -406,9 +406,9 @@ open class FileManager : NSObject {
406406
}
407407

408408
open func linkItem(atPath srcPath: String, toPath dstPath: String) throws {
409-
var isDir = false
409+
var isDir: ObjCBool = false
410410
if self.fileExists(atPath: srcPath, isDirectory: &isDir) {
411-
if !isDir {
411+
if !isDir.boolValue {
412412
// TODO: Symlinks should be copied instead of hard-linked.
413413
if link(srcPath, dstPath) == -1 {
414414
throw _NSErrorWithErrno(errno, reading: false, path: srcPath)
@@ -532,12 +532,13 @@ open class FileManager : NSObject {
532532
if let isDirectory = isDirectory {
533533
if (s.st_mode & S_IFMT) == S_IFLNK {
534534
if stat(path, &s) >= 0 {
535-
isDirectory.pointee = (s.st_mode & S_IFMT) == S_IFDIR
535+
isDirectory.pointee = ObjCBool((s.st_mode & S_IFMT) == S_IFDIR)
536536
} else {
537537
return false
538538
}
539539
} else {
540-
isDirectory.pointee = (s.st_mode & S_IFMT) == S_IFDIR
540+
let isDir = (s.st_mode & S_IFMT) == S_IFDIR
541+
isDirectory.pointee = ObjCBool(isDir)
541542
}
542543
}
543544

Foundation/NSAttributedString.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ open class NSAttributedString: NSObject, NSCopying, NSMutableCopying, NSSecureCo
115115
attributesInRange = attributes(at: currentIndex, longestEffectiveRange: &attributesEffectiveRange, in: enumerationRange)
116116
}
117117

118-
var shouldStop = false
118+
var shouldStop: ObjCBool = false
119119
block(attributesInRange, attributesEffectiveRange, &shouldStop)
120120
stop.pointee = shouldStop
121121

@@ -133,7 +133,7 @@ open class NSAttributedString: NSObject, NSCopying, NSMutableCopying, NSSecureCo
133133
attributeInRange = attribute(attrName, at: currentIndex, longestEffectiveRange: &attributeEffectiveRange, in: enumerationRange)
134134
}
135135

136-
var shouldStop = false
136+
var shouldStop: ObjCBool = false
137137
block(attributeInRange, attributeEffectiveRange, &shouldStop)
138138
stop.pointee = shouldStop
139139

@@ -232,10 +232,10 @@ private extension NSAttributedString {
232232
func _enumerate(in enumerationRange: NSRange, reversed: Bool, using block: (Int, UnsafeMutablePointer<ObjCBool>) -> NSRange) {
233233
var attributeEnumerationRange = AttributeEnumerationRange(range: enumerationRange, reversed: reversed)
234234
while attributeEnumerationRange.hasMore {
235-
var stop = false
235+
var stop: ObjCBool = false
236236
let effectiveRange = block(attributeEnumerationRange.currentIndex, &stop)
237237
attributeEnumerationRange.advance(step: effectiveRange.length)
238-
if stop {
238+
if stop.boolValue {
239239
break
240240
}
241241
}

Foundation/NSDictionary.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,11 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
467467
lock.lock()
468468
var stop = sharedStop
469469
lock.unlock()
470-
if stop { return }
470+
if stop.boolValue { return }
471471

472472
closure(keys[idx], objects[idx], &stop)
473473

474-
if stop {
474+
if stop.boolValue {
475475
lock.lock()
476476
sharedStop = stop
477477
lock.unlock()

Foundation/NSIndexSet.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,9 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
396396
let iteration = withoutActuallyEscaping(block) { (closure: @escaping (P, UnsafeMutablePointer<ObjCBool>) -> R) -> (Int) -> Void in
397397
return { (rangeIdx) in
398398
lock.lock()
399-
var stop = sharedStop
399+
var stop = ObjCBool(sharedStop)
400400
lock.unlock()
401-
if stop { return }
401+
if stop.boolValue { return }
402402

403403
let idx = rangeSequence.index(rangeSequence.startIndex, offsetBy: Int64(rangeIdx))
404404
let curRange = rangeSequence[idx]
@@ -407,9 +407,9 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
407407
if intersection.length > 0 {
408408
let _ = closure(intersection as! P, &stop)
409409
}
410-
if stop {
410+
if stop.boolValue {
411411
lock.lock()
412-
sharedStop = stop
412+
sharedStop = stop.boolValue
413413
lock.unlock()
414414
return
415415
}
@@ -426,9 +426,9 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
426426
} else {
427427
let _ = closure(idx as! P, &stop)
428428
}
429-
if stop {
429+
if stop.boolValue {
430430
lock.lock()
431-
sharedStop = stop
431+
sharedStop = stop.boolValue
432432
lock.unlock()
433433
return
434434
}

Foundation/NSPathUtilities.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,9 @@ public extension NSString {
408408
return false
409409
}
410410

411-
var isDirectory = false
411+
var isDirectory: ObjCBool = false
412412
let exists = FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory)
413-
return exists && isDirectory
413+
return exists && isDirectory.boolValue
414414
}
415415

416416
internal typealias _FileNamePredicate = (String?) -> Bool

Foundation/NSSet.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ extension NSSet {
255255
withUnsafeMutablePointer(to: &stop) { stop in
256256
block(obj, stop)
257257
}
258-
if stop {
258+
if stop.boolValue {
259259
break
260260
}
261261
}

Foundation/NSSwiftRuntime.swift

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,73 @@ import CoreFoundation
2424
}
2525
#endif
2626

27-
public typealias ObjCBool = Bool
28-
extension ObjCBool {
29-
public var boolValue: Bool { return self == true }
27+
/// The Objective-C BOOL type.
28+
///
29+
/// On 64-bit iOS, the Objective-C BOOL type is a typedef of C/C++
30+
/// bool. Elsewhere, it is "signed char". The Clang importer imports it as
31+
/// ObjCBool.
32+
@_fixed_layout
33+
public struct ObjCBool : ExpressibleByBooleanLiteral {
34+
#if os(OSX) || (os(iOS) && (arch(i386) || arch(arm)))
35+
// On OS X and 32-bit iOS, Objective-C's BOOL type is a "signed char".
36+
var _value: Int8
37+
38+
init(_ value: Int8) {
39+
self._value = value
40+
}
41+
42+
public init(_ value: Bool) {
43+
self._value = value ? 1 : 0
44+
}
45+
46+
#else
47+
// Everywhere else it is C/C++'s "Bool"
48+
var _value: Bool
49+
50+
public init(_ value: Bool) {
51+
self._value = value
52+
}
53+
#endif
54+
55+
/// The value of `self`, expressed as a `Bool`.
56+
public var boolValue: Bool {
57+
#if os(OSX) || (os(iOS) && (arch(i386) || arch(arm)))
58+
return _value != 0
59+
#else
60+
return _value
61+
#endif
62+
}
63+
64+
/// Create an instance initialized to `value`.
65+
@_transparent
66+
public init(booleanLiteral value: Bool) {
67+
self.init(value)
68+
}
69+
}
70+
71+
extension ObjCBool : CustomReflectable {
72+
/// Returns a mirror that reflects `self`.
73+
public var customMirror: Mirror {
74+
return Mirror(reflecting: boolValue)
75+
}
76+
}
77+
78+
extension ObjCBool : CustomStringConvertible {
79+
/// A textual representation of `self`.
80+
public var description: String {
81+
return self.boolValue.description
82+
}
83+
}
84+
85+
// Functions used to implicitly bridge ObjCBool types to Swift's Bool type.
86+
public // COMPILER_INTRINSIC
87+
func _convertBoolToObjCBool(_ x: Bool) -> ObjCBool {
88+
return ObjCBool(x)
89+
}
90+
91+
public // COMPILER_INTRINSIC
92+
func _convertObjCBoolToBool(_ x: ObjCBool) -> Bool {
93+
return x.boolValue
3094
}
3195

3296
internal class __NSCFType : NSObject {

Foundation/NSURL.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
309309

310310
let thePath = _standardizedPath(path)
311311

312-
var isDir : Bool = false
312+
var isDir: ObjCBool = false
313313
if thePath.hasSuffix("/") {
314314
isDir = true
315315
} else {
@@ -323,7 +323,7 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
323323
let _ = FileManager.default.fileExists(atPath: absolutePath, isDirectory: &isDir)
324324
}
325325

326-
self.init(fileURLWithPath: thePath, isDirectory: isDir, relativeTo: baseURL)
326+
self.init(fileURLWithPath: thePath, isDirectory: isDir.boolValue, relativeTo: baseURL)
327327
}
328328

329329
public convenience init(fileURLWithPath path: String, isDirectory isDir: Bool) {
@@ -339,7 +339,7 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
339339
thePath = path
340340
}
341341

342-
var isDir : Bool = false
342+
var isDir: ObjCBool = false
343343
if thePath.hasSuffix("/") {
344344
isDir = true
345345
} else {
@@ -348,7 +348,7 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
348348
}
349349
}
350350
super.init()
351-
_CFURLInitWithFileSystemPathRelativeToBase(_cfObject, thePath._cfObject, kCFURLPOSIXPathStyle, isDir, nil)
351+
_CFURLInitWithFileSystemPathRelativeToBase(_cfObject, thePath._cfObject, kCFURLPOSIXPathStyle, isDir.boolValue, nil)
352352
}
353353

354354
public convenience init(fileURLWithFileSystemRepresentation path: UnsafePointer<Int8>, isDirectory isDir: Bool, relativeTo baseURL: URL?) {
@@ -779,8 +779,8 @@ extension NSURL {
779779
var result : URL? = appendingPathComponent(pathComponent, isDirectory: false)
780780
if !pathComponent.hasSuffix("/") && isFileURL {
781781
if let urlWithoutDirectory = result {
782-
var isDir : Bool = false
783-
if FileManager.default.fileExists(atPath: urlWithoutDirectory.path, isDirectory: &isDir) && isDir {
782+
var isDir: ObjCBool = false
783+
if FileManager.default.fileExists(atPath: urlWithoutDirectory.path, isDirectory: &isDir) && isDir.boolValue {
784784
result = self.appendingPathComponent(pathComponent, isDirectory: true)
785785
}
786786
}
@@ -859,14 +859,14 @@ extension NSURL {
859859
}
860860

861861
// It might be a responsibility of NSURL(fileURLWithPath:). Check it.
862-
var isExistingDirectory = false
862+
var isExistingDirectory: ObjCBool = false
863863
let _ = FileManager.default.fileExists(atPath: resolvedPath, isDirectory: &isExistingDirectory)
864864

865865
if excludeSystemDirs {
866866
resolvedPath = resolvedPath._tryToRemovePathPrefix("/private") ?? resolvedPath
867867
}
868868

869-
if isExistingDirectory && !resolvedPath.hasSuffix("/") {
869+
if isExistingDirectory.boolValue && !resolvedPath.hasSuffix("/") {
870870
resolvedPath += "/"
871871
}
872872

TestFoundation/XDGTestHelper.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class XDGCheck {
3535
storage.setCookie(simpleCookie!)
3636
let fm = FileManager.default
3737
let destPath = xdg_data_home! + "/xdgTestHelper/.cookies.shared"
38-
var isDir = false
38+
var isDir: ObjCBool = false
3939
let exists = fm.fileExists(atPath: destPath, isDirectory: &isDir)
4040
if (!exists) {
4141
print("Expected cookie path: ", destPath)

0 commit comments

Comments
 (0)