Understanding to visualize swift initializers - ios

I am unable to visualise the working of initializers in Swift. For this example from the official documentation.
struct Fahrenheit{
var temp : Double
init(){
temp = 32.0
}
}
var f = Fahrenheit()
print(" \(f.temp)")
//Prints 32.0
Here's what I understood till now, PLEASE correct me when i am wrong:
struct is a value type.
variable temp is a stored property that stores values inside the memory space where the struct is defined (in memory).
when we create a variable 'f' is an instance(object) copy of the Structure Fahrenheit is made in another memory space having the same properties.
What i am unable to understand is that what is
init(){
temp = 32.0
}
doing to the instance f.
When do we use intializers in general. (Main purpose : using an example).
Also the difference between functions, closures and initializers, how they are stored in memory?

It is definitely important to deeply understand the process of creation of object (as an instance of a class or an instance of the struct). Objects are created based on a template defined in class or struct, in a "space" I like to name as "Object space". So, the object is the instance of struct Fahrenheit in an "Object space" and you could try to see it (visualize) as a balloon. The variable f is a reference to this object and it is been used as a tool to access this balloon (object, instance).
I suggest you to take a look to Apple's documentation:
https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html
Here you can see this:
And - In my opinion, it is a good way how to visualize objects and references to objects.
So, when the system executes: var f = Fahrenheit(), first - it makes an balloon in Object space, it invokes initialiser (implicit or explicit) to set initial values, than it makes an reference (f) - that points to the just-borned-object.
So:
init(){
temp = 32.0
}
does not make an effect to - f, it makes an effect inside of object (balloon), and f is been uses to access to the balloon. (If there is no reference, the ARC will kill the object)

Related

Why weakifying a strong reference by using a local variable doesn't work?

