How to check dictionary value is empty or not - swift

i am new to swift and facing little issue and i not understanding how to check dictionary value.
func doValidate(data:Dictionary<String,AnyObject>,isEmail : String) -> Bool {
if(isEmail=="signup"){
if( data["last_name"] == nil || data["email"] == nil || data["password"] == nil || data["first_name"] == nil){
return false;
}
else{
return true;
}
}
}
Dictionary key is always constant and every time dictionary key exists but how i can check
value of data["last_name"] is empty or not?
if i used loop then its working but why single value not work?
for( myKey,myValue ) in data {
if(myValue as! String == ""){ // Work Perfect
return false;
}
}

For checking the value use objectForKey()
Here is modified code
func doValidate(data:Dictionary<String,AnyObject>,isEmail : String) -> Bool {
if(isEmail=="signup"){
if( data.objectForKey("last_name") == nil || data.objectForKey("email") == nil || data.objectForKey("password") == nil || data.objectForKey("first_name") == nil){
return false;
}
else{
return true;
}
}
}

You could consider the syntax introduced with Swift 1.2 inverting your if logic, obtaining something like this:
func doValidate(data:Dictionary<String,AnyObject>,isEmail : String) -> Bool {
if(isEmail == "signup"){
if let lastName = data["last_name"] as? String,
let email = data["email"] as? String,
let password = data["password"] as? String,
let firstName = data["first_name"] as? String
where
!lastName.isEmpty &&
!email.isEmpty &&
!password.isEmpty &&
!firstName.isEmpty {
return true
}
}
return false
}

Check length if you wanna check your string value. for example
func doValidate(data:Dictionary<String,AnyObject>,isEmail : String) -> Bool {
if(isEmail=="signup"){
if( data["last_name"]?.length == 0 || data["email"]?.length == 0 || data["password"]?.length == 0 || data["first_name"]?.length == 0){
return false;
}
else{
return true;
}
}
}

Related

I want to check the validation for both mobile number and email id in single textfield. How should I validate this?

