Swift Version 2 Bool [closed] - boolean

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

Related

Swift: Should I Use `self` in ViewController Methods in Swift/iOS? [closed]

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.

Is it possible to print the UI Content Presented on Screen | Swift [closed]

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 1 year ago.
Improve this question
Essentially I have UI View that presents on screen and takes up the entire phones screen, I would like to be able to select a button and print what is currently presented on the screen over AirPrint. Is something like this possible? Are there libraries or other ways of achieving this?
I appreciate any help.
Step 1: First for converting your UIView into image , add this extension:
extension UIView {
func toImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
Step 2: Add this into your button action for print:
#IBAction func printButton(sender: AnyObject) {
let printInfo = UIPrintInfo(dictionary:nil)
printInfo.outputType = UIPrintInfoOutputType.General
printInfo.jobName = "My UIView"
let printController = UIPrintInteractionController.sharedPrintController()
printController.printInfo = printInfo
printController.printingItem = self.view.toImage()
// If you want to specify a printer
guard let printerURL = URL(string: "Your printer URL here, e.g. ipps://HPDC4A3E0DE24A.local.:443/ipp/print") else { return }
guard let currentPrinter = UIPrinter(url: printerURL) else { return }
printController.print(to: currentPrinter, completionHandler: nil)
printController.presentFromRect(self.view.frame, inView: self.view, animated: true, completionHandler: nil)
}

Multiply methods to property in Swift [closed]

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()

How to pass data back with no storyboard using Swift [closed]

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

how to validate user input for game without them having to press a 'submit' button each time [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've got any array of ints numbers = [3,12,9]
The user needs to type in each number. if they write the correct number, the text field resets and they type in the next number. no hitting submit button.
function so far isn't working.
func UserInput(textView:UITextView!)
{
if(input == nil )
{
return;
}
var inputs = input!.text.toInt();
var x = 0
while (x < numbers.count){
if( inputs != nil)
{
if(inputs! == numbers[x] )
{
println("Correct!");
input.text = ""
x++
//println(x)
}
else
{
println("Incorrect!");
}
}
}
}
You can use event EditingChanged from your textField. With that you can get live input from user this way:
import UIKit
class ViewController: UIViewController {
var numbers = [3,12,9]
#IBOutlet weak var input: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
input.addTarget(self, action:"edited", forControlEvents:UIControlEvents.EditingChanged)
}
func edited() {
for item in numbers {
if input.text == "\(item)" {
println("correct")
//I set here timer so if user enter correct input then it will remove text after some time
var timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("resetText"), userInfo: nil, repeats: false)
} else {
println("incorrect")
}
}
}
//This method will call after some time use if user enter correct input
func resetText(){
input.text = ""
}
}
Hope it will help.