Swift increment Int! not working
This error occurs with all inout
parameters and should be considered a bug.
The usual way how inout
parameters work is that their getter gets called once and their setter at least once. In this case the getter returns an Int!
:
let num: Int! = 0
let num2 = num // is inferred to be of type Int!
so the signature of the getter/setter is not the same as the function/operator expects. But the compiler should implicitly unwrap the value if it gets assigned to an Int
or passed like so:
var num3 = 0 // is of type Int
num3 = num // gets automatically unwrapped
// also with functions
func someFunc(i: Int) {}
someFunc(num) // gets automatically unwrapped
Sidenote:
Almost the same error occurs if you want to pass an Int
to a function with an inout
parameter of type Int!
. But here it is obvious why this doesn't work (logically): The setter of an Int
never takes a nil
.
Swift :Issue with increment Int inside String
You need to make your var
static
to share it between class instances:
class DownloadFile : NSObject {
static var number = 1
init(url : String) {
...
DownloadFile.number += 1
...
}
}
Cannot increment a number in swift
You are trying to add a value to a let
and let is a constant, you can not assign values to a let
after you initialized it.
Apple docs:
Constants and variables associate a name (such as maximumNumberOfLoginAttempts or welcomeMessage) with a value of a particular type (such as the number 10 or the string "Hello"). The value of a constant cannot be changed once it is set, whereas a variable can be set to a different value in the future.
var
is what you want
Can You Increment A Variable Inside A Function Call In Swift?
The expression featCount += 1
– unlike unsupported featCount++
– doesn't return anything (that's what the error states) so you need an extra line.
If the variable is supposed to be incremented before being used in the function
featCount += 1
features.insert(σ.min(values: yArray)!, at: featCount)If the variable is supposed to be incremented after being used in the function
features.insert(σ.min(values: yArray)!, at: featCount)
featCount += 1
Swift :Issue with increment Int inside String
You need to make your var
static
to share it between class instances:
class DownloadFile : NSObject {
static var number = 1
init(url : String) {
...
DownloadFile.number += 1
...
}
}
Can't increment variable in Swift - not throwing any errors
You have to move the text setting in an extra method. Right now the text only gets set in viewDidLoad - that function will not be triggered more than once however.
change your viewDidLoad to something like
updateUI()
add a new function
func updateUI() {
if teamCounter % 2 == 0 {
scoreLabel.text = "Score: \(teamOneScore)"
} else {
scoreLabel.text = "Score: \(teamTwoScore)"
}
}
and call that method as the last thing in the button action:
@IBAction func buttonPressed(sender: AnyObject) {
if timer.valid && teamCounter % 2 == 0 {
++teamOneScore
} else if timer.valid && teamCounter % 2 != 0 {
++teamTwoScore
}
updateUI()
}
Swift, increment a number on each button tap
Do something like this:
class FooViewController: UIViewController {
var keyCounter = 0
@IBAction func doSomething(_ sender: Any) {
keyCounter = keyCounter + 1
print("Key Counter = ",keyCounter)
}
}
Why does the increment not work in this case?
Your expression is grouped as
(
binaryNumber[i] == 1 || binaryNumber[i] == 0
? i++
: printf("Binary bits can only be 0 or 1!\n")
), i--;
and compiles with defined behaviour since printf
returns an int
type and ,
is a sequencing point. In other words, i
is always decremented.
Note that the bit between ?
and :
is grouped as if it were in parentheses, but that's not true for the part after :
. In other words, somewhat bizarrely, the expression
!(binaryNumber[i] == 1 || binaryNumber[i] == 0)
? printf("Binary bits can only be 0 or 1!\n"), i--
: i++
would behave the way you'd expect although relying on that is probably a step too far, even for me.
Related Topics
Swift Override Static Method Compile Error
Nstextfield Non-System-Font Content Clipped When Usessinglelinemode Is True
Xcodebuild Commands Failed to Generate Ipa
Issue with Returning a Directory Enumerator from Nsfilemanager Using Enumeratoraturl in Swift
Accessibility Custom Actions Aren't Announced in Swift
Converting Scanlocation from Utf16 Units to Character Index in Nsscanner (Swift)
Cannot Get Newpassword to Generate Strong Password and Autofill
How to Get Textfields from Static Cells in UItableviewcontroller? Swift
How to Update UIviewrepresentable with Observableobject
Remove from Array of Anycancellable When Publisher Finishes
Performseguewithidentifier Not Working If Called from Viewdidload
Realm: Predicate Returning Lazyfiltercollection - How to Convert to Results<T>
Member Operator '==' Must Have at Least One Argument of Type
Combined Watch Os and iOS Framework