How to get defined variables in Swift? - swift

Does swift have a way to access all global and scope variables that have been defined such as the get_defined_variables function in php? I am trying to target a global variable in a function by having its key name match the string sent to the function.

I ended up solving by holding data in a dictionary and matching the method I wanted to run on the value in the dictionary with an enum. Thanks for the replies.

Related

Stopping reference variables changing the value of the original variable

I am assigning the value of a custom class to another variable. Updating the value of the new variable is affecting the value of the original variable. However, I need to stop the reference variable from updating the original variable.
Here's a basic representation of what's happening:
var originalVariable = CustomClass()
originalVariable.myProperty = originalValue
var referenceVariable = originalVariable
referenceVariable.myProperty = updatedValue
print("\(originalVariable.myProperty)") //this prints the ->updatedValue<- and not the ->originalValue<-
I've tried wrapping the referenceVariable in a struct to make it a value type but it hasn't solved the problem.
I've found information regarding value and reference types but I haven't been able to find a solution.
My question in a nutshell: How do I stop an update to a reference variable from updating the original variable that it got its value assigned from?
Thanks in advance.
The whole point of reference semantics (as used by classes) is that all variables point to the same (i.e., they reference the same) object in memory. If you don't want that behaviour, you should use value types (Struct, Enum, Array...) or create copies of your object.
If CustomClass implements the NSCopying protocol you can do:
var referenceVariable = originalVariable.copy()
If it doesn't, you'll have to find some other way to copy it or implement the protocol yourself.
Wrapping the class in a struct will just make two different structs each containing a different reference to the same object.

Why doesn't inout pass by reference?

I'm doing something like this:
someFunction(&myClass)
where someFunction sorts an array on myClass.
someFunction(inout someclass:ClassA) {
someClass.sort({$0.price > $1.price})
}
If I print myClass after the function call, I notice the array is still unsorted. From what I know, Swift passes values by copy. But when I use inout, shouldn't it change to pass by reference?
This is because class instances and functions are reference types. Ints, structs, and everything else are value types. When you pass a reference type into a function as a parameter, you are already going to be referencing that instance. When you pass a value type as a parameter, the function gets a copy of that variable (by default), so inout is usually (see edit) only needed if you want to alter a value type from inside of a function.
Altering a class instance without & or inout:
More details
When you create a reference type var t = myClass(), you're really creating a variable t that is a pointer to a myClass instance in memory. By using an ampersand &t in front of a reference type, you are really saying "give me the pointer to the pointer of a myClass instance"
More info on reference vs value types: https://stackoverflow.com/a/27366050/580487
EDIT
As was pointed out in the comments, you can still use inout with reference types if you want to alter a pointer, etc, but I was trying to shed light on the general use case.
Below is an example of sorting an array inside of a function:
If you post your code here, it would be more meaningful. BTW, look at below links that might helpful for you,
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-ID173
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-ID545

Scala how to get object property value given name of property in string?

I wondering how to get object property value given name of property in string in Scala? I saw examples when you get all fields of object using Reflection and iterate over it. But is it possible to call it without iteration? Or may be there is a way to pass object.field to another function without evaluation and evaluate it there and return result?
Kolmar comment give me right direction to call by name function.

Modifying content of immutable array in Swift does not work

Silly beginner Swift question:
I am expecting the following 3 lines of code to work in the playground:
let items = ["Apple", "Orange", "Pear"]
items[1] = "Banana" // error here
items
Now the error
error: '#lvalue $T5' is not identical to 'String'
items[1] = "Banana"
My understanding that updating content of immutable array is possible in Swift.
I use XCODE 6.1.1
Any idea what is going on here?
Thanks
Based on this thread this was possible in previous releases:
Why in immutable array in swift value can be changed?
When you write let, you define an immutable variable. Use var instead; this lets you define a variable that is mutable.
as stated before me- use a var array if you wish to change your array.
a bit below the question you posted a link and an answer was given inside:
Array in Swift has been completely redesigned to have full value
semantics like Dictionary and String have always had in Swift. This
resolves various mutability problems – now a 'let' array is completely
immutable, and a 'var' array is completely mutable – composes properly
with Dictionary and String, and solves other deeper problems. Value
semantics may be surprising if you are used to NSArray or C arrays: a
copy of the array now produces a full and independent copy of all of
the elements using an efficient lazy copy implementation. This is a
major change for Array, and there are still some performance issues to
be addressed. Please see the Swift Programming Language for more
information. (17192555)
The use of the let keyword declares a Constant.
By definition, a constant cannot be modified.
You want to use the var keyword to declare a Variable,
hence things that will/may vary.
From the apple Swift documentation:
Constants and Variables
Constants and variables associate a name (such as
maximumNumberOfLoginAttempts or welcomeMessage) with a value of a
particular type (such as the number 10 or the string "Hello"). The
value of a constant cannot be changed once it is set, whereas a
variable can be set to a different value in the future.
Declaring Constants and Variables
Constants and variables must be declared before they are used. You
declare constants with the let keyword and variables with the var
keyword. Here’s an example of how constants and variables can be used
to track the number of login attempts a user has made:
let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0

convert instance variable into string?

If I have a uiviewcontroller with a instance variable "direction". I can do
MyViewController.direction
but if I don't want the value but a string with the variable name, so I want #"direction".
Is it possible?
You can retrieve the list of properties using class_copyPropertyList and you can use property_getName to retrieve the name of the property passing the struct returned by the first method.
You can read more on that in Objective-C Runtime programming guide in the Declared Properties section.