appcoda-tw

Open full view…

求助Swift3 藍牙字串

sssyuan
Wed, 22 Mar 2017 09:26:55 GMT

新手想做個接收藍牙資料的APP 請問該如何從接收資料的字串中去讀其中的字元 例如我收到一組字串有10個字元,我只想判定第5字元或者是5-8的字元 有大大可幫解惑 拿範例給我參考嗎 感恩!!

carlhung
Tue, 18 Apr 2017 07:13:20 GMT

字串接收後應該是string吧?

carlhung
Tue, 18 Apr 2017 08:01:49 GMT

extension String { subscript(pos: Int) -> String { precondition(pos >= 0, "character position can't be negative") return self[pos...pos] } subscript(range: Range<Int>) -> String { precondition(range.lowerBound >= 0, "range lowerBound can't be negative") let lowerIndex = index(startIndex, offsetBy: range.lowerBound, limitedBy: endIndex) ?&quest; endIndex return self[lowerIndex..<(index(lowerIndex, offsetBy: range.count, limitedBy: endIndex) ?&quest; endIndex)] } subscript(range: ClosedRange<Int>) -> String { precondition(range.lowerBound >= 0, "range lowerBound can't be negative") let lowerIndex = index(startIndex, offsetBy: range.lowerBound, limitedBy: endIndex) ?&quest; endIndex return self[lowerIndex..<(index(lowerIndex, offsetBy: range.count, limitedBy: endIndex) ?&quest; endIndex)] } }

carlhung
Tue, 18 Apr 2017 08:05:13 GMT

我也只是copy and paste,但已經試過,是可用的。 let x = "hello" x[1] // "e" x[2..<5] // "llo"