A simple text scanner which can parse NSMutableData using delimiter.
Faster because it does not have to do a NSData <-> String conversion.
It can be easily and reliably parse of the Twitter Streaming API and other stream.
- MutableDataScanner specific delimiter ... average: 0.071
- MutableDataScanner line delimiter ( CRLF or LF ) ... average: 0.092
- String#componentsSeparatedByString ... average: 0.304
See Processing streaming data for information about the Parsing responses you will receive from the streaming API.
import Foundation
import MutableDataScanner
import SwiftyJSON
class TwitterAPIStreamingRequest: NSObject, NSURLSessionDataDelegate {
let scanner = MutableDataScanner(delimiter: "\r\n")
// ...
func connection(connection: NSURLConnection, didReceiveData data: NSData) {
self.scanner.appendData(data)
while let data = self.scanner.next() {
if data.length > 0 {
let json = JSON(data: data)
}
}
}
}
read(length: Int) -> NSData?
let data = "0123456789abcdefghijklmnopqrstuvwxyz".dataUsingEncoding(NSUTF8StringEncoding)!
let scanner = MutableDataScanner(data: data)
scanner.read(length: 10) // => 0123456789
scanner.read(length: 10) // => abcdefghij
scanner.read(length: 10) // => klmnopqrst
scanner.read(length: 10) // => uvwxyz
scanner.read(length: 10) // => (nil)
read(offset: Int, length: Int) -> NSData?
let data = "0123456789abcdefghijklmnopqrstuvwxyz".dataUsingEncoding(NSUTF8StringEncoding)!
let scanner = MutableDataScanner(data: data)
scanner.read(offset: 5, length: 5) // => 56789
scanner.read(offset: 5, length: 5) // => fghij
scanner.read(offset: 5, length: 5) // => pqrst
scanner.read(offset: 5, length: 5) // => z
scanner.read(offset: 5, length: 5) // => (nil)
init(data: NSMutableData = default, delimiter: NSData)
init(data: NSMutableData = default, delimiter: String)
next() -> NSData?
hasNext() -> Bool
let data = "0123456789\nabcdefghijklmnopqrstuvwxyz\n0123".dataUsingEncoding(NSUTF8StringEncoding)!
let scanner = MutableDataScanner(data: data, delimiter: "\n")
scanner.data // => 0123456789\nabcdefghijklmnopqrstuvwxyz\n0123
scanner.hasNext() // => true
scanner.next() // => 0123456789
scanner.data // => abcdefghijklmnopqrstuvwxyz\n0123
scanner.hasNext() // => true
scanner.next() // => abcdefghijklmnopqrstuvwxyz
scanner.data // => 0123
scanner.hasNext() // => false
scanner.next() // => (nil)
next(delimiter: NSData) -> NSData?
hasNext(delimiter: NSData) -> Bool
next(delimiter: String) -> NSData?
hasNext(delimiter: String) -> Bool
let data = "0123456789\nabcdefghijklmnopqrstuvwxyz\n0123".dataUsingEncoding(NSUTF8StringEncoding)!
let scanner = MutableDataScanner(data: data)
scanner.data // => 0123456789\nabcdefghijklmnopqrstuvwxyz\n0123
scanner.hasNext("\r\n") // => false
scanner.hasNext("\n") // => true
scanner.next("\r\n") // => (nil)
scanner.next("\n") // => 0123456789
scanner.data // => abcdefghijklmnopqrstuvwxyz\n0123
nextLine() -> NSData?
hasNextLine -> Bool
let data = "0123456789\r\nabcdefghijklmnopqrstuvwxyz\n0123".dataUsingEncoding(NSUTF8StringEncoding)!
let scanner = MutableDataScanner(data: data)
scanner.data // => 0123456789\r\nabcdefghijklmnopqrstuvwxyz\n0123
scanner.hasNextLine() // => true
scanner.nextLine() // => 0123456789
scanner.data // => abcdefghijklmnopqrstuvwxyz\n0123
scanner.hasNextLine() // => true
scanner.nextLine() // => abcdefghijklmnopqrstuvwxyz
scanner.data // => 0123
scanner.hasNextLine() // => false
scanner.nextLine() // => (nil)
- iOS 8.0+ / Mac OS X 10.10+
- Xcode 8+ / Swift 3+
Add the following line to your Cartfile
github "s-aska/MutableDataScanner"
Add the following line to your Podfile
use_frameworks!
pod 'MutableDataScanner'
MutableDataScanner is released under the MIT license. See LICENSE for details.