Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Greeting, Everyone!
I'm wondering how implement ability to multiply methods associated to certain class
For example I've got custom class of UITextField
and I want to configurate it any way I need.
How can I handle it to get result kinda myTextField.configure().addSomeExtraFeatures().andOneMoreMethod()
like in RxSwift viewModel.fetch().rx.asObservable.bind(to: ...
Can anyone show me some direction to resolving? :) Any clue, please ๐
Even how properly call this process will be useful ๐๐๐
Return self in every function and use #discardableResult attribute so no need to care about the return.
For more about #discardableResult: https://www.avanderlee.com/swift/discardableresult/
Your UITextField custom class should be like this.
class CustomTextField: UITextField {
#discardableResult
func configure() -> Self {
// Do your code here
return self
}
#discardableResult
func addSomeExtraFeatures() -> Self {
// Do your code here
return self
}
#discardableResult
func andOneMoreMethod() -> Self {
// Do your code here
return self
}
}
Usage:
Create an instance of CustomTextField and call function.
let customTextField: CustomTextField = CustomTextField()
customTextField.configure().addSomeExtraFeatures()
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last month.
Improve this question
There are two way to call variables & methods in ViewController:
Case 1: Using self
import UIKit
class ViewController: UIViewController {
var count = 0;
override func viewDidLoad() {
super.viewDidLoad()
self.count = 1
self.myFunc()
}
func myFunc(){
...
}
}
Case 2: Access directly
import UIKit
class ViewController: UIViewController {
var count = 0;
override func viewDidLoad() {
super.viewDidLoad()
count = 1
myFunc()
}
func myFunc(){
...
}
}
Should I call variables and methods with the instance `self` in the scalable project at everywhere, or access them directly, as calling with the instance is a good way?
There is any difference between those two a function-calls in a class:
self.myFunc()
VS
myFunc()
It is working in both ways. It make any difference?
The best practice would be that we should avoid unnecessary calls with self.
It should be used when it's necessary like you want have a initialiser or a function with same name as your variable then you can use self to distinguish between both.
Another place would be the clousure.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
We are unable to select auto-suggestion when loading in UITextfields and UITextviews.
We are using IQkeyboardManager for auto-scroll UItextfield visibility.
This issue is by gestureRecognizer
Example:
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
let className = String(describing: self.classForCoder)
if className == "TUIPredictionView" {
return true
}
return (touch.view == self)
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have 2 controllers. Let's call them A and B
Controller A is the main one and I have to check in viewWillAppear() if user exists.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let user = currentUser else {
let setupProfileViewController = SetupProfileViewController()
print("Current user empty")
let navigationController = UINavigationController(rootViewController: setupProfileViewController)
present(navigationController, animated: true, completion: nil)
return
}
}
Here user sees controller B and needs to fill data. On submit I validate all fields and create new User. Then I am dismissing B controller.
self.dismiss(animated: true) {
let matchViewController = MatchViewController()
matchViewController.currentUser = user
matchViewController.kolodaView.reloadData()
}
But what happens it A controller says there is no value in currentUser. I appreciate any advice on how to solve this.
The problem here is that
let matchViewController = MatchViewController()
matchViewController.currentUser = user
is a new instance of the class not the currently presented one , so setting user to it has no value , you have to use delegate or share your user via singleton class
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
#IBOutlet weak var button: NSButton!
#IBOutlet weak var password: NSSecureTextField!
#IBAction func buttonclick(_ sender: Any) {
if(password.text == "test"){
}
}
this said it didn't work or something
now I want it to check if the password is like test and then it enters
but now i tried to do it like the normal text box but it wouldn't work ether
Replace with
if password.stringValue == "test" {
print("correct")
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm having trouble with creating an app in Swift2.
The App has an image of a wolf which changes every 0.4 seconds to reveal a running wolf.
However I have Bool errors in Swift 2 that I cannot fix.
I also have issues with declaring a void function.
Any help would be appreciated.
#IBAction func startRunnng(sender: UIButton)
{
tmrRun = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "update", userInfo: nil, repeats: true)
btnGo.userInteractionEnabled = NO;
btnStop.userInteractionEnabled = YES;
sliSpeed.userInteractionEnabled = NO;
}
#IBAction func stopRunnng(sender: UIButton)
{
tmrRun invalidate()
btnGo.userInteractionEnabled = YES;
btnStop.userInteractionEnabled = NO;
sliSpeed.userInteractionEnabled = YES;
}
void takeaBound
{
String *imageName = [NSString stringWithFormat:#"wolf%d.png", pic];self.imvWolf.image = [UIImage imageNamed:imageName];
pic += 1;
if (pic == 8)
pic = 0;
}
override func viewDidLoad() {
super.viewDidLoad()
pic = 0;
// Do any additional setup after loading the view, typically from a nib.
}
Boolean values in Swift are true and false, YES and NO is used in Objective C.
So in your stopRunning method for instance, you should write:
#IBAction func stopRunnng(sender: UIButton)
{
tmrRun invalidate()
btnGo.userInteractionEnabled = true
btnStop.userInteractionEnabled = false
sliSpeed.userInteractionEnabled = true
}
(sidenote, you don't need the ; in Swift either)
About the void function. In Swift you write the return type AFTER your method declaration, starting with a ->. Like so:
func takeABound(parametersWouldGoHere) -> ()
For a void method you can write () or Void or, as you'll often do, don't write anything at all.
func takeABound(parametersWouldGoHere)
As it says in "The Swift Programming Language" in the chapter about functions
Because it does not need to return a value, the functionโs definition does not include the return arrow (->) or a return type.
You can read more about functions, booleans and the like in "The Swift Programming Language", there is a nice chapter called "A Swift Tour" that will introduce you to many of the basic things.
Hope that helps