This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String.CharacterView { | |
subscript(sequentialAccess i: Int) -> Character { | |
let index = self.index(startIndex, offsetBy: i) | |
return self[index] | |
} | |
subscript(sequentialAccess range: Range<Int>) -> String.CharacterView { | |
let start = self.index(startIndex, offsetBy: range.lowerBound) | |
let end = self.index(start, offsetBy: range.count) | |
return self[start..<end] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String.CharacterView { | |
func read(_ i: Int) -> Character { | |
let index = self.index(startIndex, offsetBy: i) | |
return self[index] | |
} | |
func read(_ range: Range<Int>) -> String.CharacterView { | |
let start = self.index(startIndex, offsetBy: range.lowerBound) | |
let end = self.index(start, offsetBy: range.count) | |
return self[start..<end] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let s = "Café du 🌍" | |
let si = s.startIndex | |
let ci = s.characters.startIndex | |
si == ci // true | |
let si2 = s.index(si, offsetBy: 3) | |
let ci2 = s.characters.index(ci, offsetBy: 3) | |
si2 == ci2 // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// まずString.CharacterViewにメソッド定義 | |
extension String.CharacterView { | |
public subscript(sequentialAccess range: Range<Int>) -> String.CharacterView { | |
let lower = range.lowerBound | |
let startIndex = index(self.startIndex, offsetBy: lower) | |
let endIndex = index(startIndex, offsetBy: range.count) | |
return self[startIndex..<endIndex] | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// こういう相互変換が正しそう | |
extension String { | |
func nsRange(from range: Range<String.Index>) -> NSRange { | |
let from = range.lowerBound.samePosition(in: utf16) | |
let to = range.upperBound.samePosition(in: utf16) | |
return NSRange(location: utf16.distance(from: utf16.startIndex, to: from), | |
length: utf16.distance(from: from, to: to)) | |
} | |
func range(from nsRange: NSRange) -> Range<String.Index>? { | |
guard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func unwrapIfCan(value: Any) -> Any { | |
let m = Mirror(reflecting: value) | |
guard let displayStyle = m.displayStyle else { | |
return value | |
} | |
switch displayStyle { | |
case .optional: | |
break | |
default: | |
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String: BidirectionalCollection, RangeReplaceableCollection {} | |
extension String { | |
public mutating func removeFirst(_ n: Int) { | |
characters.removeFirst(n) | |
} | |
public mutating func removeLast(_ n: Int) { | |
characters.removeLast(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
var capitalizingFirstLetter: String { | |
return String(characters.prefix(1)).capitalized + String(characters.dropFirst()) | |
} | |
} | |
enum MyEnum: String { | |
case fooBar | |
var rawValue: String { return String(describing: self).capitalizingFirstLetter } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct DisposableWrapper: Disposable { | |
private let disposable: Disposable | |
init(disposable: Disposable) { | |
self.disposable = disposable | |
} | |
func dispose() { | |
disposable.dispose() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"profiles": [ | |
{ | |
"devices": [ | |
{ | |
"identifiers": { | |
"is_keyboard": true, | |
"is_pointing_device": false, | |
"product_id": 272, | |
"vendor_id": 2131 |