What does mobile network code of 65535 mean with CTTelephonyNetworkInfo().subscriberCellularProvider?.mobileNetworkCode? - ios

I'm looking at some code somebody else has written which has no documentation, why is this code making a comparison with 65535?
class func canMakePhoneCall() -> Bool
{
guard let URL = URL(string: "tel://") else {
return false
}
let canOpenURL = UIApplication.shared.canOpenURL(URL)
if canOpenURL == false
{
return false
}
let mobileNetworkCode = CTTelephonyNetworkInfo().subscriberCellularProvider?.mobileNetworkCode
let isInvalidNetworkCode = mobileNetworkCode == nil
|| mobileNetworkCode?.characters.count == 0
|| mobileNetworkCode == "65535"
return isInvalidNetworkCode == false
}

According to an answer here, this could be an indication of removed SIM card, or in general inability to make a call at the moment.

Related

Why `startActivityUpdates` update rate is low in background?

I am trying to detect when a user starts driving (or being driven) while the app is in the background.
The update rate of activity becomes very low while the app is background and this causes the app to miss the beginning of the drive. When the app is in the foreground, this problem does not exist.
Is there a way to remedy this?
Here is my code:
func startMotionClassification() {
guard CMMotionActivityManager.isActivityAvailable() else {
return
}
opQueue = {
let o = OperationQueue()
o.name = "core-motion-updates"
return o
}()
self.motionActivityManager.startActivityUpdates(to: opQueue!) { activity in
guard let activity = activity else {return}
if (activity.automotive || Constants.IS_DEBUG) &&
(activity.confidence == .medium || activity.confidence == .high) &&
!self.isDriving {
self.isDriving = true
self.markStartOfTrip()
}
if !activity.automotive &&
(activity.confidence == .high) &&
self.isDriving {
self.isDriving = false
self.markEndOfTrip()
}
}
}

How to fix conditional binding have optional type not 'Bool'?

how can I solve this problem?
Ive been getting the same error in 10 different places, I have been running tests on it and can't seem to figure this
thanks in advance for any help that you guys provide it really means a lot to me
Initializer for conditional binding must have Optional type, not 'Bool'
extension HomeController: FiltersViewControllerDelegate{
func query(withCategory jewelry: Bool, shoe: Bool, hat: Bool, apearel: Bool, gear: Bool) -> Query {
if jewelry == false && shoe == false && hat == false && apearel == false && gear == false {
stackViewHeightConstraint.constant = 0
activeFiltersStackView.isHidden = true
} else {
stackViewHeightConstraint.constant = 44
activeFiltersStackView.isHidden = false
}
var filtered = baseQuery
// Sort and Filter data
if let jewelry = jewelry, !jewelry.isEmpty { //Error
filtered = filtered.whereField("category", isEqualTo: jewelry)
}
//......more Filters....\\
if let gear = gear, !gear.isEmpty { //Error
filtered = filtered.whereField("category", isEqualTo: gear)
}
return filtered
}
func controller(_ controller: FilterViewController,
didSelectCategory jewelry: Bool,
shoe: Bool,
hat: Bool,
apearel: Bool,
gear: Bool) {
if jewelry == false && shoe == false && hat == false && apearel == false && gear == false {
stackViewHeightConstraint.constant = 0
activeFiltersStackView.isHidden = true
} else {
stackViewHeightConstraint.constant = 44
activeFiltersStackView.isHidden = false
}
let filtered = query(withCategory: jewelry, shoe: shoe, hat: hat, apearel: apearel, gear: gear)
if let jewelry = jewelry, ! jewelry.isEmpty { //Error
jewelryFilterLbl.text = "Jewelry"
jewelryFilterLbl.isHidden = false
} else {
jewelryFilterLbl.isHidden = true
}
//......more Filters....\\
if let gear = gear, !gear.isEmpty { //Error
gearFilterLbl.text = "gear"
gearFilterLbl.isHidden = false
} else {
gearFilterLbl.isHidden = true
}
query = filtered
}
}
Remove .isEmpty check it's not a property of a Bool
if jewelry { //Error
filtered = filtered.whereField("category", isEqualTo: jewelry)
}
//......more Filters....\\
if gear { //Error
filtered = filtered.whereField("category", isEqualTo: gear)
}
You're using if let binding on variables that are not optionals.
For example, your jewel variable is a Bool, not a Bool?. Using optional binding doesn’t make any sense.
if let jewelry = jewelry, ! jewelry.isEmpty { // Jewelry isn't an Optional!!!
jewelryFilterLbl.text = "Jewelry"
jewelryFilterLbl.isHidden = false
} else {
jewelryFilterLbl.isHidden = true
}
Plus, as other users have stated, Bool variables don't have .isEmpty method. Revise your logic and your code, it doesn't work at all.

