Here my code:
key0 = {1}
key1 = {A}
Loop
{
If (GetKeyState(key0, "P"))
{
Send {key1}
}
else
{
Send {key0}
}
sleep 500
}
F12::ExitApp
Not works correctly. Cannot use keys as variables. Can anyone explain to me how to do this?
Correct syntax in the send command would be with % sign around the variable. Also you have figure brace in declaration {A} - should be just A if you use {A} literal, then it is not possible to do e.g. {a down}.
E.g. you pass key0 to Getkeystate() - the first argument there must be just key name unlike the curly braces in the send command.
Try this:
key0 = 1
key1 = A
loop
{
if getkeystate(key0, "P")
{
send {%key1%}
}
}
Related
I'm just trying to perform a very simple for loop in a function and yet it's not working as expected. However, If I replace the checkForImage() call with the contents of the function, it works. Is there something wrong with my syntax?
dict := {"img1": "shark","img2": "blue","img3": "red"}
sendAnswers(answer) {
msgbox, %answer%
}
checkForImage() {
MsgBox hi
for key, val in dict
MsgBox %val%
return
}
^z::
checkForImage()
I get a message box with "hi" but the loop doesn't seem to do anything.
I am using Version 1.1.30.00
Your syntax is correct. The function simply cannot "see" dict. There are a couple of ways to solve this problem, see Global.
First method: Only dict is global, all other variables are accessible only within checkForImage(). If you're accessing only dict or a couple more global variables, this method is recommended.
checkForImage() {
global dict ; Only dict will be global (accessible outside this function)
myLocalVariable := 0 ; This is accessible only within this function
MsgBox hi
for key, val in dict
MsgBox %val%
}
Second method: ALL variables within the function is global.
checkForImage() {
Global ; dict and all other global variables will be accessible
myNotLocalVariable := 0 ; This is accessible even outside this function
MsgBox hi
for key, val in dict
MsgBox %val%
return
}
Third method: Declare dict as a super-global variable.
global dict := {"img1": "shark","img2": "blue","img3": "red"}
checkForImage() {
MsgBox hi
for key, val in dict
MsgBox %val%
return
}
Please check the attached code below.
Why Pattern raise Error? , "No exact matches in call to subscript"
What is the difference between A and B?
This is the text import from the console.
3
Everest 8849
K2 8611
Kangchenjunga 8586
This is code
struct Mountain {
let name: String
let height: Int
}
func highestmountain() {
var mtList = [Mountain]()
let N = Int(readLine()!)!
(0..<N)
.forEach { _ in
/* Pattern A */
readLine()!
.split(separator: " ")
.forEach {
mtList.append(Mountain(name: "\($0[0])", height: Int("\($0[1])")!)) // Error: No exact matches in call to subscript
}
/* Pattern B */
let reads = readLine()!
.split(separator: " ")
mtList.append(Mountain(name: "\(reads[0])", height: Int("\(reads[1])")!)) // works!
}
}
In Pattern A, you're using split(" ") which creates an array of Strings (or, more specifically, String.SubSequence), and then you call forEach on that array.
forEach calls the attached closure for each item in the array. So, with your second input line, for example, on the first forEach, $0 will be "Everest" and on the second call, it'll be 8849. However, in your code, you're attempting to get $0[0], but $0 is not an array -- it's a single String.SubSequence. Thus the error about the subscript.
Your second approach (Pattern B) works because reads is an Array of String.SubSequence, so using a subscript (ie the [0]) works.
Unrelated to your question, but it's worth noting that using subscripts like [1] will fail and crash the app if you haven't first checked to make sure that the array has enough items in it. Force unwrapping with a ! (like you do with Int(...)!) can also cause crashes.
I have a function (actually several instances of this), but there are times that it may return a list of several elements, and there are times that it may return a single element. I want the function to return an array ([System.Object[]]) every time so that (on the receiving end), I can always anticipate it being an array and index into it, even if I am just pulling the 0th element.
I've tried casting the return type multiple ways (see code below) ... including (for example) return #("asdf"), return [System.Object[]]#("asdf") and similar, but it seems that the only to get a consistent behavior is to add a second null element in the array ... which feels wrong to me. (See code below)
function fn1 {
return #("asdf")
}
function fn2 {
return [array]#("asdf")
}
function fn3 {
return [System.Object[]]#("asdf")
}
function fn4 {
# This works but with the side effect of sending a null string that is not actually necessary
return #("asdf",$Null)
}
$v = fn1 # Same for fn2, fn3.
$v.GetType().Name # Expected: Object[], Actual: String
$v[0] # Expected: "asdf", Actual: "a"
$v = fn4
$v.GetType().Name # Expected: Object[], Actual: Object[]
$v[0] # Expected: "asdf", Actual: "asdf"
If I understand your question, you can use the , operator when returning the value; e.g.:
function fn1 {
,#("asdf")
}
The function will output a single-element array.
As an alternative to wrapping in an extra array, use Write-Output -NoEnumerate:
function fn1 {
Write-Output #('asdf') -NoEnumerate
}
or, in cmdlet-bound/advanced functions prior to version 4.0:
function fn1 {
[CmdletBinding()]
param()
$PSCmdlet.WriteObject(#('asdf'), $false)
}
What is the mechanism of declaring w/o value in Swift5 ? Does the first assign become the real declaration ?
And, should we avoid to declare without value in Swift?
var v:String;
if true {
v = "Hello"
print(v) // print "Hello" when without the print below
}
print(v) // variable 'v' used before being initialized
var v:String="";
if true {
v = "Hello"
print(v) // print "Hello"
}
print(v) // print "Hello"
Well, the message is not very helpful, and that's the problem. This pattern (which I call computed initialization) is perfectly legal and useful and — amazingly — you can even use let instead of var. But you must initialize the uninitialized variable by all possible paths before you use it. So you have:
var v:String
if true {
v = "Hello"
}
print(v) // error
But hold my beer and watch this:
var v:String
if true {
v = "Hello"
} else {
v = "Goodbye"
}
print(v) // fine!
Or even:
let v:String
if true {
v = "Hello"
} else {
v = "Goodbye"
}
print(v) // fine!
Amazing, eh?
Now, you might say: OK, but true will always be true so it's silly to make me fulfill the "all paths" rule. Too bad! The compiler insists anyway, and then lets you off later with a warning that the else won't be executed. But a warning lets you compile; an error doesn't. The truth is that your example is very artificial. But this is a real-life possibility:
let v:String
if self.someBoolProperty {
v = "Hello"
} else {
v = "Goodbye"
}
print(v) // fine!
Not only is this sort of thing legal, it is actually the pattern that Apple recommends under certain slightly tricky circumstances. For instance, it is used in Apple's own example code showing how to use the Swift 5 Result struct:
let result: Result<Int, EntropyError>
if count < AsyncRandomGenerator.entropyLimit {
// Produce numbers until reaching the entropy limit.
result = .success(Int.random(in: 1...100))
} else {
// Supply a failure reason when the caller hits the limit.
result = .failure(.entropyDepleted)
}
So this is because swift compiles your code and notices that your value var v:String; is undeclared before being used which makes it an "optional" value. Even though you are assigning it within the if statement, if you were to get rid of the true value it is possible that the if statement would never run therefore no value will ever be stored in v, thus it would be used before "assigned".
So to answer your question if you want your value to be an optional and possible empty value declare v as the following var v:String? if you would like it to be a non-optional value with a value always stored within v you should declare it as the following var v = "". Swift will interpret this declaration as a String.
To answer your second question, defining without values in swift is 100% allowed, it really just depends on how you want to handle your code. I use optional values in my code all the time, but I don't necessarily like optionals so i usually like to declare my values such as var v = "" that way if the value is empty my UI or whatever else i'm manipulating won't break. But if i need to ensure a value is present i will have to make my value optional so i can use an if statement to check whether it's a valid value or not.
Shorter version of what I'm trying to say is, you are receiving the compiler warning because you are declaring v as a non-optional value rather than an optional value.
I am trying to create a function within the ViewController class. I want to loop through a string and count the number of times a specific character occurs. Code looks like this:
var dcnt:Int = 0
func decimalCount(inputvalue: String) -> Int {
for chr in inputvalue.characters {
if chr == “.” {
++dcnt
}
}
return dcnt
}
The input string comes from a UILabel!
I get a warning: Immutable value ‘chr’ was never used.
How can I fix this problem
The problem, as so often in Swift, lies elsewhere. It's the curly quotes. Put this:
if chr == "." {