How to pass a reference to a Boolean rather than its value? - swift

I want to use booleans as state storage.
In order to do that, I need to be able to change their state from difference places in the project.
To do that, I need somewhere to store them, and a way to pass a reference to them.
I've tried storing them as static variables in a GameManager, but passing references to these only seems to pass the value of true of false, not a reference.
How do I achieve this goal of having a passable boolean reference I can change the state of it from any part of the project?
UPDATE:
This can't be the best way to do this, but this achieves the goal of having a bunch of state booleans that I can use around the game world:
class GameManager {
static let sharedInstance = GameManager()
var previewAudioIsON: Bool = false
var previewVisuaIsOn: Bool = false
var timerDisplayIsOn: Bool = false
var quickStartIsOn: Bool = false
func touchedPreviewAudioButton() -> Bool {
if previewAudioIsON { previewAudioIsON = false}
else { previewAudioIsON = true }
return previewAudioIsON
}
func touchedPreviewVisualButton() -> Bool {
if previewVisuaIsOn { previewVisuaIsOn = false }
else { previewVisuaIsOn = true }
return previewVisuaIsOn
}
func touchedTimeDisplayButton() -> Bool {
if timerDisplayIsOn { timerDisplayIsOn = false }
else { timerDisplayIsOn = true }
return timerDisplayIsOn
}
func touchedQuickStartButton() -> Bool {
if quickStartIsOn { quickStartIsOn = false }
else { quickStartIsOn = true }
return quickStartIsOn
}
}

I gave you partially wrong information the other day ( I was having a brain fart), and need to apologize for that. I had overlooked something in my testing...
Here is what you need if you don't want to make the RefBool instances as I suggested (requires more legwork, not recommended):
/// Mutates a boolean:
func toggle(_ boolean: inout Bool) -> Bool {
boolean ? (boolean = false) : (boolean = true)
return boolean
}
/// Static state manager for Booleans
struct IsOn {
private static var
_previewAudio = false,
_previewVisual = false,
_timerDisplal = false,
_quickStart = false
enum State { case toggle, get }
static func previewAudio(_ toggleVal: State = .get) -> Bool {
if toggleVal == .toggle { toggle(&_previewAudio) }; return _previewAudio
}
// ... others
}
Testing:
let referenceToPA = IsOn.previewAudio
print ( IsOn.previewAudio() ) // False (default pram works)
print ( referenceToPA(.get) ) // False (can't use default pram)
referenceToPA(.toggle)
print ( IsOn.previewAudio() ) // True
print ( referenceToPA(.get) ) // True
IsOn.previewAudio(.toggle)
print ( IsOn.previewAudio() ) // False
print ( referenceToPA(.get) ) // False
But honestly, it would be easier to just do the RefBool from my other answer, then you wouldn't need the enum or the functions:
/// Holds a boolean in .val:
final class RefBool { var val: Bool; init(_ boolean: Bool) { val = boolean } }
/// Static state manager for Booleans
struct IsOn {
static var
previewAudio = RefBool(false),
previewVisual = RefBool(false),
timerDisplal = RefBool(false),
quickStart = RefBool(false)
}
Convenience Funcs (not necessary):
/// Mutates a boolean:
func toggle(_ boolean: inout Bool) -> Bool {
boolean ? (boolean = false) : (boolean = true)
return boolean
}
/// Mutates .val:
func toggle(_ refBool: RefBool) -> Bool {
refBool.val ? (refBool.val = false) : (refBool.val = true)
return refBool.val
}
Testing2:
let refToPA = IsOn.previewAudio
refToPA.val = true
print(refToPA.val) // true
print(IsOn.previewAudio.val) // true
toggle(&refToPA.val)
print(refToPA.val) // false
print(IsOn.previewAudio.val) // false
toggle(refToPA) // Using our fancy second toggle
print(refToPA.val) // true
print(IsOn.previewAudio.val) // true