How can I check for empty textfields and email, password regex before calling a function?

I'm trying to check for empty textfields and email and password regex before calling my chkInternet function, but can't figure it out, tried nested if statement, and making individual if statements with an else calling the function but that didn't work either, this is what I got so far but I'm stuck:
#IBAction func RegisterBTN(_ sender: Any) {
let userEmail = userEmailTxtField.text!
let userPhone = phoneNumberTxtField.text!
let password = passwordTxtField.text!
let passConfirm = passConfirmTxtField.text!
let emailValid = isValidEmailAddress(emailAddressString: userEmail)
let passValid = isPasswordValid(passWordString: password)
if userEmail.isEmpty{
emailErrorImg.isHidden = false
}
if userPhone.isEmpty{
phoneErrorImg.isHidden = false
}
if password.isEmpty{
passwordErrorImg.isHidden = false
}
if passConfirm.isEmpty{
passConfirmErrorImg.isHidden = false
}
if !emailValid {
emailErrorImg.isHidden = false
}
if !passValid {
passwordErrorImg.isHidden = false
}
ChkInternet()
}
Others have pointed out decent solutions, but there's a simple way to remove a lot of code repetition here:
let fieldImagePairs = [
(userEmailTxtField, emailErrorImg),
(phoneNumberTxtField, phoneErrorImg),
(passwordTxtField, passwordErrorImg),
(passConfirmTxtField, passConfirmErrorImg)
]
for (textField, errorImage) in fieldImagePairs {
guard textField.text?.isEmpty == false else {
errorImage.isHidden = false
return
}
}
guard emailValid else {
emailErrorImg.isHidden = false
return
}
guard passValid else {
passwordErrorImg.isHidden = false
return
}
ChkInternet()
If all of these codes are inside any function with no return type, simply adding return will suffice
if userEmail.isEmpty {
emailErrorImg.isHidden = false
return
}
if userPhone.isEmpty {
phoneErrorImg.isHidden = false
return
}
if password.isEmpty {
passwordErrorImg.isHidden = false
return
}
if passConfirm.isEmpty {
passConfirmErrorImg.isHidden = false
return
}
if !emailValid {
emailErrorImg.isHidden = false
return
}
if !passValid {
passwordErrorImg.isHidden = false
return
}
ChkInternet()
}
What return does is that it ends the execution of code inside a function block, meaning if return is called any and all codes below it will not be called anymore, therefore not calling your ChkInternet() function
For email validation simply add an extension to String
extension String
var isValidEmail: Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailTest = NSPredicate(format:"SELF MATCHES %#", emailRegEx)
return emailTest.evaluateWithObject(self)
}
}
// use case
if !someTextfield.text.isValidEmail {
return // do nothing cause not valid
}
// working codes here
// or
let someString: String = "asdasdawdaw"
if !someString.text.isVaildEmail {
return // do nothing cause not valid
}
// working codes here
// or
let someString: String = "asdasdawdaw"
guard someString.text.isValidEmail else {
return // do nothing cause not valid
}
// working codes here
i am using third party library to remove this headache. try this SwiftValidator

swift - sort an array of objects by their optional boolean property without force unwrapping

