Go directly to GitHub to see the code
YYGRegular
I am: language songCopy the code
It is a regular expression used on iOS, which implement by Swift
This is an extension class based on swift rapid development. The current coverage includes detailed printing, regular expressions, and will be maintained frequently
Add updates, go straight to the back. Or just look at GitHub
Just drag the Category folder into the project
1. Printing method and brief introduction
symbol | type | describe |
---|---|---|
#file | String | The path to the file containing the symbol |
#line | Int | The line number on which the symbol appears |
#column | Int | The class in which the symbol appears |
#function | String | The name of the method that contains the symbol |
public func printLogDebug<T>(_ message: T, file: String = #file, method: String = #function, line: Int = {# # line) if the DEBUG print (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ lineBegin (line) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ n class name: \(file as NSString).lastPathComponent)\n Method name: \(method)\n Info: \(message)\n------------------\(line) line End-------------------------") #endif }Copy the code
call
Direct call to print: printLogDebug("Content")Copy the code
Output to the console is automatically turned off in the Release version.
The printing effect is as follows:
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- (* line number *) lineBegin -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the class name: * * *. Swift method name: * * * information: Print the contents of the * * * * * * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- (* line number *) line End -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --Copy the code
See the table below for the use of public
The name of the | Control level (1-5, 5 highest) | explain |
---|---|---|
private | 1 | Can only be used in the current scope |
fileprivate | 2 | Can only be used in the current folder |
internal | 3 the default | Additional code access in target |
public | 4 | Develop libraries for other developers to use |
open | 5 | Only those libraries that use the open flag for other developers can be overridden in other libraries |
:
2. Regular expression usage
Example: >
Since Swift supports overloaded operators, this works= ~To judge whether it conforms to the definition of =~ function and symbol, go to the code to find it!!
>
if "[email protected]" =~ mail {
printLogDebug("^_^")}else {
printLogDebug("~ ~ ~ ~ (> _ <) ~ ~ ~ ~")}/// custom matching format to determine whether it is a mobile phone number
let isIphoneNum: String = "1\\d{10}"
if "13146492969" =~ isIphoneNum {
printLogDebug("^_^")}else {
printLogDebug("~ ~ ~ ~ (> _ <) ~ ~ ~ ~")}Copy the code
Defines the precedence of the operator:
precedencegroup
Defines the associative order in which the same operators appear
associativity
Indicates input before and after
infix
If you want to know more about regular matching, please go to:Regular expression learning
Here’s the basic notation.
Please point out the deficiencies
1./// The mailbox matches
let mail: String = "^ ([a - z0-9 _ \ \. -] +) @ ([\ \. \ \ da - z -] +) \ \. ([a-z \ \] {2, 6}) $"
2./// Matches the user name literal or numeric combination of 4 to 16 digits
let Username: String = [a - z0-9 "^ _ -] 16th {4} $"
3./// Match the password literal with an underscore, 6 to 18 characters
let Password: String = [a - z0-9 "^ _ -] {6} 16 $"
4./// match hexadecimal
let HexValue: String = "^ #? ([a-f0-9]{6}|[a-f0-9]{3})$"
5./// Contents with split symbol "anne-blair"
let Slug: String = "^[a-z0-9-]+$"
6./ / / matching URL
let isURL: String = "^(https? : \ \ / \ \ /)? ([\ \. \ \ da - z -] +) \ \. ([a-z \ \] {2, 6}) ([\ \ / \ \ w \ \. -] * * \ \ /? $"
7./// Matches the IP address
let IPAddress: String = "^ (? : (? : 25 [0 to 5] | 2 [0 to 4] [0-9] | [01]? [0-9] [0-9]?) \ \.) {3} (? : 25 [0 to 5] | 2 [0 to 4] [0-9] | [01]? [0-9] [0-9]?) $"
8./// the HTML
content <\center> conforms
let HTMLTag: String = "^<([a-z]+)([^<]+)*(? :>(.*)<\\/\\1>|\\s+\\/>)$"
9./// Date (ym-M-DD)
let isDate1: String = "(\\d{4}|\\d{2})-((1[0-2])|(0? [1-9])) - (([12] [0-9]) | | [01] (3) (0? [1-9]))"
10./// Date (mm/dd/yy)
let isDate2: String = "([0-2] (1) | (0? [1-9]))/(([12] [0-9]) | | [01] (3) (0? [1-9]))/(\\d{4}|\\d{2})"
11./// Time (hour: minute, 24-hour system)
let TimeFormat: String = "((1 | 0? [0-9] [0, 3] | 2) : [[0 to 5] [0-9])"
12./ / / is Chinese characters
let isChinese: String = "[\\u4e00-\\u9fa5]"
13./// Chinese and full-angle punctuation marks (characters)
let ChineseParagraph: String = "[\\u3000-\\u301e\\ufe10-\\ufe19\\ufe30-\\ufe44\\ufe50-\\ufe6b\\uff01-\\uffee]"
14./// China landline number
let fixedLineTelephone: String = "(\\d{4}-|\\d{3}-)? (\\d{8}|\\d{7})"
15.// Mainland China ID number (15 or 18 digits)
let IdNum: String = "\\d{15}(\\d\\d[0-9xX])?"
16./ / / phone number
let isIphoneNum: String = "1\\d{10}"
17./// zip code
let zipCode: String = "[1-9]\\d{5}"Copy the code
## Added some new method extensions. For example: UIView extension, rich text extension, UIColor extension, thread-safe use extension
3. Some extensions of UIView
The main convenience is to directly control the view x, Y and so on a series of operations
This is relatively simple, not to introduce, mainly set,get, integration directly use on the line
extension UIView {
var x: CGFloat {
get {
return frame.origin.x
}
set {
frame = CGRect(x: newValue, y: frame.origin.y, width: frame.width, height: frame.height)
}
}
........................Copy the code
4. Thread-safe operations
References are made in Oc to prevent secure access to certain resources in multithreading
Synchronized operation, more about multithreading knowledge points I, here a large number of pages described, will make you get more harvest. No statements here
It has been removed in Swiftsynchronized
We can directly use:
Objc_sync_enter // Resource operation objc_sync_exit
This can be done directly in conjunction with the use of closures
public func synchronized(_ lock: AnyObject, closure: () -> ()) {
objc_sync_enter(lock)
closure()
objc_sync_exit(lock)
}Copy the code
You can perform the corresponding execution like this:
var test = 0
synchronized(test as AnyObject) {
test = 1 // test is not changed by other threads in this scope
print(test)
}
/// or so
synchronized(test as AnyObject, closure: {
print(test) // test is not changed by other threads in this scope
})Copy the code
So you can use it happily
5. Extensions to UIColor
We all know that Xcode from 8.0 can use color editor, image preview directly in code. This greatly saves our development efficiency
But in the use of sometimes habits, may be more or less with code to operate, here to bring you in the use of code when some of the convenience
Sometimes during the development process, the UI gives a color label such as 0xCE0755, so we sometimes need to convert, of course, in the color editor can be used directly.
The conversion is as follows:
public extension UIColor {
/// Init color without divide 255.0
///
/// - Parameters:
/// - r: (0 ~ 255) red
/// - g: (0 ~ 255) green
/// - b: (0 ~ 255) blue
/// - a: (0 ~ 1) alpha
convenience init(r: Int, g: Int, b: Int, a: CGFloat) {
self.init(red: CGFloat(r) / 255.green: CGFloat(g) / 255.blue: CGFloat(b) / 255.alpha: a)
}
/// Init color without divide 255.0
///
/// - Parameters:
/// - r: (0 ~ 255) red
/// - g: (0 ~ 255) green
/// - b: (0 ~ 1) alpha
convenience init(r: Int, g: Int, b: Int) {
self.init(r: r, g: g, b: b, a: 1)}/// Init color with hex code
///
/// - Parameter hex: hex code (eg. 0x00eeee)
convenience init(hex: Int) {
self.init(r: (hex & 0xff0000) > >16.g: (hex & 0xff00) > >8.b: (hex & 0xff), a: 1)}}Copy the code
Here is to say that the main principle is: first through the & operation, obtain the corresponding color bit, and then by moving the corresponding number of bits to the left to carry out conversion. I won’t go into that here.portal
6. Some operations of rich text, the project is very clear introduction of the use, here do not make a statement.
Scientific notation conversion
Conversion of RMB amount
Updates continue, if you feel helpful to you hope you give a star
Go directly to GitHub to see the code