I defined Dictionary in Swift, with enum as the key and struct as value. At runtime I want to add value to the dictionary for a given enum key, however I get the following error:
'#lvalue $T9' is not identical to '(MyEnum, MyData)'
enum MyEnum {
case A, B, C
}
struct MyData {
var x : Int
var y : Int
init(x:Int, y: Int) {
self.x = x
self.y = y
}
}
class Tester {
let myDictionary = [MyEnum : MyData]()
func dummy() {
self.myDictionary[MyEnum.A] = MyData(x: 1, y: 2) // <-- error in this line
}
}
Any idea how to do it properly ?
The problem is that you’ve declared myDictionary with let rather than var. Switch it to var and your code will work as expected.
Dictionaries are structs, which are a “value” type. That means when you declare them with let, they are frozen forever with the value they were assigned. You can’t call methods that change their value, including assigning to them via a subscript ([ ]).
Unfortunately the error message isn’t super-helpful. Ideally it would read something like “attempt to modify immutable value myDictionary”.
Don’t let this put you off using let though – it’s a great practice to use let by default unless you know you need to change a value. But in this instance you do, so you need to use var.
Related
In this simplified example, I make a propertyWrapper of an UInt to hold a Natural Number (an Integer > 0). My own example uses a more complex filter but this shows the issue. Rather than find a workaround, the point of the question is to shed light on the confusing (to me) error.
Assigning it to a simple UInt brings the error message listed.
Using its wrappedValue property as in the following line works fine. But surely the whole point of the wrapping is to be able to treat it as an UInt as returned by the get?
The error "cannot assign value of type 'NonZero' to type 'UInt" appears to undermine the whole point of the wrapper type. What am I misunderstanding?
Xcode 11.0
import Foundation
#propertyWrapper
struct NonZero {
private let myNumber : UInt
init(n : UInt)
{
if ( n == 0){fatalError(" cannot be 0")}
myNumber = n
}
var wrappedValue: UInt {
get { return myNumber }
}
}
struct Nums {
var num :UInt
init( nz: NonZero){
num = nz //error message "cannot assign value of type 'NonZero' to type 'UInt"
num = nz.wrappedValue //no error
}
}
This is not how property wrappers work. The code:
init(nz: NonZero)
declares nz to have the type NonZero – which is just the struct defined earlier, NonZero here is not being used as a wrapped property.
A wrapped property is used as an attribute on a property declaration, for example:
#NonZero var num : UInt = 1
[Putting that in the code requires changing the parameter label of NonZero's init to be changed to wrappedValue, e.g.:
init(wrappedValue : Uint) { ... wrappedValue ... }
]
You could write the init of struct Nums as:
init(nz: UInt)
{
num = nz // ok *after* you add a `set` to `NonZero`, will `fatalError` if `nz` is zero
let unwrapped : UInt = num // ok
print(unwrapped) // so you see it worked
}
Keep exploring! You might find SE-0259 Property Wrappers (Apple) and Swift Property Wrappers (NSHipster) useful.
HTH
I want to initialize every time a struct with dictionaries. Later, I'm going to use its properties instead a dictionary's keys and values - it seems rather easier. However, when I try the code below, it tells me that "Return from initializer without initializing all stored properties" and "1. 'self.one' not initialized" and "2. 'self.two' not initialized". My question is how to initialize a struct from a dictionary, so that I have basically a struct with the contents of the dictionary? Or how to transform it into struct?
struct Blabla {
var one: String
var two: [Int]
init(three: [String: [Int]]) {
for i in three {
self.one = i.key
self.two = i.value
}
} ERROR! - Return from initializer without initializing all stored properties
}
struct Blabla {
var one: String
var two: [Int]
init(three: [String: [Int]]) {
one = ""
two = []
for i in three {
self.one = i.key
self.two = i.value
}
} ERROR! - Return from initializer without initializing all stored properties
}
for in clause may have zero runs, in which case struct properties will not be initialized. You have to provide default values (or emit fatalError if you really need to).
While I think your example is pure synthetical, there is no need to loop through array, you can set properties to its last entry.
The issues is that if three is an empty Dictionary, the instance properties one and two don't get initialised. Also, you are overwriting the properties in each iteration of the for loop and the compiler cannot guarantee that there will be any iterations of the loop in compile-time, hence the compiler error.
You could make the initialiser failable to account for this by checking that the dictionary actually contains at least one key-value pair and assigning that first key-value pair to your properties.
struct Blabla {
var one: String
var two: [Int]
init?(three: [String: [Int]]) {
guard let key = three.keys.first, let value = three[key] else { return nil }
one = key
two = value
}
}
However, you should rethink what it is that you are actually trying to achieve, since with your current setup you have a mismatch between your init input values and the properties of your struct.
This code should compile, but it feels unsafe to me to initialize a Struct in this way because:
It assume your dictionary has values in it.
Your stored properties will always have the last value you looped through.
In order to pull values out to satisfy the compiler you need to force unwrap them. (With Dávid Pásztor's guard-letting approach, this can be avoided)
struct Blabla {
var one: String
var two: [Int]
init(three: [String: [Int]]) {
self.one = three.keys.first!
self.two = three[three.keys.first!]!
}
}
let input = ["pizza": [1,2]]
let l = Blabla(three: input)
If I were you I would let the memberwise initializer that you get for free do its thing and provide either a specialized initializer to handle your case of taking a Dictionary as input or move that parsing to another function/class/etc....
The compiler error is clear: If the dictionary is empty the struct members are never initialized. But the code makes no sense anyway as each iteration of the dictionary overwrites the values.
Maybe you mean to map the dictionary to an array of the struct
struct Blabla {
let one: String
let two: [Int]
}
let three = ["A":[1,2], "B":[3,4]]
let blabla = three.map{Blabla(one: $0.key, two: $0.value)}
print(blabla) // [Blabla(one: "A", two: [1, 2]), Blabla(one: "B", two: [3, 4])]
struct blabla{
var a : string
var b : [int] = []
init(_ data: [string:[int]]){
// whatever you want to do
}
}
Please have a look at the following code and note the compiler error on the last line:
class C {
var value: Int
init( _ value: Int) { self.value = value }
}
let array1 = [C(1), C(2), C(3)]
array1[0].value = 4
struct S {
var value: Int
}
let array2 = [S(value: 1), S(value: 2), S(value: 3)]
array2[0].value = 4 // Error: Cannot assign to property: 'array2' is a 'let' constant
From the compiler error I want to conclude that the item at index 0 is being read from array2, modified, and then written back to array2. What else could produce the result that there is an attempt to modify array2? But, if my reasoning is correct, then why does the same thing not happen with array1?
Classes Are Reference Types
Unlike value types(struct), reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used instead.
Please refer Apple documentation about Class & struct
I've been trying to figure out why it's possible to add objects to a let constant dictionary, but cannot find the answer.
The code below works, but I've always thought of let constants as immutable objects.
Anyone that can shed som light on this?
// Create dictionary to allow for later addition of data
let data: NSMutableDictionary = ([
"firstname" : "john",
"lastname" : "doe"
])
// Add email to dictionary if e-mail is not empty
if email != "" {
data.setValue(email, forKey: "email")
}
In Swift the let keyword is used to declare a constant. However, there are some things you need to be aware of depending on if you are declaring a constant for a reference type or a value type.
Reference Type
// Declare a class (which is a reference type)
class Foo {
var x = 1
}
// foo's reference is a constant.
// The properties are not unless they are themselves declared as constants.
let foo = Foo()
// This is fine, we are not changing the foo reference.
foo.x = 2
// This would result in a compiler error as we cannot change
// the reference since foo was declared as a constant.
foo = Foo()
Value Type
// Declare a struct (which is a value type)
struct Bar {
var y = 1 // Note the var
}
// bar's value is a constant. The constant nature of the value type properties
// that are part of this value are subject to bar's declaration.
let bar = Bar()
// This would result in a compiler error as we cannot change
// the value of bar.
bar.y = 2
Mixture of Reference and Value Types
Generally you wouldn't want to have a reference type property defined on a value type. This is for illustrative purposes.
// Declare a struct (which is a value type)
struct Car {
let foo = Foo() // This a reference type
}
// The value is a constant. But in this case since the property foo
// is declared as a constant reference type, then the reference itself
// is immutable but its x property is mutable since its declared as a var.
let car = Car()
// This is fine. The x property on the foo reference type is mutable.
car.foo.x = 2
Since NSMutableDictionary is a class, declaring the reference as a constant ensures you cannot change its reference, however its mutable properties can be changed.
The comment on your question from #vadian regarding NSMutableDictionary should be noted.
I want to check if there is a value in a array and if so assign to a String using a if-left statement:
if let scoreValue = scoreValueArray[element!]{
// do something with scoreValue
}
Error: Bound value in a conditional binding must be of optional type
So tried changing the ! to ? but error persists.
Any input appreciated.
scoreValueArray is an array of strings, where a String value is appended to array if a condition is met, then array is saved to NSUserdefaults.
So element is a int which corresponds to a index in the array, bt only if the index is occupied with a String, so
scoreValueArray[element!]
could return an 'Index out of bounds', hence want to use the if-let.
Although the accepted answer clearly puts why optional binding is not available in the current implementation, it doesn't provide with a solution.
As it is shown in this answer, protocols provide an elegant way of safely checking the bounds of an array. Here's the Swift 2.0 version:
extension Array {
subscript (safe index: Int) -> Element? {
return indices ~= index ? self[index] : nil
}
}
Which you can use like this:
let fruits = ["Apple", "Banana", "Cherry"]
if let fruit = fruits[safe: 4] {
// Do something with the fruit
}
It's not clear what type your scoreValueArray is, but for the sake of this answer, I'm going to assume it's an array of Int.
var scoreValueArray: Array<Int>
Now, if we look the definition of the Array struct, we'll find this:
struct Array<T> : MutableCollectionType, Sliceable {
// other stuff...
subscript (index: Int) -> T
// more stuff
}
So, calling the subscript method on our array (which is what we do when we say scoreValueArray) returns a non-optional. And non-optionals cannot be used in the conditional binding if let/if var statements.
We can duplicate this error message in a more simple example:
let foo: Int = 3
if let bar = foo {
// same error
}
This produces the same error. If we instead do something more like the following, we can avoid the error:
let foo: Int? = 3
if let bar = foo {
// perfectly valid
}
This is different from a dictionary, whose subscript method does return an optional (T?). A dictionary will return a value if the key passed in the subscript is found or nil if there is no value for the passed key.
We must avoid array-index-out-of-bounds exceptions in the same way we always do... by checking the array's length:
if element < scoreValueArray.count {
scoreValue = scoreValueArray[element]
}