'initializeMemory(as:at:count:to:)' is deprecated - swift

As is, I'm using the following code to set a value at an index of a UnsafeMutableRawPointer (the pointer points to an array):
ptr.initializeMemory(as: Float.self, at: idx, count: 1, to: someValue)
This works and sets ptr at idx to someValue. But Xcode is giving me a warning saying that said method is deprecated, and suggesting me to use initializeMemory(as:repeating:count:) instead. However, such method doesn't have an index parameter.
So, my question is: which function should I use in order to appropriately set the value of an UnstableMutableRawPointer at some index?

Related

Get position as int from Set<>.index in Swift

I have a set collection and I want to get index of an element. I'm using firstIndex function of set but It returns Set.index. How can I convert it to int?
let index = mySet.firstIndex(of: dataSource) // This returns Set<>.index
Set.index Documentation
Set is unordered, try using an Array for example
It is very easy to convert a set index to an int
index.hashValue
however you will find that that index isn't very useful
Set(["a", "b", "c"]).firstIndex(of: "a")?.hashValue
// 5346078814578125500
this is because a set uses a hash table as its underlying storage mechanism. You'll see this if you try to print the index itself
Index(_variant: Swift.Set<Swift.String>.Index._Variant.native(Swift._HashTable.Index(bucket: Swift._HashTable.Bucket(offset: 3), age: -233657665)))
What firstIndex returns to you is simply the hashkey at which that element is stored. It does not relate to any kind of "position" because hash tables don't have a meaningful sense of position.

Why can a Swift constant be used as a loop index?

I know this Swift code is valid:
let index = 0
for index in (1...9) {
print(index) // index changes value from 1 to 9
}
But then if you say
index += index
you get the error
"Cannot assign to value: 'index' is a 'let' constant"
So, if index is a constant why is it ok to use it in the loop where its value will change?
So, if index is a constant why is it ok to use it in the loop where its value will change?
You could think of index as being newly created and initialized each time through the loop, just as a variable declared inside the loop's body would be. It makes sense to use let here because normally you don't change the loop counter inside the loop.
Update: After the edit, the code makes more sense, and pkamb's comment really is more clear: the index that you declare with let index = 0 is a different variable from the index used in the for loop.

memset with Swift 5.2

Sometimes I need to clear a part of a byte array to zero. With a for loop this is slow. Much faster is using the memset() function. I have the following code which will clear elements 200 to 299 of the array "a".
var a: [UInt8] = [UInt8](repeating: 1, count: 1000) // a[0..999] set to 1
start = 200
length = 100
memset(&a + start, 0, length) // a[100..199] set to 0
This worked well until Swift 5.2 with Xcode 11.4 came out. Now it works, too, but a warning appears:
Inout expression creates a temporary pointer, but argument #1 should be a pointer that outlives the call to '+'
How can this made, to have no warning.? Andybody has an idea?
Some more explanations is whows in Xcode:
Implicit argument conversion from '[UInt8]' to 'UnsafeMutableRawPointer' produces a pointer valid only for the duration of the call to '+'
Use the 'withUnsafeMutableBytes' method on Array in order to explicitly convert argument to buffer pointer valid for a defined scope
I do not understand, what this means.
The hint to use withUnsafeMutableBytes is exactly what you should follow:
a.withUnsafeMutableBytes { ptr in
_ = memset(ptr.baseAddress! + start, 0, length)
}

Fatal Error: Array Index out of range in Swift Xcode6

