File tree Expand file tree Collapse file tree 1 file changed +5
-9
lines changed Expand file tree Collapse file tree 1 file changed +5
-9
lines changed Original file line number Diff line number Diff line change @@ -152,17 +152,13 @@ open class JSONSerialization : NSObject {
152
152
} else {
153
153
fatalError ( " Top-level object was not NSArray or NSDictionary " ) // This is a fatal error in objective-c too (it is an NSInvalidArgumentException)
154
154
}
155
-
156
- let count = jsonStr. lengthOfBytes ( using: . utf8)
157
- let bufferLength = count+ 1 // Allow space for null terminator
158
- var utf8 : [ CChar ] = Array < CChar > ( repeating: 0 , count: bufferLength)
159
- if !jsonStr. getCString ( & utf8, maxLength: bufferLength, encoding: . utf8) {
160
- fatalError ( " Failed to generate a CString from a String " )
155
+
156
+ let count = jsonStr. utf8. count
157
+ return jsonStr. withCString {
158
+ Data ( bytes: $0, count: count)
161
159
}
162
- let rawBytes = UnsafeRawPointer ( UnsafePointer ( utf8) )
163
- let result = Data ( bytes: rawBytes. bindMemory ( to: UInt8 . self, capacity: count) , count: count)
164
- return result
165
160
}
161
+
166
162
open class func data( withJSONObject value: Any , options opt: WritingOptions = [ ] ) throws -> Data {
167
163
return try _data ( withJSONObject: value, options: opt, stream: false )
168
164
}
You can’t perform that action at this time.
0 commit comments