(I understand how ARC works and the difference between weak and unowned. The question is about a specific use of them and why it doesn't work. I'll use unowned in the example below just for simplicity.)
See example below. Note line 10, which is intended to change the passed strong reference to an unowned reference. I thought this would work, but when I used it in my code recently I found I was wrong.
1 import Foundation
2 class MyClass {
3 var uuid: UUID = UUID()
4
5 deinit {
6 print("Deinited!")
7 }
8 }
9 func test(_ d: inout [UUID:MyClass], _ o: MyClass) {
10 unowned let u = o // <- !
11 d[u.uuid] = u
12 }
13 var d = [UUID: MyClass]()
14 test(&d, MyClass())
Run the above code in Playground. The result shows that deinit isn't called, indicating a strong reference to the object is saved in the dictionary.
I wonder why? Does weak and unowned keyword only applies to property? But the above code doesn't generate compiler error and the Swift book does mention it can be used in variable declaration:
You indicate an unowned reference by placing the unowned keyword
before a property or variable declaration.
Can anyone share how you understand it? Thanks!
BTW, I know how to solve the issue (for example, using a wrapper like this). What I try to understand is why the above code doesn't work.
When assigning (a = b), you can't control what kind of reference a is. You can only control what object a refer to.
Here:
d[u.uuid] = u
The code is not saying:
The value associated with the key u.uuid in d is set to the (unowned) reference u.
It's saying:
The value associated with the key u.uuid in d is a reference (not saying what kind) that refers to what u is referring to.
The fact that u is unowned is rather irrelevant. Dictionaries will always store strong references to objects. That's just how they are designed.
In the case of the Weak<T> wrapper, the dictionary will still store a strong reference to Weak<T>, but Weak<T> will store a weak reference to the wrapped T object. That's how it achieves not having a strong reference to the T object.
#Sweeper's answer contains all the points I'll give below. But I'll try to make them a bit more explicit:
Variable represents a storage (a location in memory)
Assignment operation changes the value in the storage
weak and unowned are attributes of the storage. They are not affected by assignment operation.
So the unowned keyword in line 10 in my example code affects only that specific storage location (it's in stack) and have no influence on the attribute of storage used by the global dictionary (as #Sweeper pointed out).

What must be accomplished in the implementation of an initializer?

What must be accomplished in the implementation of an initializer?
a. All properties need to be initialized with a value
b. All properties need to be explicitly assigned a value.
c. All non-Optional properties need to be initialized.
Sorry, that's incorrect.
Optionals need to be initialized as well.
d. The object needs to be created
What answer is correct and why?
In my opinion that is very confusingly stated question. Because what you as the developer have to do is Option c.
Take a look at this simple code example and the minimum init to be compilable
class SomeClass {
var a : AnyObject
var b : AnyObject?
var c : AnyObject!
var d = ":)"
init() {
a = ""
print("initialized")
}
}
The swift docu states
Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.
You can set an initial value for a stored property within an initializer, or by assigning a default property value as part of the property’s definition. These actions are described in the following sections.
Option d. is imho non-sense since the object creation is handled by the underlying runtime environment and not via the initializer.
Now b. and a. remain with the tiny difference in wording explicitly assigned vs. initialized. I would therefore discard Option b because the b and c variable do not need any explicit value, the implicit nil is perfectly fine for the time (reading c will not work yet though)
Therefore my answer choice would be Option a. After the init method all properties need to have some specific value. Some of them explicitly in the init function, some of them implicitly.
tl;dr:
my final answer is Option a.

Swift Array Pass by Value...same memory address?

Can someone please clear this up for me.
I understand (thought) Swift passes arrays by value as it is a struct.
But when I pass an array via segue to the next view controller it appears to me that it is passing by reference, as when I check the memory address of the array they are the same.
This is how I'm checking
println("\(unsafeAddressOf(runs))") // 0x0000000174240c00
I would have thought these memory addresses would be different ? Or am I just confusing myself.
The Run / StaffTask classes both inherit from NSObject for saving purposes.
class Run: NSObject, NSCoding { }
Furthermore, if I access a item in the array
var service = self.staffTasks[indexPath.row]
and edit the value, both the service variable value and the element in the array are updated. They have the same memory address, as shown by
println("\(unsafeAddressOf(service)) \(unsafeAddressOf(self.staffTasks[indexPath.row]))")
Also...
staffTasks are a subset of a larger array called runs
When I search for the service object, with the larger set, I find that they are also the same memory address
if let index = find(self.runs, self.staff) {
println("local \(unsafeAddressOf(self.staff)) main list \(unsafeAddressOf(self.runs[index]))")
}
I am using NSCoding to save my objects, so I thought I would need to find the service object within the larger set, replace that object and then save them out.
But turns out I don't need to, I am able to edit the service variable and array of runs is updated on automatically ... like a reference type would be.
Passing? .. setting the Var
in prepare for segue, just setting the local var in the second VC using the standard way, var runsInSecondVC = runs. Not using, &pointers or any other weirdness.
Thanks for any help.
Class Basic Details
class Run: NSObject, NSCoding {
var runDate:NSDate!
var staffName:String!
var staffEmail:String!
var runTasks:[StaffTask]!
}
class StaffTask: NSObject, NSCoding {
var taskName:String
var taskTime:NSInteger
var clientName:String!
}
These are the basic of these classes. Nothing complicated.
passes arrays by value
No. Swift arrays are a value type. This does not mean they are pass-by-value. It means that you can mutate an array by way of a reference without affecting other references to which that array has been assigned.
You are getting a little confused about the array itself and the contents of the array.
..if I access a item in the array
var service = self.staffTasks[indexPath.row]
and edit the value, both the service variable value and the element in the array are updated. They have the same memory address...
Putting an object into an array doesn't copy the object. In fact, the array holds a reference to the object, so the line above actually says "make a variable, service that holds a reference to the object that self.staffTasks[indexPath.row] holds a reference to". There is only one object, now referred to in two places, so when you update the object that change is visible through both variables.

Simple Clarification Objects Swift Language

I have a very simple question on something that I may have misunderstood.
I have two UIViews "A" and "B". If I write :
let A = UIView() // Or something else
let B = A
and then I change properties of B (for exemple the frame), will the properties of A change too ?
I though not, but I was animating a view, so I had the initial view and the final view. I created a transition view like this :
let transitionView = finalView
and then I changed the properties of transitionView, the position of a label for exemple.
When I added the final view at the end of the animation, the label was at the new position.
Why ? Thanks
In swift types are split in 2 main categories:
reference types
value types
Classes are reference types; structs (which include arrays and dictionaries), basic data types (int, float, string, etc.), and enums are all value types.
A value type is always passed by value, which means when assigning to a variable or passing to a function/method, a copy of the original data is created. There's an exception to this rule: a function/method can use the inout modifier on a value type parameter to have it passed by reference.
Note that the compiler and the runtime usually do optimizations, so a copy is not always created unless strictly needed - what's important is that we, as developer, know that we are working on a copy and not on the original data.
A reference type is always passed by reference, which means when assigning it to a variable or passing it to a function/method, a reference to the data and not the data itself is assigned/passed.
UIView is a class, so when you create an instance, assign it to a variable, then assign that variable to another variable, the reference to the instance and not the instance itself is assigned. Both variables point to the same UIView instance. Any change made to the instance is visible to all variables referencing that instance.
Suggested reading: Classes and Structures
Because B and A are not two views. They are references to the same UIView object.
That, in turn, is because class instances are passed as reference types in Swift.
See now my little essay on this topic here: https://stackoverflow.com/a/27366050/341994

#property/#synthesize equivalent in swift

We used to declare property to pass data between classes as following:
.h file (interface file)
#property (nonatomic) double topSpeed;
.m file (implementation file)
#synthesize topSpeed;
Now there is no interface class, how to pass data between .swift classes ?
Swift provides no differentiation between properties and instance variables (i.e, the underlying store for a property). To define a property, you simply declare a variable in the context of a class.
A swift class is simply a ClassName.swift file.
You declare a class and properties as
class SomeClass {
var topSpeed: Double
var aStrProperty: String
var anIntProperty: Int
//Initializers and other functions
}
You access property values via dot notation. As of Xcode6 beta 4, there also are access modifiers (public, internal and private) in Swift. By default every property is internal. See here for more information.
For more information, refer to the Swift Programming Guide:
Stored Properties and Instance Variables
If you have experience with Objective-C, you may know that it provides
two ways to store values and references as part of a class instance.
In addition to properties, you can use instance variables as a backing
store for the values stored in a property.
Swift unifies these concepts into a single property declaration. A
Swift property does not have a corresponding instance variable, and
the backing store for a property is not accessed directly. This
approach avoids confusion about how the value is accessed in different
contexts and simplifies the property’s declaration into a single,
definitive statement. All information about the property—including its
name, type, and memory management characteristics—is defined in a
single location as part of the type’s definition.
Using Properties.
From the Swift Programming Guide:
Stored Properties and Instance Variables
If you have experience with Objective-C, you may know that it provides
two ways to store values and references as part of a class instance.
In addition to properties, you can use instance variables as a backing
store for the values stored in a property.
Swift unifies these concepts into a single property declaration. A
Swift property does not have a corresponding instance variable, and
the backing store for a property is not accessed directly. This
approach avoids confusion about how the value is accessed in different
contexts and simplifies the property’s declaration into a single,
definitive statement. All information about the property—including its
name, type, and memory management characteristics—is defined in a
single location as part of the type’s definition.
Properties in Objective-C correspond to properties in Swift. There are two ways to implement properties in Objective-C and Swift:
Synthesized/auto-synthesized properties in Objective C -- these are called "stored properties" in Swift. You simply declare it with var topSpeed : Double or let topSpeed : Double = 4.2 in a class declaration, exactly as you would declare a local variable in a function body. You don't get to specify the name of the backing instance variable because, well, there are currently no instance variables in Swift. You must always use the property instead of its backing instance variable.
Manually implemented properties in Objective-C -- these are called "computed properties" in Swift. You declare them in the class declaration like var topSpeed : Double { get { getter code here } set { setter code here } } (for readwrite properties), or var topSpeed : Double { getter code here } (for readonly properties).
It sounds like at least part of your question relates to communicating a given class's interface to other classes. Like Java (and unlike C, C++, and Objective-C), Swift doesn't separate the interface from the implementation. You don't import a header file if you want to use symbols defined somewhere else. Instead, you import a module, like:
import Foundation
import MyClass
To access properties in another class, import that class.
Stored Properties and Instance Variables
If you have experience with Objective-C, you may know that it provides two ways to store values and references as part of a class instance. In addition to properties, you can use instance variables as a backing store for the values stored in a property.
Swift unifies these concepts into a single property declaration. A Swift property does not have a corresponding instance variable, and the backing store for a property is not accessed directly. This approach avoids confusion about how the value is accessed in different contexts and simplifies the property’s declaration into a single, definitive statement. All information about the property—including its name, type, and memory management characteristics—is defined in a single location as part of the type’s definition.
From the Swift Programming Book:
struct FixedLengthRange {
var firstValue: Int
let length: Int
}
var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length: 3)
I say : typealias is equivalent even more in swift for #synthesize
just look at this link : https://docs.swift.org/swift-book/ReferenceManual/Declarations.html

Resources