Skip to content

Commit

Permalink
Updating code for property wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
codecat15 committed Jan 12, 2020
1 parent f163b72 commit 7930442
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import UIKit

/* Thank you for downloading the code, I hope the video on property wrapper helped you and if you have any questions then please feel free to ask them on the channel as a comment OR as email, My email Id is mentioned in the code :)
Do subscribe to the channel and share it with your friends OR on any social media you like ~codecat15
*/

//extension String
//{
// func isValidEmail(email: String) -> Bool {
// let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-za-z]{2,64}"
// let pred = NSPredicate(format: "SELF MATCHES %@", regex)
// return pred.evaluate(with: email)
// }
//}

@propertyWrapper
struct EmailPropertyWrapper
{
private var _value: String
var wrappedValue: String
{
get
{
return isValidEmail(email: _value) ? _value : String()
}
set
{
_value = newValue
}
}

init(_emailValue: String) {
_value = _emailValue
}

private func isValidEmail(email: String) -> Bool {
let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-za-z]{2,64}"
let pred = NSPredicate(format: "SELF MATCHES %@", regex)
return pred.evaluate(with: email)
}
}

struct Employee
{
@EmailPropertyWrapper var employeeEmailId: String
}

struct User
{
var name: String
@EmailPropertyWrapper var email: String

func validate() -> Bool
{
if(name.isEmpty || email.isEmpty)
{
debugPrint("name and a valid email is required and cannot be empty")
return false
}
return true
}

func registerUser()
{
if(validate())
{
//saving user records code...
debugPrint("User data saved")
}
}
}

let user = User(name: "codecat15", email: EmailPropertyWrapper(_emailValue:"codecat15gmail.com"))
user.registerUser()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ravirdixit/Desktop/MyPlayground.playground#CharacterRangeLen=1884&amp;CharacterRangeLoc=0&amp;EndingColumnNumber=0&amp;EndingLineNumber=74&amp;StartingColumnNumber=0&amp;StartingLineNumber=0&amp;Timestamp=600486351.617571"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ravirdixit/Desktop/MyPlayground.playground#CharacterRangeLen=1884&amp;CharacterRangeLoc=0&amp;EndingColumnNumber=0&amp;EndingLineNumber=74&amp;StartingColumnNumber=0&amp;StartingLineNumber=0&amp;Timestamp=600486351.617712"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>

0 comments on commit 7930442

Please sign in to comment.