Passing Data between Interface Controllers on Xcode 8.0 Swift 3.0 - swift

I want to pass a string between two Interface Controllers. On InterfaceController1 I want to create a variable like:
var level:String = ("easy")
and then be able to access that variable on InterfaceController2.
I would prefer not to use global variables as I am aware that they are not recommended.
There is one answer on StackOverFlow but it was created when Swift 1 was out and I can't find any up to date anwser.
Thanks

You have several options to achieve this.
Use pushController(withName:,context:) and set level as the context input argument, then in InterfaceController2's awake(withContext:) use that variable.
Use singletons (declare the variable as static) if you need to access the variable of InterfaceController2 from several classes or if you don't directly navigate to InterfaceController2.
If you only need to set up that variable from InterfaceController1 and you directly present InterfaceController2 from InterfaceController1, option 1 is the preferred method.

Related

Should I use Class, Struct or Dictionary for an object containing some simple config parameters?

In an IOS project, I want the user to be able to config their UI.
The user will pick:
"backgroundColor", "fontColor", "fontSize", "font", "lineSpace"
in UIConfigViewController and when user hit done, the result will be stored in an object and passed back into ReadingViewController.
Should I use Struct, Class or Dictionary for this result object?
Can I use the Struct like this?
Ended up using NSManagedObject
You could simply implement NSUserDefaults.standardUserDefaults, which is basically a dictionary for user settings. The main advantage for that is it's designed to be accessible at the global scope so no need to pass the object around.

dynamic setting and getting values from Swift class

I'd like to copy all properties from a NSManagedObject over to a "regular" Swift class. I don't want to do this manually, i.e. make a regular class with all the properties for every NSManagedObject and then manually copy all those values.
I do know how to read property names and values dynamically from my managed object, but how to set them on a Swift class in a way that I can then use those values like
mySwiftObject.name
which returns a String or
mySwiftObject.age
which returns a Number (as those are the types on the Managed Object). Custom subscripting and stuff like that came to my mind, but I didn't manage to achieve this... Is there a nice way to do exactly that?

Why does Swift not allow stored properties in extensions?

I've been trying to find the best way to implement a stored property in an extension, and came across this question: Swift extension stored properties alternative. However, I have not found a reason why in the discussion or anywhere else. Is there a reason why stored properties are not allowed in Swift? And if so, what is the reason?
Extensions are for extending the functionality of an existing class without changing the memory structure. It's more or less syntactic sugar. Imagine you could add stored properties and methods, what would it be? Nothing else but inheritance. So if you like to add new properties and methods just inherit from the class.

Eclipse Core Expressions object properties

So, I have a core expression. It has a with element that retrieves my variable (successfully). Say my variable is of type java.awt.Point; thus it has two public properties, x and y. Is there any way to test against these properties in a core expression?
If there was some way I could provide my own implementation of org.eclipse.core.expressions.Expression, that would work. Is that possible?
I think you can do this by creating a propertyTester that would react properly to your variable using your custom code.

Classes: Public vars or public functions to change local vars?

Exactly what the topic title says,
In which cases would you prefer using public functions to change local variables over just defining that variable as public and modifying it directly?
Don't expose the data members directly: using opaque accessors means you can change the implementation at a later date without changing the interface.
I should know. I take the short cut from time-to-time, and have had occasion to regret it.
Obviously if you want changing the variable to have some other effect on the object's state (like recalculating some other property of the object) you must use a mutator function.
If it's possible to set the variable to something that places the object in an invalid state, you should probably also use a mutator function. This way you can throw an exception (or return an error, or just ignore) if something illegal is about to happen. This does wonders for debugging.
But if some variables can be modified with mutator functions, and others are public, the programmer needs to keep track of which is which. This is a waste of time and effort so in some cases it's easiest to just use mutator functions for everything.
If you look at an object purely in term of service, you realize that exposing a variable is not a good way to expose those services.
The API must reflect what the object is all about (for it to achieve a high cohesiveness), and if you define a setValue(...), it is not so much because you need a way to -- today -- changes a variable, but because it makes sense for the object to expose this service.
So:
Don't provide accessors or mutator function to every single member of every single class you write. Only provide accessors/mutator functions if the accessors/mutator methods are a sensible and useful part of the class's interface (API).
Don't think of these methods as accessors or mutators. Instead, think of them as methods that access or mutate a certain abstract property of the object that happens to be represented by a single member today, but may be computed in a more complex manner tomorrow.
You should mention what language you are dealing with, since that will affect the answer.
Your first thought should be about the API to your class. If you want to keep that API stable (and you should!), then consider how you might change today's simple variable into a full-blown method later.
In many languages, you can't change a variable to a method without changing the calling code. C, C++, and Java fall into this category. Never use public variables in these languages, because you won't have any wiggle room later.
In Python, you can change a variable to a property without changing the callers, so you don't have to worry up front: use public variables.
C# I believe has properties that can let you change variables to methods transparently, but I am not sure.
If you want to change a variable inside a class, your best doing it through Properties.
Its not good practice to have variable's modified on the outside.
Think of future development too. You could put some logic behind a Property without changing the whole program.