Swift NSScanner不会返回起始字符

时间:2015-09-19 09:20:15

标签: swift svg nsscanner

我正在扫描SVG路径字符串,并使用NSScanner尝试将其分解为其组件。当它运行但我得到了组件,但我没有回到"键入"

例如:" m 186.14021,456.15955 3.19716,8.75681 c 1.78393"

应该返回:" m 186.14021,456.15955 3.19716,8.75681"

实际结果:" 186.14021,456.15955 3.19716,8.75681"

我的代码出错的任何想法?

import UIKit

let whitespaceAndPunctuationSet = NSMutableCharacterSet.whitespaceAndNewlineCharacterSet()
whitespaceAndPunctuationSet.formUnionWithCharacterSet(NSCharacterSet.punctuationCharacterSet())


var str = "m 186.14021,456.15955 3.19716,8.75681 c 1.78393,0.93528 30.33822,25.2032 27.48643,47.35972 -4.11654,31.98273 -39.24645,39.28157 -59.94003,28.99354 -20.69358,-10.28804 -17.12572,-56.58417 -14.98501,-58.45472 2.14072,-1.87055 24.97501,-80.4337 44.24145,-26.65535 z"
let characterSet = NSCharacterSet(charactersInString: "mlhvcsqtaz")
let aScanner = NSScanner(string: str)
aScanner.charactersToBeSkipped = whitespaceAndPunctuationSet

while aScanner.atEnd == false
{
    var testString:NSString?

    aScanner.scanUpToCharactersFromSet(characterSet, intoString: &testString)

    aScanner.scanCharactersFromSet(characterSet, intoString: nil)

    print(testString)
}

1 个答案:

答案 0 :(得分:0)

好的,这似乎有效:

import UIKit

let whitespaceAndPunctuationSet = NSMutableCharacterSet.whitespaceAndNewlineCharacterSet()
whitespaceAndPunctuationSet.formUnionWithCharacterSet(NSCharacterSet.punctuationCharacterSet())


var str = "m 186.14021,456.15955 3.19716,8.75681 c 1.78393,0.93528 30.33822,25.2032 27.48643,47.35972 -4.11654,31.98273 -39.24645,39.28157 -59.94003,28.99354 -20.69358,-10.28804 -17.12572,-56.58417 -14.98501,-58.45472 2.14072,-1.87055 24.97501,-80.4337 44.24145,-26.65535 z"
let characterSet = NSCharacterSet(charactersInString: "mlhvcsqtaz")
let aScanner = NSScanner(string: str)
aScanner.charactersToBeSkipped = whitespaceAndPunctuationSet

while aScanner.atEnd == false
{
    var testString:NSString?
    var controlChar:NSString?

    aScanner.scanCharactersFromSet(characterSet, intoString: &controlChar)

    aScanner.scanUpToCharactersFromSet(characterSet, intoString: &testString)

    print(controlChar)
    print(testString)
}