I keep getting this error in my func
I'm trying to read the value in array answerRecord. I uses a global var arrayCount, which keep track which index im currently pointing to.
func buttonColourControl(){
switch answerRecord[arrayCount]{
case1: xxxxxxx
I did a println in my earlier func and it return a value of int 1 for the var arrayCount
Therefore arrayCount is not empty. So it should be able to interpret the array as:
*assuming arrayCount is now 1
answerRecord[arrayCount] should be interpreted as answerRecord[1]
Please correct me if im wrong
#IBAction func nextButtonClicked(sender: UIButton) {
arrayCount = ++arrayCount
question.text = spouseQuesion[arrayCount]
controlBackNextButton()
answer1Label.text = spouseAnswer1[arrayCount]
answer2Label.text = spouseAnswer2[arrayCount]
answer3Label.text = spouseAnswer3[arrayCount]
println(arrayCount)
buttonColourControl()
}
Let's say you have an array with one object in it:
let arr = ["hello"]
The only valid index into that array is 0. arr[0] is legal. arr[1] is not. The array has 1 element but its index number is 0.
This is true for any array. Every array holds some number of elements. It might be 0 elements, in which case no index is legal. It might be 3 elements, in which case you can refer to the array's elements by index numbers 0, 1, and 2. And so on. That's all. Those are the rules. You cannot use any other index number or you will crash.
So the error message is simply telling you that you are making that mistake. You have an array answerRecord and it has some number of elements - I have no idea how many, and it doesn't matter. Then you are using the expression answerRecord[arrayCount] and the value of arrayCount is outside the bounds I have just explained. That's all you need to know. The error message tells you the bug in your program. Now you can fix it.

How exactly does the "let" keyword work in Swift?

I've read this simple explanation in the guide:
The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once.
But I want a little more detail than this. If the constant references an object, can I still modify its properties? If it references a collection, can I add or remove elements from it? I come from a C# background; is it similar to how readonly works (apart from being able to use it in method bodies), and if it's not, how is it different?
let is a little bit like a const pointer in C. If you reference an object with a let, you can change the object's properties or call methods on it, but you cannot assign a different object to that identifier.
let also has implications for collections and non-object types. If you reference a struct with a let, you cannot change its properties or call any of its mutating func methods.
Using let/var with collections works much like mutable/immutable Foundation collections: If you assign an array to a let, you can't change its contents. If you reference a dictionary with let, you can't add/remove key/value pairs or assign a new value for a key — it's truly immutable. If you want to assign to subscripts in, append to, or otherwise mutate an array or dictionary, you must declare it with var.
(Prior to Xcode 6 beta 3, Swift arrays had a weird mix of value and reference semantics, and were partially mutable when assigned to a let -- that's gone now.)
It's best to think of let in terms of Static Single Assignment (SSA) -- every SSA variable is assigned to exactly once. In functional languages like lisp you don't (normally) use an assignment operator -- names are bound to a value exactly once. For example, the names y and z below are bound to a value exactly once (per invocation):
func pow(x: Float, n : Int) -> Float {
if n == 0 {return 1}
if n == 1 {return x}
let y = pow(x, n/2)
let z = y*y
if n & 1 == 0 {
return z
}
return z*x
}
This lends itself to more correct code since it enforces invariance and is side-effect free.
Here is how an imperative-style programmer might compute the first 6 powers of 5:
var powersOfFive = Int[]()
for n in [1, 2, 3, 4, 5, 6] {
var n2 = n*n
powersOfFive += n2*n2*n
}
Obviously n2 is is a loop invariant so we could use let instead:
var powersOfFive = Int[]()
for n in [1, 2, 3, 4, 5, 6] {
let n2 = n*n
powersOfFive += n2*n2*n
}
But a truly functional programmer would avoid all the side-effects and mutations:
let powersOfFive = [1, 2, 3, 4, 5, 6].map(
{(num: Int) -> Int in
let num2 = num*num
return num2*num2*num})
Let
Swift uses two basic techniques to store values for a programmer to access by using a name: let and var. Use let if you're never going to change the value associated with that name. Use var if you expect for that name to refer to a changing set of values.
let a = 5 // This is now a constant. "a" can never be changed.
var b = 2 // This is now a variable. Change "b" when you like.
The value that a constant refers to can never be changed, however the thing that a constant refers to can change if it is an instance of a class.
let a = 5
let b = someClass()
a = 6 // Nope.
b = someOtherClass() // Nope.
b.setCookies( newNumberOfCookies: 5 ) // Ok, sure.
Let and Collections
When you assign an array to a constant, elements can no longer be added or removed from that array. However, the value of any of that array's elements may still be changed.
let a = [1, 2, 3]
a.append(4) // This is NOT OK. You may not add a new value.
a[0] = 0 // This is OK. You can change an existing value.
A dictionary assigned to a constant can not be changed in any way.
let a = [1: "Awesome", 2: "Not Awesome"]
a[3] = "Bogus" // This is NOT OK. You may not add new key:value pairs.
a[1] = "Totally Awesome" // This is NOT OK. You may not change a value.
That is my understanding of this topic. Please correct me where needed. Excuse me if the question is already answered, I am doing this in part to help myself learn.
First of all, "The let keyword defines a constant" is confusing for beginners who are coming from C# background (like me). After reading many Stack Overflow answers, I came to the conclusion that
Actually, in swift there is no concept of constant
A constant is an expression that is resolved at compilation time. For both C# and Java, constants must be assigned during declaration:
public const double pi = 3.1416; // C#
public static final double pi = 3.1416 // Java
Apple doc ( defining constant using "let" ):
The value of a constant doesn’t need to be known at compile time, but you must assign the value exactly once.
In C# terms, you can think of "let" as "readonly" variable
Swift "let" == C# "readonly"
F# users will feel right at home with Swift's let keyword. :)
In C# terms, you can think of "let" as "readonly var", if that construct was allowed, i.e.: an identifier that can only be bound at the point of declaration.
Swift properties:
Swift Properties official documentation
In its simplest form, a stored property is a constant or variable that is stored as part of an instance of a particular class or structure. Stored properties can be either variable stored properties (introduced by the varkeyword) or constant stored properties (introduced by the let keyword).
Example:
The example below defines a structure called FixedLengthRange, which describes a range of integers whose range length cannot be changed once it is created:
struct FixedLengthRange {
var firstValue: Int
let length: Int
}
Instances of FixedLengthRange have a variable stored property called firstValue and a constant stored property called length. In the example above, length is initialized when the new range is created and cannot be changed thereafter, because it is a constant property.