Skip to content

Commit fed31bc

Browse files
authored
Merge pull request #1358 from ikesyo/optional-binding
2 parents a3898c1 + a4fb766 commit fed31bc

12 files changed

+108
-116
lines changed

Foundation/Data.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ public final class _DataStorage {
343343
let tryCalloc = (origLength == 0 || (newLength / origLength) >= 4)
344344
if allocateCleared && tryCalloc {
345345
newBytes = _DataStorage.allocate(newCapacity, true)
346-
if newBytes != nil {
347-
_DataStorage.move(newBytes!, _bytes!, origLength)
346+
if let newBytes = newBytes {
347+
_DataStorage.move(newBytes, _bytes!, origLength)
348348
_freeBytes()
349349
}
350350
}
@@ -353,8 +353,8 @@ public final class _DataStorage {
353353
allocateCleared = false
354354
if _deallocator != nil {
355355
newBytes = _DataStorage.allocate(newCapacity, true)
356-
if newBytes != nil {
357-
_DataStorage.move(newBytes!, _bytes!, origLength)
356+
if let newBytes = newBytes {
357+
_DataStorage.move(newBytes, _bytes!, origLength)
358358
_freeBytes()
359359
_deallocator = nil
360360
}
@@ -369,8 +369,8 @@ public final class _DataStorage {
369369
allocateCleared = clear && _DataStorage.shouldAllocateCleared(newCapacity)
370370
if allocateCleared && tryCalloc {
371371
newBytes = _DataStorage.allocate(newCapacity, true)
372-
if newBytes != nil {
373-
_DataStorage.move(newBytes!, _bytes!, origLength)
372+
if let newBytes = newBytes {
373+
_DataStorage.move(newBytes, _bytes!, origLength)
374374
_freeBytes()
375375
}
376376
}
@@ -619,8 +619,8 @@ public final class _DataStorage {
619619
memmove(mutableBytes! + start + replacementLength, mutableBytes! + start + length, currentLength - start - length)
620620
}
621621
if replacementLength != 0 {
622-
if replacementBytes != nil {
623-
memmove(mutableBytes! + start, replacementBytes!, replacementLength)
622+
if let replacementBytes = replacementBytes {
623+
memmove(mutableBytes! + start, replacementBytes, replacementLength)
624624
} else {
625625
memset(mutableBytes! + start, 0, replacementLength)
626626
}

Foundation/FileManager.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,13 @@ open class FileManager : NSObject {
296296
result[.systemNumber] = NSNumber(value: UInt64(s.st_dev))
297297
result[.systemFileNumber] = NSNumber(value: UInt64(s.st_ino))
298298

299-
let pwd = getpwuid(s.st_uid)
300-
if pwd != nil && pwd!.pointee.pw_name != nil {
301-
let name = String(cString: pwd!.pointee.pw_name)
299+
if let pwd = getpwuid(s.st_uid), pwd.pointee.pw_name != nil {
300+
let name = String(cString: pwd.pointee.pw_name)
302301
result[.ownerAccountName] = name
303302
}
304303

305-
let grd = getgrgid(s.st_gid)
306-
if grd != nil && grd!.pointee.gr_name != nil {
307-
let name = String(cString: grd!.pointee.gr_name)
304+
if let grd = getgrgid(s.st_gid), grd.pointee.gr_name != nil {
305+
let name = String(cString: grd.pointee.gr_name)
308306
result[.groupOwnerAccountName] = name
309307
}
310308

Foundation/NSCache.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ open class NSCache<KeyType : AnyObject, ObjectType : AnyObject> : NSObject {
119119
return
120120
}
121121

122-
while currentElement.nextByCost != nil && currentElement.nextByCost!.cost < entry.cost {
123-
currentElement = currentElement.nextByCost!
122+
while let nextByCost = currentElement.nextByCost, nextByCost.cost < entry.cost {
123+
currentElement = nextByCost
124124
}
125125

126126
// Insert entry between currentElement and nextElement

Foundation/NSPathUtilities.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,8 @@ public extension NSString {
475475
break loop
476476
}
477477

478-
if char != nil {
479-
let lhs = caseSensitive ? char : String(char!).lowercased().first!
478+
if let char = char {
479+
let lhs = caseSensitive ? char : String(char).lowercased().first!
480480
let rhs = caseSensitive ? c : String(c).lowercased().first!
481481
if lhs != rhs {
482482
break loop

Foundation/NSString.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ extension NSString {
699699
return
700700
}
701701
/* Find the starting point first */
702-
if startPtr != nil {
702+
if let startPtr = startPtr {
703703
var start: Int = 0
704704
if range.location == 0 {
705705
start = 0
@@ -723,7 +723,7 @@ extension NSString {
723723
buf.rewind()
724724
}
725725
}
726-
startPtr!.pointee = start
726+
startPtr.pointee = start
727727
}
728728
}
729729

Foundation/NSStringAPI.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,8 +1471,8 @@ extension StringProtocol where Index == String.Index {
14711471
tokenRanges: $0) as NSArray
14721472
}
14731473

1474-
if nsTokenRanges != nil {
1475-
tokenRanges?.pointee = (nsTokenRanges! as [AnyObject]).map {
1474+
if let nsTokenRanges = nsTokenRanges {
1475+
tokenRanges?.pointee = (nsTokenRanges as [AnyObject]).map {
14761476
self._range($0.rangeValue)
14771477
}
14781478
}

Foundation/Process.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,9 @@ open class Process: NSObject {
308308

309309
// If a termination handler has been set, invoke it on a background thread
310310

311-
if process.terminationHandler != nil {
311+
if let terminationHandler = process.terminationHandler {
312312
let thread = Thread {
313-
process.terminationHandler!(process)
313+
terminationHandler(process)
314314
}
315315
thread.start()
316316
}

Foundation/String.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ extension String : _ObjectTypeBridgeable {
2929
result = source._storage
3030
} else if type(of: source) == _NSCFString.self {
3131
let cf = unsafeBitCast(source, to: CFString.self)
32-
let str = CFStringGetCStringPtr(cf, CFStringEncoding(kCFStringEncodingUTF8))
33-
if str != nil {
34-
result = String(cString: str!)
32+
if let str = CFStringGetCStringPtr(cf, CFStringEncoding(kCFStringEncodingUTF8)) {
33+
result = String(cString: str)
3534
} else {
3635
let length = CFStringGetLength(cf)
3736
let buffer = UnsafeMutablePointer<UniChar>.allocate(capacity: length)

Foundation/XMLParser.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,10 @@ internal func _NSXMLParserStartElementNs(_ ctx: _CFXMLInterface, localname: Unsa
291291
// idx+3 = value, i+4 = endvalue
292292
// By using XML_PARSE_NOENT the attribute value string will already have entities resolved
293293
var attributeValue = ""
294-
if attributes[idx + 3] != nil && attributes[idx + 4] != nil {
295-
let numBytesWithoutTerminator = attributes[idx + 4]! - attributes[idx + 3]!
294+
if let value = attributes[idx + 3], let endvalue = attributes[idx + 4] {
295+
let numBytesWithoutTerminator = endvalue - value
296296
if numBytesWithoutTerminator > 0 {
297-
let buffer = UnsafeBufferPointer(start: attributes[idx + 3]!,
297+
let buffer = UnsafeBufferPointer(start: value,
298298
count: numBytesWithoutTerminator)
299299
attributeValue = String._fromCodeUnitSequence(UTF8.self,
300300
input: buffer)!

TestFoundation/TestJSONSerialization.swift

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -757,15 +757,14 @@ extension TestJSONSerialization {
757757
XCTFail("Unable to convert string to data")
758758
return
759759
}
760-
let filePath = createTestFile("TestJSON.txt",_contents: data)
761-
if filePath != nil {
762-
let fileStream: InputStream = InputStream(fileAtPath: filePath!)!
760+
if let filePath = createTestFile("TestJSON.txt",_contents: data) {
761+
let fileStream: InputStream = InputStream(fileAtPath: filePath)!
763762
fileStream.open()
764763
let resultRead = try JSONSerialization.jsonObject(with: fileStream, options: [])
765764
let result = resultRead as? [String: Any]
766765
XCTAssertEqual(result?.count, 0)
767766
fileStream.close()
768-
removeTestFile(filePath!)
767+
removeTestFile(filePath)
769768
}
770769
} catch {
771770
XCTFail("Error thrown: \(error)")
@@ -780,14 +779,13 @@ extension TestJSONSerialization {
780779
XCTFail("Unable to convert string to data")
781780
return
782781
}
783-
let filePath = createTestFile("TestJSON.txt",_contents: data)
784-
if filePath != nil {
785-
let url = URL(fileURLWithPath: filePath!)
782+
if let filePath = createTestFile("TestJSON.txt",_contents: data) {
783+
let url = URL(fileURLWithPath: filePath)
786784
let inputStream: InputStream = InputStream(url: url)!
787785
inputStream.open()
788786
let result = try JSONSerialization.jsonObject(with: inputStream, options: []) as? [Any]
789787
inputStream.close()
790-
removeTestFile(filePath!)
788+
removeTestFile(filePath)
791789
XCTAssertEqual(result?[0] as? Bool, true)
792790
XCTAssertEqual(result?[1] as? Bool, false)
793791
XCTAssertEqual(result?[2] as? String, "hello")
@@ -1387,14 +1385,13 @@ extension TestJSONSerialization {
13871385
func test_jsonObjectToOutputStreamFile() {
13881386
let dict = ["a":["b":1]]
13891387
do {
1390-
let filePath = createTestFile("TestFileOut.txt",_contents: Data(capacity: 128))
1391-
if filePath != nil {
1392-
let outputStream = OutputStream(toFileAtPath: filePath!, append: true)
1388+
if let filePath = createTestFile("TestFileOut.txt",_contents: Data(capacity: 128)) {
1389+
let outputStream = OutputStream(toFileAtPath: filePath, append: true)
13931390
outputStream?.open()
13941391
let result = try JSONSerialization.writeJSONObject(dict, toStream: outputStream!, options: [])
13951392
outputStream?.close()
13961393
if(result > -1) {
1397-
let fileStream: InputStream = InputStream(fileAtPath: filePath!)!
1394+
let fileStream: InputStream = InputStream(fileAtPath: filePath)!
13981395
var buffer = [UInt8](repeating: 0, count: 20)
13991396
fileStream.open()
14001397
if fileStream.hasBytesAvailable {
@@ -1404,7 +1401,7 @@ extension TestJSONSerialization {
14041401
XCTAssertEqual(NSString(bytes: buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue), "{\"a\":{\"b\":1}}")
14051402
}
14061403
}
1407-
removeTestFile(filePath!)
1404+
removeTestFile(filePath)
14081405
} else {
14091406
XCTFail("Unable to create temp file")
14101407
}

TestFoundation/TestStream.swift

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -54,56 +54,56 @@ class TestStream : XCTestCase {
5454
func test_InputStreamWithUrl() {
5555
let message: NSString = "Hello, playground"
5656
let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)!
57+
guard let testFile = createTestFile("testFile_in.txt", _contents: messageData) else {
58+
XCTFail("Unable to create temp file")
59+
return
60+
}
61+
5762
//Initialiser with url
58-
let testFile = createTestFile("testFile_in.txt", _contents: messageData)
59-
if testFile != nil {
60-
let url = URL(fileURLWithPath: testFile!)
61-
let urlStream: InputStream = InputStream(url: url)!
62-
XCTAssertEqual(Stream.Status.notOpen, urlStream.streamStatus)
63-
urlStream.open()
64-
XCTAssertEqual(Stream.Status.open, urlStream.streamStatus)
65-
var buffer = [UInt8](repeating: 0, count: 20)
66-
if urlStream.hasBytesAvailable {
67-
let result :Int = urlStream.read(&buffer, maxLength: buffer.count)
68-
urlStream.close()
69-
XCTAssertEqual(Stream.Status.closed, urlStream.streamStatus)
70-
XCTAssertEqual(messageData.count, result)
71-
if(result > 0) {
72-
let output = NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue)
73-
XCTAssertEqual(message, output!)
74-
}
63+
let url = URL(fileURLWithPath: testFile)
64+
let urlStream: InputStream = InputStream(url: url)!
65+
XCTAssertEqual(Stream.Status.notOpen, urlStream.streamStatus)
66+
urlStream.open()
67+
XCTAssertEqual(Stream.Status.open, urlStream.streamStatus)
68+
var buffer = [UInt8](repeating: 0, count: 20)
69+
if urlStream.hasBytesAvailable {
70+
let result :Int = urlStream.read(&buffer, maxLength: buffer.count)
71+
urlStream.close()
72+
XCTAssertEqual(Stream.Status.closed, urlStream.streamStatus)
73+
XCTAssertEqual(messageData.count, result)
74+
if(result > 0) {
75+
let output = NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue)
76+
XCTAssertEqual(message, output!)
7577
}
76-
removeTestFile(testFile!)
77-
} else {
78-
XCTFail("Unable to create temp file")
7978
}
79+
removeTestFile(testFile)
8080
}
8181

8282
func test_InputStreamWithFile() {
8383
let message: NSString = "Hello, playground"
8484
let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)!
85+
guard let testFile = createTestFile("testFile_in.txt", _contents: messageData) else {
86+
XCTFail("Unable to create temp file")
87+
return
88+
}
89+
8590
//Initialiser with file
86-
let testFile = createTestFile("testFile_in.txt", _contents: messageData)
87-
if testFile != nil {
88-
let fileStream: InputStream = InputStream(fileAtPath: testFile!)!
89-
XCTAssertEqual(Stream.Status.notOpen, fileStream.streamStatus)
90-
fileStream.open()
91-
XCTAssertEqual(Stream.Status.open, fileStream.streamStatus)
92-
var buffer = [UInt8](repeating: 0, count: 20)
93-
if fileStream.hasBytesAvailable {
94-
let result: Int = fileStream.read(&buffer, maxLength: buffer.count)
95-
fileStream.close()
96-
XCTAssertEqual(Stream.Status.closed, fileStream.streamStatus)
97-
XCTAssertEqual(messageData.count, result)
98-
if(result > 0){
99-
let output = NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue)
100-
XCTAssertEqual(message, output!)
101-
}
91+
let fileStream: InputStream = InputStream(fileAtPath: testFile)!
92+
XCTAssertEqual(Stream.Status.notOpen, fileStream.streamStatus)
93+
fileStream.open()
94+
XCTAssertEqual(Stream.Status.open, fileStream.streamStatus)
95+
var buffer = [UInt8](repeating: 0, count: 20)
96+
if fileStream.hasBytesAvailable {
97+
let result: Int = fileStream.read(&buffer, maxLength: buffer.count)
98+
fileStream.close()
99+
XCTAssertEqual(Stream.Status.closed, fileStream.streamStatus)
100+
XCTAssertEqual(messageData.count, result)
101+
if(result > 0){
102+
let output = NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue)
103+
XCTAssertEqual(message, output!)
102104
}
103-
removeTestFile(testFile!)
104-
} else {
105-
XCTFail("Unable to create temp file")
106105
}
106+
removeTestFile(testFile)
107107
}
108108

109109
func test_InputStreamHasBytesAvailable() {
@@ -125,22 +125,22 @@ class TestStream : XCTestCase {
125125
}
126126

127127
func test_outputStreamCreationToFile() {
128-
let filePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256))
129-
if filePath != nil {
130-
let outputStream = OutputStream(toFileAtPath: filePath!, append: true)
131-
XCTAssertEqual(Stream.Status.notOpen, outputStream!.streamStatus)
132-
var myString = "Hello world!"
133-
let encodedData = [UInt8](myString.utf8)
134-
outputStream?.open()
135-
XCTAssertEqual(Stream.Status.open, outputStream!.streamStatus)
136-
let result: Int? = outputStream?.write(encodedData, maxLength: encodedData.count)
137-
outputStream?.close()
138-
XCTAssertEqual(myString.count, result)
139-
XCTAssertEqual(Stream.Status.closed, outputStream!.streamStatus)
140-
removeTestFile(filePath!)
141-
} else {
128+
guard let filePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256)) else {
142129
XCTFail("Unable to create temp file");
130+
return
143131
}
132+
133+
let outputStream = OutputStream(toFileAtPath: filePath, append: true)
134+
XCTAssertEqual(Stream.Status.notOpen, outputStream!.streamStatus)
135+
var myString = "Hello world!"
136+
let encodedData = [UInt8](myString.utf8)
137+
outputStream?.open()
138+
XCTAssertEqual(Stream.Status.open, outputStream!.streamStatus)
139+
let result: Int? = outputStream?.write(encodedData, maxLength: encodedData.count)
140+
outputStream?.close()
141+
XCTAssertEqual(myString.count, result)
142+
XCTAssertEqual(Stream.Status.closed, outputStream!.streamStatus)
143+
removeTestFile(filePath)
144144
}
145145

146146
func test_outputStreamCreationToBuffer() {
@@ -159,22 +159,22 @@ class TestStream : XCTestCase {
159159
}
160160

161161
func test_outputStreamCreationWithUrl() {
162-
let filePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256))
163-
if filePath != nil {
164-
let outputStream = OutputStream(url: URL(fileURLWithPath: filePath!), append: true)
165-
XCTAssertEqual(Stream.Status.notOpen, outputStream!.streamStatus)
166-
var myString = "Hello world!"
167-
let encodedData = [UInt8](myString.utf8)
168-
outputStream!.open()
169-
XCTAssertEqual(Stream.Status.open, outputStream!.streamStatus)
170-
let result: Int? = outputStream?.write(encodedData, maxLength: encodedData.count)
171-
outputStream?.close()
172-
XCTAssertEqual(myString.count, result)
173-
XCTAssertEqual(Stream.Status.closed, outputStream!.streamStatus)
174-
removeTestFile(filePath!)
175-
} else {
162+
guard let filePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256)) else {
176163
XCTFail("Unable to create temp file");
164+
return
177165
}
166+
167+
let outputStream = OutputStream(url: URL(fileURLWithPath: filePath), append: true)
168+
XCTAssertEqual(Stream.Status.notOpen, outputStream!.streamStatus)
169+
var myString = "Hello world!"
170+
let encodedData = [UInt8](myString.utf8)
171+
outputStream!.open()
172+
XCTAssertEqual(Stream.Status.open, outputStream!.streamStatus)
173+
let result: Int? = outputStream?.write(encodedData, maxLength: encodedData.count)
174+
outputStream?.close()
175+
XCTAssertEqual(myString.count, result)
176+
XCTAssertEqual(Stream.Status.closed, outputStream!.streamStatus)
177+
removeTestFile(filePath)
178178
}
179179

180180
func test_outputStreamCreationToMemory(){

0 commit comments

Comments
 (0)