-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
7 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// Collection+Extension.swift | ||
// momoIOS | ||
// | ||
// Created by 임수현 on 2023/02/11. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Collection { | ||
subscript (safe index: Index) -> Element? { | ||
return indices.contains(index) ? self[index] : nil | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// String+Extension.swift | ||
// momoIOS | ||
// | ||
// Created by 임수현 on 2023/02/11. | ||
// | ||
|
||
import Foundation | ||
|
||
extension String { | ||
subscript (safe index: Int) -> String? { | ||
guard self.count > index else { return nil } | ||
let index = self.index(self.startIndex, offsetBy: index) | ||
return "\(self[index])" | ||
} | ||
|
||
subscript (range: Range<Int>) -> String? { | ||
let fromIndex = self.index(self.startIndex, offsetBy: max(0, range.lowerBound)) | ||
let toIndex = self.index(self.startIndex, offsetBy: min(range.upperBound, self.count)) | ||
return String(self[fromIndex..<toIndex]) | ||
} | ||
|
||
subscript(_ range: Range<Int>) -> String { | ||
let fromIndex = self.index(self.startIndex, offsetBy: range.startIndex) | ||
let toIndex = self.index(self.startIndex, offsetBy: range.endIndex) | ||
return String(self[fromIndex..<toIndex]) | ||
} | ||
|
||
func replaced(_ string: String, in range: NSRange) -> String? { | ||
guard let range = Range(range, in: self) else { return nil } | ||
return self.replacingCharacters(in: range, with: string) | ||
} | ||
|
||
var isBackspace: Bool { | ||
if let char = self.cString(using: String.Encoding.utf8) { | ||
return strcmp(char, "\\b") == -92 | ||
} | ||
return false | ||
} | ||
} |
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
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