I can sort this array of store objects by their 'flagship' boolean property, but how can I safely unwrap the 'flagship' property first?
let flagshipStores = self.stores.sort {
$0.flagship! && !$1.flagship!
}
let flagshipStores = self.stores.sort {
guard let flagship0 = $0.flagship, let flagship1 = $1.flagship else { return false }
return flagship0 && !flagship1
}
One more approach: turn the Bool? into an Int, then compare the Ints. You get to specify how a nil value compares to non-nil values.
For instance, this sorts nil values before both false and true:
stores.sort { Int($0.flagship ?? -1) < Int($1.flagship ?? -1) }
This sorts nil values after both false and true:
stores.sort { Int($0.flagship ?? 2) < Int($1.flagship ?? 2) }
You can use the same pattern to make nil compare the same as true or the same as false. It's up to you.
Here's another approach.
You can use flatMap which will remove nil objects and unwrap those that are present. Then, the force unwrap will be safe to sort:
let flagshipStores = stores.flatMap({ return $0.flagship ? $0 : nil }).sort {
$0.flagship! && !$1.flagship!
}
This will remove stores with a nil flagship from the array.
How about:
$0.flagship == true && $1.flagship != true
The left side will succeed if the value is not nil and is true, the right side will succeed if the value is either nil or false.
As mr.Fixit pointed out on a comment, the accepted answer doesn't fully work because it doesn't take care of nils. Here is the correct answer with an extra string sample.
SWIFT 4
for a boolean sorting
let flagshipStores = self.stores.sorted(by: {
guard let flagship0 = $0.flagship, let flagship1 = $1.flagship else {
if $0.flagship == nil && $1.flagship == nil || $0.flagship != nil && $1.flagship == nil{
return true
}
else {
return false
}
}
return ($0.flagship == $1.flagship || $0.flagship == true && $1.flagship == false ? true : false)
})
for strings comparison sorting
let stores = self.stores.sorted(by: {
guard let store0 = $0.store, let store1 = $1.store else {
if $0.store == nil && $1.store == nil || $0.store != nil && $1.store == nil{
return true
}
else {
return false
}
}
return ( ($0.store)?.localizedStandardCompare($1.store!) == ComparisonResult.orderedAscending )
})
To filter nil values just use compactMap before sort
let flagshipStores = self.stores.compactMap { return $0.flagship }.sorted {
$0 && !$1
}
You could use this function to compare the Optional values without the need to unwrap.
func sortOptionalValues<T: Comparable>(lhs: T?, rhs: T?) -> Bool? {
switch (lhs != nil, rhs != nil) {
case (false, false):
return nil
case (true, false):
return true
case (false, true):
return false
case (true, true):
guard let lhs = lhs, let rhs = rhs else { return nil }
return lhs < rhs
}
}

Disable Alphabetic Characters in a Text Field (Part of my code will not execute)

To prevent the user from inputting more than one decimal into the text field and to prevent the user from inputting alphabetical characters into the text field I have tried the code below. The problem is I don't know how to check them both within the function and have two If statements which means the second one won't run as it never gets executed. If I take the code that checks number of decimals out and leave the character checker code it works perfectly. How can I get them both working though?
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let existingTextHasDecimalSeparator = textField.text?.rangeOfString(".")
let replacementTextHasDecimalSeparator = string.rangeOfString(".")
let charactersNotAllowed = NSCharacterSet.letterCharacterSet()
let replacementTextHasLetter = string.rangeOfCharacterFromSet(charactersNotAllowed)
if existingTextHasDecimalSeparator != nil && replacementTextHasDecimalSeparator != nil {
return false
} else {
return true
}
if replacementTextHasLetter != nil {
return false
} else {
return true
}
}
For swift 4 users you could delete the if replacementTextHasLetter != nil condition thats nested in the first condition along with the else condition and the code would work as well:
let existingTextHasDecimalSeparator = textField.text?.range( of: ".")
let replacementTextHasDecimalSeparator = string.range( of: ".")
let charactersNotAllowed = NSCharacterSet.letters
let replacementTextHasLetter = string.rangeOfCharacter(from: charactersNotAllowed)
if existingTextHasDecimalSeparator != nil, replacementTextHasDecimalSeparator != nil{
return false
}
if replacementTextHasLetter != nil{
return false
}
return true
}
The reason your function does not work is that it makes its decision early for both the positive and the negative case.
Defer returning true to the very end of the function:
if existingTextHasDecimalSeparator != nil && replacementTextHasDecimalSeparator != nil {
// Do not allow multiple decimal separators
return false
}
if replacementTextHasLetter != nil {
// Do not allow letters
return false
}
return true
This reflects the all-or-nothing logic of the decision: all checks must succeed in order to all checks, while it takes only one failed check to reject the change.
let existingTextHasDecimalSeparator = textField.text?.range( of: ".")
let replacementTextHasDecimalSeparator = string.range( of: ".")
let charactersNotAllowed = NSCharacterSet.letters
let replacementTextHasLetter = string.rangeOfCharacter(from: charactersNotAllowed)
if existingTextHasDecimalSeparator != nil, replacementTextHasDecimalSeparator != nil{
if replacementTextHasLetter != nil{
return false
}
return false
} else {
if replacementTextHasLetter != nil{
return false
}
return true
}
Could improve like the one above, but it's how I got my app to not allow multiple decimals and characters. It works.

Resources