Cannot assign to property: 'desc' is a 'let' constant
class infoViewController: UIViewController {
var result:Data1!
override func viewDidLoad() {
saveToJsonFile(result2 : &result!)
}
func saveToJsonFile(result2 : inout Data1) {
result2.data[1].monday[1].desc = "cos"
}
}
struct Data1{
var data: [Monday]
}
struct Monday {
var monday: [Desc]
}
struct Desc{
let desc: String
}
If you try as above you will get "Cannot assign to property: 'desc' is a 'let' constant" error.
So you need to change the let into var, because let is immutable.
class infoViewController: UIViewController {
var result:Data1!
override func viewDidLoad() {
saveToJsonFile(result2 : &result!)
}
func saveToJsonFile(result2 : inout Data1) {
result2.data[1].monday[1].desc = "cos"
}
}
struct Data1{
var data: [Monday]
}
struct Monday {
var monday: [Desc]
}
struct Desc{
var desc: String
}
ViewModel function: Cannot assign to value: 'error' is a 'let' constant
I'm not entirely sure what you're trying to do with SingUpError
(do you mean SignUpError
?) but this is my attempt at it. Basically I am moving the enum out of the func and I make the func return a String
class SingUpError {
enum ErrorMessage: String {
case noName = "Name cannot be empty"
case noLastName = "Last name cannot be empty"
case noEmail = "Email cannot be empty"
case noPassword = "Password cannot be empty"
}
func errorMessage(name: String?, lastName: String?,email: String?, password: String?) -> String? {
var message: String?
if name == nil || name.isEmpty {
message = ErrorMessage.noName.rawValue
} //else if lastName...
return message
}
}
and then in submitData
if let error = vm.errorMessage(name: name, lastName: lastName, email: email, password: password) {
nameErrLbl.text = error
}
Cannot assign to property: 'card' is a 'let' constant in a for loop in swift
card is ofcourse a let constant, to make it variable use: for var card in cards
Cannot assign to property: 'item' is a 'let' constant (Swift 4)
You could do a loop over a range and then access categories
by index to get a mutable object:
for i in 0..<(categories?.count)! {
var item = categories[i]
item.element?.availability = quotient + (item.offset < remainder ? 1 : 0)
}
I would also recommend avoiding the use of force unwraps by guarding around categories
:
guard let categories = categories else {
//Handle a nil categories array here
return
}
cannot assign through subscript in swift
Parameters are immutable by default. To make a parameter mutable, you need to add an inout
modifier.
However, seeing that your method returns an array, you probably don't want the parameter to be modified as well. You should instead make a copy of the parameter, modify that, and return the copy:
func InsertionSort (numbers : [Int]) -> [Int]
{
var maxNumber = 0
var j = 0
let size = numbers.count-1
var numbersCopy = numbers // <------ note this line
for (i,number) in numbers.enumerated()
{
j = i + 1
for index in j...size
{
if numbers[index] > number
{
maxNumber = numbers[index]
// note how I modified the copy here
numbersCopy[index] = numbers[i]
numbersCopy[i] = maxNumber
}
}
}
return numbersCopy
}
Error trying to save to core data in SwiftUI
selected = coreDataViewModel.savedCart[0].paymentMethod
is trying to assign a value from your model to selected
, which is a constant. Shouldn't it be
coreDataViewModel.savedCart[0].paymentMethod = selected
In terms of your second question, it's hard to say given the information you've provided. One thing I would advise is to use enums with string raw values for things like payment methods, and have calculated properties on your managed object models to access them so you're not passing strings around:
var paymentMethod: PaymentMethod {
get { PaymentMethod(rawValue: rawPaymentMethod) ?? .turnips }
set { rawPaymentMethod = newValue.rawValue }
}
Related Topics
Storyboard Tableview with Segues to Multiple Views
How to Detect That Parameter Is a Tuple of Two Arbitrary Types
How to Create String Split Extension with Regex in Swift
Spawning a Spritekit Node at a Random Time
Convert Firdatasnapshot to Custom Type
Cannot Use Mutating Member on Immutable Value of Type
How to Get Day and Month from Date Type - Swift 4
Swift Safe Area Layout Guide and Visual Format Language
Lazy Readonly Property in Swift
Access Properties via Subscripting in Swift
How to Use Storyboards with Spritekit Using Swift
Swift: Extract Float from Byte Data
Remove Multiple Indices from Array
Blue Highlighting/Focus Ring on Catalyst App
Why Does Swift Provide Both a Cgrect Initializer and a Cgrectmake Function