try something like that:
Full example
import UIKit
enum ButtonType {
case PreviewAudio;
case PreviewVisua;
case TimerDisplay;
case QuickStart;
}
class SwitchProperty {
var type: ButtonType
var value: Bool
init (type: ButtonType) {
self.type = type
self.value = false
}
var description: String {
var result = "type = \(type)\n"
result += "value = \(value)"
return result
}
func switchValue() {
value.invertValue()
}
}
class GameManager {
static var previewAudioIsON = SwitchProperty(type: .PreviewAudio)
static var previewVisuaIsOn = SwitchProperty(type: .PreviewVisua)
static var timerDisplayIsOn = SwitchProperty(type: .TimerDisplay)
static var quickStartIsOn = SwitchProperty(type: .QuickStart)
}
class Button: UIButton {
var switchValue: SwitchProperty
init (type: ButtonType, frame: CGRect) {
switch type {
case .PreviewAudio:
switchValue = GameManager.previewAudioIsON
case .PreviewVisua:
switchValue = GameManager.previewVisuaIsOn
case .TimerDisplay:
switchValue = GameManager.timerDisplayIsOn
case .QuickStart:
switchValue = GameManager.quickStartIsOn
}
super.init(frame: frame)
addTarget(self, action: #selector(Button.buttonTouched), for: .touchUpInside)
}
func buttonTouched() {
switchValue.switchValue()
print(switchValue.description)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension Bool {
mutating func invertValue() {
self = !self
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
addButton(type: .PreviewVisua, frame: CGRect(x: 40, y: 40, width: 200, height: 40));
addButton(type: .PreviewAudio, frame: CGRect(x: 40, y: 100, width: 200, height: 40));
}
func addButton(type: ButtonType, frame: CGRect) {
let button = Button(type: type, frame: frame);
button.setTitleColor(UIColor.blue, for: .normal)
button.setTitle("\(type)", for: .normal)
view.addSubview(button)
}
}
Properties accsess from another code
// change value
GameManager.previewAudioIsON.value = false
// check type
if (GameManager.previewAudioIsON.type == .PreviewAudio) {
print("previewAudioIsON")
}
Result

Related

How do I modify function to be used in MVP architecture?

I have the function below. It works properly.
When a user types any character it validates the user input and hides some imageView based on the input.
#IBAction func onEmailValueChanged(_ sender: UITextField) {
let hasMinimumLength = TextValidationHelper.validateHasMinimumLength(password: sender.text!)
passLengthCheckmarkImageView.isHidden = hasMinimumLength ? false : true
let hasCapitalLetter = TextValidationHelper.validateHasCapitalLetter(password: sender.text!)
passHasUppercaseCheckmarkImageView.isHidden = hasCapitalLetter ? false : true
let hasNumber = TextValidationHelper.validateHasNumber(password: sender.text!)
passHasNumberCheckmarkImageView.isHidden = hasNumber ? false : true
let hasSpecialCharacter = TextValidationHelper.validateHasSpecialCharacter(password: sender.text!)
passHasSymbolCheckmarkImageView.isHidden = hasSpecialCharacter ? false : true
resetButton.isHidden = hasMinimumLength && hasCapitalLetter && hasNumber && hasSpecialCharacter ? false : true
}
But now I want to apply an MVP model on this function to remove the function from the ViewController file.
How can I do that?
Do I need to publish more code to make it possible to create an answer for this question?
It is not a good practice to use any architectural pattern only for method. So assuming you are having a complete app with many classes or files.
An important thing is that it is not fixed/compulsory to use any specific pattern. It actually depends on the code, sometimes you end up writing much code just to handle a method. So try to think the optimal approach to make the code more testable and scalable.
But for your reference, you can check the following code:
On ViewController:
lazy var presenter:Presenter = Presenter(view:self)
#IBAction func onEmailValueChanged(_ sender: UITextField) {
presenter.validateHasMinimumLength(password: sender.text!)
presenter.validateHasCapitalLetter(password: sender.text!)
presenter.validateHasNumber(password: sender.text!)
presenter.validateHasSpecialCharacter(password: sender.text!)
}
//Adopting ViewController:PrensenterViewProtocol on ViewController
extension ViewController:PrensenterViewProtocol {
func updateLengthCheckmarkImageView(isHidden:Bool) {
passLengthCheckmarkImageView.isHidden = isHidden
}
func updateUpperCaseCheckmarkImageView(isHidden:Bool) {
passHasUppercaseCheckmarkImageView.isHidden = isHidden
}
func updateNumberCheckmarkImageView(isHidden:Bool) {
passHasNumberCheckmarkImageView.isHidden = isHidden
}
func updateSymbolCheckmarkImageView(isHidden:Bool) {
passHasSymbolCheckmarkImageView.isHidden = isHidden
}
func updateResetButton(isHidden:Bool) {
resetButton.isHidden = isHidden
}
}
PresenterView protocol as:
protocol PrensenterViewProtocol:NSObjectProtocol {
func updateLengthCheckmarkImageView(isHidden:Bool)
func updateUpperCaseCheckmarkImageView(isHidden:Bool)
func updateNumberCheckmarkImageView(isHidden:Bool)
func updateSymbolCheckmarkImageView(isHidden:Bool)
func updateResetButton(isHidden:Bool)
}
Presenter as:
class Presenter {
weak var view:PrensenterViewProtocol!
private var hasMinimumLength:Bool = false
private var hasCapitalLetter:Bool = false
private var hasNumber:Bool = false
private var hasSpecialCharacter:Bool = false
init(view:PrensenterViewProtocol) {
self.view = view
}
func validateHasMinimumLength(password:String?) {
hasMinimumLength = TextValidationHelper.validateHasMinimumLength(password: password)
self.view.updateLengthCheckmarkImageView(isHidden: hasMinimumLength)
checkAllValidations()
}
func validateHasCapitalLetter(password:String?) {
hasCapitalLetter = TextValidationHelper.validateHasCapitalLetter(password: password)
self.view.updateUpperCaseCheckmarkImageView(isHidden:hasCapitalLetter )
checkAllValidations()
}
func validateHasNumber(password:String?) {
hasNumber = TextValidationHelper.validateHasNumber(password: password)
self.view.updateNumberCheckmarkImageView(isHidden: hasNumber)
checkAllValidations()
}
func validateHasSpecialCharacter(password:String?) {
hasSpecialCharacter = TextValidationHelper.validateHasSpecialCharacter(password: password)
self.view.updateSymbolCheckmarkImageView(isHidden: hasSpecialCharacter)
checkAllValidations()
}
func checkAllValidations() {
let areAllValid:Bool = hasMinimumLength && hasCapitalLetter && hasNumber && hasSpecialCharacter ? false : true
self.view.updateResetButton(isHidden: areAllValid)
}
}

init class from another class swift

I have this ScreenSecurityWorker
final class ScreenSecurityWorker {
weak var delegate: ScreenSecurityWorkerDelegate?
private var isPassPhraseOn: Bool { return SettingsManager.shared.load(.passPhrase) }
private var isPassPhraseForced: Bool { return environment.forcePassPhraseEnabled }
var screenSecurityEnabled = false
inside my Mainrouter I am changing screenSecurityEnabled bool value to true at activateScreenSecurity
func activateScreenSecurity() {
guard !screenSecurityWorker.passwordSecurityInProgress else { return }
screenSecurityVC = ScreenSecurityBuiler.make()
app.dismissKeyboardOnTopVC()
app.window.rootViewController = screenSecurityVC
screenSecurityWorker.passPhareSuccess = false
screenSecurityWorker.screenSecurityEnabled = true
}
I am trying to reach to screenSecurityEnabled from ChatVC
but if I init it as
let screenSecurityWorker = ScreenSecurityWorker()
it has no values inside at all.
if I try like this it gives error
let screenSecurityWorker : ScreenSecurityWorker?
init(screenSecurityWorker: ScreenSecurityWorker){
self.screenSecurityWorker = screenSecurityWorker
}
I need this screenSecurityWorker.screenSecurityEnabled to change subviews in this function inside ChatVC
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if screenSecurityWorker!.screenSecurityEnabled {
let place = chatTextAreaContainerView.bounds.height + CGFloat(lastContentOffset ?? collectionView.contentOffset.y - chatTextAreaContainerView.bounds.height)
self.collectionView.contentOffset.y = place
screenSecurityWorker!.screenSecurityEnabled = false
}
}

How to toggle a boolean value using timer in Swift

I have a boolean called 'isMatched'. It is written in my Model. I use this boolean to show a view on a certain state of the view. However, I want the view to get hidden automatically after 0.5 second. I tried to use DispatchQueue.main.asyncAfter and also Timer, but always got error "Escaping closure captures mutating 'self' parameter". I have copied my Model below. Please advice me a solution.
I made the boolean var 'isMatched' true in the 'choose()' function.
import Foundation
struct WordMatchingGameModel<CardContent> {
private(set) var cards: Array<Card>
private(set) var words: Array<Word>
private(set) var gameDataSet: Array<String>
private(set) var matchedWord: String = ""
var isMatched: Bool = false
private var colors: [[String]] = [["AeroBlue", "AeroBlue"], ["BlueBell", "BlueBell"], ["PinkLavender", "PinkLavender"], ["Opal", "Opal"], ["CornflowerBlue", "CornflowerBlue"]]
mutating func choose(card: Card) {
if let choosenIndex = cards.firstIndex(matching: card) {
if !cards[choosenIndex].isTapped {
cards[choosenIndex].isTapped = true
cards[choosenIndex].bgColors = numberOfMatchedWords == 0 ? colors[0] : numberOfMatchedWords == 1 ? colors[1] : colors[2]
words.append(Word(id: cards[choosenIndex].id, content: cards[choosenIndex].content))
print(word)
if match() {
for index in 0..<cards.count {
if cards[index].isTapped { cards[index].isDisabled = true }
}
numberOfMatchedWords += 1
score += 10
matchedWord = word
isMatched = true
delay(interval: 0.5) {
self.isMatched = false
}
words.removeAll()
}
} else {
cards[choosenIndex].isTapped = false
cards[choosenIndex].bgColors = ["GradientEnd", "GradientStart"]
var tempWords = Array<Word>()
for index in 0..<words.count {
if words[index].id != cards[choosenIndex].id { tempWords.append(words[index]) }
}
words = tempWords
}
}
}
func delay(interval: TimeInterval, closure: #escaping () -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + interval, execute: closure)
}
func match() -> Bool {
for index in 0..<gameDataSet.count {
if gameDataSet[index] == word {
return true
}
}
return false
}
var word: String {
var array = Array<Character>()
for index in 0..<words.count {
array.append(words[index].content as! Character)
}
return String(array).capitalized
}
private(set) var numberOfMatchedWords: Int = 0
private(set) var score: Int = 0
init(numberOfCards: Int, gameDataSet: Array<String>, cardContentFactory: (Int) -> CardContent) {
cards = Array<Card>()
words = Array<Word>()
self.gameDataSet = gameDataSet
for index in 0..<numberOfCards {
let content = cardContentFactory(index)
self.cards.append(Card(id: index + 1, content: content))
}
cards.shuffle()
colors.shuffle()
}
struct Card: Identifiable {
var id: Int
var content: CardContent
var bgColors: [String] = ["GradientEnd", "GradientStart"]
var isTapped: Bool = false
var isDisabled: Bool = false
}
struct Word: Identifiable {
var id: Int
var content: CardContent
}
}
Create a reference for self inside the function and use that instant of self inside the delay function.
mutating func choose(card: Card) {
var createReference = self
....................
delay(interval: 0.5) {
createReference.isMatched = false
}
}

How to perform class nil checks

I have a class in Swift:
class myClass {
var theBool : Bool
init(theBool: Bool) {
self.theBool = theBool
}
init() {
self.theBool = false
}
}
Elsewhere in my code, I have this check:
classist = myClass()
if let daBool = someRandomBool {
classist.theBool = daBool
}
I want to know where to insert this check into the Class.
Simple solution: Declare the (required) init method with an optional parameter type and perform the check there
class MyClass {
var theBool : Bool
init(bool: Bool?) {
self.theBool = bool ?? false
}
}
let someRandomBool : Bool? = true
let classist = MyClass(bool: someRandomBool)
or – a bit different but still simpler – with a struct
struct MyStruct {
var theBool : Bool
}
let someRandomBool : Bool? = true
let classist = MyStruct(theBool: someRandomBool ?? false)

Button Click Twice

As we all know, to avoid clicking twice, we can set the code bellow on the tap method and add a HUD such as SVProgress.show().
isUserInteractionEnabled = false
After the network request, set it to true and SVProgress.dismiss().
I wonder if there is a method to extract the function for those button which needs to send a request. I have thought to use method swizzling. Add this to the button extension, the codes is bellow. It seems not good. Do you guys have some good ways to extract the function? Using inheritance, protocol or something else?
extension UIButton {
private struct AssociatedKeys {
static var cp_submitComplete = "cp_submitComplete"
static var cp_defaultMessage:String = NSLocalizedString("Loading", comment: "prompt")
static var cp_customMessage = "cp_customMessage"
}
var submitNotComplete: Bool {
get {
let objc_Get = objc_getAssociatedObject(self, &AssociatedKeys.cp_submitComplete)
if objc_Get != nil {
if let objc_Get = objc_Get as? Bool, objc_Get == true {
return true
}
return false
} else {
return false
}
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.cp_submitComplete, newValue as Bool, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
if !newValue {
isUserInteractionEnabled = true
SVProgressHUD.dismiss()
}
}
}
var customMessage: String {
get {
let cp_customMessage = objc_getAssociatedObject(self, &AssociatedKeys.cp_customMessage)
if let message = cp_customMessage {
return message as! String
} else {
return AssociatedKeys.cp_defaultMessage
}
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.cp_customMessage, newValue as String, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
override open class func initialize() {
if self == UIButton.self {
DispatchQueue.once(NSUUID().uuidString, block: {
let systemSel = #selector(UIButton.sendAction(_:to:for:))
let swizzSel = #selector(UIButton.cpSendAction(_:to:for:))
let systemMethod = class_getInstanceMethod(self, systemSel)
let swizzMethod = class_getInstanceMethod(self, swizzSel)
let isAdd = class_addMethod(self, systemSel, method_getImplementation(swizzMethod), method_getTypeEncoding(swizzMethod))
if isAdd {
class_replaceMethod(self, swizzSel, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
} else {
method_exchangeImplementations(systemMethod, swizzMethod);
}
})
}
}
private dynamic func cpSendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
cpSendAction(action, to: target, for: event)
if submitNotComplete {
//begin submit
isUserInteractionEnabled = false
SVProgressHUD.show(withStatus: customMessage)
}
}
}
I think it's a bad idea to handle this kind of logic in UIButton. I would rather make the view controller responsible for enabling/disabling the button.
func handleTap(_ sender: UIButton) {
sender.isEnabled = false
SVProgressHUD.show(withStatus: customMessage)
doSomeTaskAsync(withCompletion: {
sender.isEnabled = true
SVProgressHUD.dismiss()
})
}