I want to check the validation for both "Email id " and "mobile number" in single textfield. How should I validate the conditions for the same textfield?
Maybe this will help:
extension String {
var isPhoneNumber: Bool {
do {
let detector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.phoneNumber.rawValue)
let matches = detector.matches(in: self, options: [], range: NSMakeRange(0, self.count))
if let res = matches.first {
return res.resultType == .phoneNumber && res.range.location == 0 && res.range.length == self.count
} else {
return false
}
} catch {
return false
}
}
}
extension String {
var isInt: Bool {
return Int(self) != nil
}
}
func isValidEmail(testStr:String) -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let emailTest = NSPredicate(format:"SELF MATCHES %#", emailRegEx)
return emailTest.evaluate(with: testStr)
}
if textField.text.characterCount > 6 {
if textField.text.isInt == true {
print("this might be users phone number"
} else if isValidEmail(testStr:textField.text) {
print("this might be users mail")
} else {
print("user entered wrong value or nothing")
}

Check Dictionary to containing nil values and empty values

I want to check my dictionary to containing nil or empty value (nil and ""), add show it on the UI
my dictionary contains string and int values
let params = [
"token":APItoken.getToken(),
"gender":gender_id,
"car_number":number_car,
"car_model":car_model,
"year_of_birth":1998,
"car_year":create_year,
"seats_number":sits,
"facilities":fac,
"type":typeID
] as [String : Any]
I try to use this code, but it doesn't work
if params.values.filter({ $0 == nil }).isEmpty
{
print("full")
}
else
{
print("empty")
}
Change the type of Dictionary from [String:Any] to [String:Any?] to compare values to nil
let someDict = ["first":"name","second":1,"third":1.2,"someNilValue":nil] as [String:Any?]
func checkEmptyDict(_ dict:[String:Any?]) -> Bool {
for (_,value) in dict {
if value == nil || value as? String == "" { return true }
}
return false
}
checkEmptyDict(someDict) //returns true
you should change params type [String : Any] to [String : Any?]
your Code for check nil is correct just need to add empty string condition
like :
if params.values.filter({
if let x = $0 {
if let str = x as? String , str.isEmpty {
return true
} else {
return false
}
} else {
return true
}
}).isEmpty
{
print("not contain nil value ")
}
else
{
print("contain nil value ")
}

Confusion about setting a optional variable

I want to add the option for a user to add their phone number. If they add any phone number I want to add an alert informing them if they have not added a valid 10 digit phone number. However if they do not add anything in the phone number field I want the phoneInput variable to be set to "0". How would I go about doing this.
var phoneInput = ""
func signUp(){
if profileImage.image == nil {
showAvatarError()
} else if phoneNumber.text == "" {
self.phoneInput = "0"
} else if (phoneNumber.text?.characters.count)! != 10 {
showphoneNumberError()
}else if email.text == "" {
showEmailError()
}else if isValid(email.text!) != true{
showEmailError()
} else{
submitPressed()
print("Set info")
}
}
I'm not sure why you get the result that you do but here is a cleaner version
var phoneInput = ""
func signUp(){
// This check doesn't have anything to do with the number, so separe it
if profileImage.image == nil {
showAvatarError()
return
}
guard let temp = planEndValue.text else {
return
}
let userInput = temp.trimmingCharacters(in: .whitespaces)
if userInput.count == 0 {
self.phoneInput = "0"
} else if userInput.count != 10 {
showphoneNumberError()
}
}

Sorting mixed String-Int Strings numerically as the primary order, then alphabetically in swift

For a String which have both String and Int values (one of each) is it possible to do simple sort that will give the items ordered in numerical order as the primary order and alphabetical as the secondary order
var nameArray = ["Dave7", "Bob8", "Cathy9", "Henry10", "Susan10", "Pat11", "Steve12", "Dan12", "Ken1", "Sean2", "Howard3", "Dixie3", "Newman5", "Billy6"]
var sortedNameArray = nameArray.sort { $0.compare($1, options: .NumericSearch) == .OrderedAscending }
print(sortedNameArray) // gives the following:
Don't want this -> ["Billy6", "Bob8", "Cathy9", "Dan12", "Dave7", "Dixie3", "Henry10", "Howard3", "Ken1", "Newman5", "Pat11", "Sean2", "Steve12", "Susan10"]
Even though .NumericSearch was used the result is alphabetical.
I was able to get the desired result using a custom binary tree. Which gives the results:
Ken1 Sean2 Dixie3 Howard3 Newman5 Billy6 Dave7 Bob8 Cathy9 Henry10 Susan10 Pat11 Dan12 Steve12
But is there a simpler solution?
extension String {
var integerValue: Int? {
return Int(self)
}
}
func extractValueFromString(theString:String)->Int{
var catNumber: [Character] = []
//print("theString \(theString)")
for character in theString.characters{
var characterString = String(character)
if var value = characterString.integerValue { //if we don't check program crashes
//if numberSet.contains(Int(String(character))!) { //another way to check but redundant here
catNumber.append(character)
//print(catNumber)
// }
}
}
let numberString = String(catNumber)
return Int(numberString)!
}
class Node{
//nodes now only arrange strings
var data = ""
var value = Int()
var left:Node?;
var right:Node?;
deinit {
//print("deleting \(data)")
// print("node deleted")
}
init(data:String){
self.data = data;
//print(data)
}
}
class binaryTreeSort{
var root:Node?
init(){
}
deinit {
//print("tree deleted")
}
func getRoot()->Node{
return root!
}
func insertNewValue(data:String){
let newNode = Node(data:data)
var node:Node? = root
if (node == nil){
root = newNode
}
while (node != nil) {
let currentValue = node?.data
if currentValue == ""{
node?.data = data
return
}
if currentValue == data {
//we don't want duplicates.
return
}
if extractValueFromString(currentValue!) < extractValueFromString(data) {
if (node!.right != nil) {
node = node!.right
//print("Going Right at data \(node!.data)")
}else{
node!.right = newNode
//print("Going New Right at data \(node!.data)")
return
}
}else if extractValueFromString(currentValue!) == extractValueFromString(data){
if currentValue < data {
if (node!.right != nil) {
node = node!.right
//print("Going Right at data \(node!.data)")
}else{
node!.right = newNode
//print("Going New Right at data \(node!.data)")
return
}
}else{
if (node!.left != nil) {
//print("Going Left at data \(node!.data)")
node = node!.left
}else{
node!.left = newNode
//print("Going New Left at data \(node!.data)")
return
}
}
}
else{
if (node!.left != nil) {
//print("Going Left at data \(node!.data)")
node = node!.left
}else{
node!.left = newNode
//print("Going New Left at data \(node!.data)")
return
}
}
}
}
func inorderPrint(baseNode:Node){
if(baseNode.left != nil)
{
inorderPrint(baseNode.left!);
//print(" \(baseNode.data)")
}
print("\(baseNode.data)")
if(baseNode.right != nil)
{
inorderPrint(baseNode.right!)
//print(" \(baseNode.data)")
}
}
func reverseOrderPrint(baseNode:Node){
if(baseNode.right != nil)
{
reverseOrderPrint(baseNode.right!)
//print(" \(baseNode.data)")
}
print("\(baseNode.data)")
if(baseNode.left != nil)
{
reverseOrderPrint(baseNode.left!);
//print(" \(baseNode.data)")
}
}
}
var myBinaryTreeSort:binaryTreeSort? = binaryTreeSort()
for item in nameArray{
//print(item)
myBinaryTreeSort!.insertNewValue(item)
}
myBinaryTreeSort!.inorderPrint(myBinaryTreeSort!.getRoot())
print("---------------")
myBinaryTreeSort!.reverseOrderPrint(myBinaryTreeSort!.getRoot())
myBinaryTreeSort = nil //delete the tree
Use map to split the names into parts, sort to sort by number and name, and then map to restore the original:
func splitName(name:String) -> (String, Int) {
if let range = name.rangeOfCharacterFromSet(NSCharacterSet.decimalDigitCharacterSet()) {
return (name[name.startIndex..<range.startIndex], Int(name[range.startIndex..<name.endIndex])!)
} else {
return (name, 0)
}
}
print(nameArray.map(splitName).sort({ lhs, rhs in
if lhs.1 < rhs.1 {
return true
} else if lhs.1 > rhs.1 {
return false
} else {
return lhs.0 < rhs.0
}
}).map({ "\($0.0)\($0.1)" }))
Some other ways it could be done would be to maintain element 0 of the tuple as the full name (with numbers) and then the final map just becomes map({ $0.0 }) Depending on sizes, this may be more optimal than splitting the name each time it's compared.
If you have an array, you can sort with a custom closure.
For example:
nameArray.sort({extractValueFromString($0) < extractValueFromString($1)})
Will get you close. You just need to check if they are equal and return $0 < $1 instead.
Here's how I solved this, doing something similar to what #Lou-Franco alluded to:
func endInteger(word: String) -> Int {
if let range = word.rangeOfCharacterFromSet(NSCharacterSet.decimalDigitCharacterSet()){
let numberSubstring = word.substringFromIndex(range.startIndex)
return Int(numberSubstring) ?? 0
}
return 0
}
let sortedArray = yourArray.sort{endInteger($1) > endInteger($0)}

Expression resolves to an unused function Only instance methods can be declared #IBAction

I'm following a youtube series and I've done exactly as the guy in the film but still i get errors please help. (:
if you know the answer please help.
#IBAction func resetClicked(sender:UIButton) {
func reset() {
}
plays = [:]
img1.image = nil
img2.image = nil
img3.image = nil
img4.image = nil
img5.image = nil
img6.image = nil
img7.image = nil
img8.image = nil
img9.image = nil
}
}
func checkForWin() {
var whoWon = ["I":0,"you":1]
for (key,value) in whoWon {
if((plays[7] == value && plays[8] == value && plays[9] == value))
(plays[4] == value && plays[5] == value && plays[6] == value)
(plays[1] == value && plays[2] == value && plays[3] == value)
(plays[1] == value && plays[4] == value && plays[7] == value)
(plays[2] == value && plays[5] == value && plays[8] == value)
(plays[1] == value && plays[5] == value && plays[9] == value)
(plays[3] == value && plays[5] == value && plays[7] == value)) {
self.userMessage.hidden = false
self.userMessage.text = "Looks like \(key) won"
self.reset.hidden = false
self.done = true
}
}
}
func aiDeciding2() {
}
func aiTurn() {
}
You can't declare a function inside of an IBAction. Your error is the
func reset() {
}
Also make sure your IBAction is inside your view controller code