Get a Swift Variable's Actual Name as String
As per the updated from this answer, it is supported in Swift 3 via #keyPath
NSPredicate(format: "%K == %@", #keyPath(Person.firstName), "Andrew")
Convert Swift variable name to string, to use as string?
You can make this a function instead of a variable, and do the same thing. I'll explain it in steps.
So first, how do we make a function the same thing as a variable?
Basically, we are "wrapping" the variable inside of the function. This is a similar to approach to the first block I suggested in your latest RefBool thread, where we grabbed or set a value via function.
Here, we are using methods/functions to alter a variable directly without parameters--so, we will be using the function by name instead of using the variable by name to access it throughout our code:
enum Static {
private static var _button1 = false
static func button1() { toggle(&_button1) }
}
Note, the Enum is just a namespace for us to call the method to emphasize that the variable's name is not needed for our code to work--Only the function needs to know its name.This code would work inside of a struct or class as well, or even in a global namespace.
So here, if I want to change the value of _button1
, I must call button1()
. _button1
is essentially invisible to everyone and everything--and that's ok, because we only need button1()
to do our work.
So now, every time we call Static.button1()
, Static._button1
will toggle true
to false
, false
to true
, and so on (using the function I gave you earlier)
Why are we doing this?
Well, because there is no way to get the name of a variable without Mirror
and reflection, and there is already a built-in command called #function
. We are jumping through the added hoop of using the function, so we don't have to set-up a mirror and other OBJc hoops.
Let's alter our enum
to demonstrate how we can get the name of the func
:
enum Static {
private static var _button1 = false
static func button1() -> String {
toggle ( &_button1 )
return ( #function )
}
}
Now, when we call button1()
, we do two things: 1, we toggle the state of the button (_button1), and we return a string with the name "button1()"
let buttonName = Static.button1() // buttonName = "button1()"
We can make this more usable by calling a member of String
to mutate itself:
let trimmedButtonName = Static.button1().replacingOccurrences(of: "()", with: "")
// trimmedButtonName = "button1"
Let's update our enum
now with this handy replacement:
enum Static {
private static var _button1 = false
static func button1() -> String {
toggle ( &_button1 )
return ( #function.replacingOccurrences(of: "()", with: "") )
}
}
And our final use case is as follows:
let buttonName = Static.button1()
Note:
You don't have to have _button1
as private, that was just to demonstrate that you don't need it to be public. If you need to grab the state of it, you can make another function, or return a tuple type from button1()
:
static func button1() -> (name: String, state: Bool) {
// ...
return ( #function.etc, _button1)
}
let results = Static.button1()
print( results.name ) // "button1"
print( results.state ) // true / false
You can also set something in the parameter if you need more explicit control over setting the variable, just as you would any normal function.
UPDATE:
For example, if you wanted to use explicit control, you could do:
func button1(equals: Bool? = nil) -> (name: String, state: Bool) {
if equals != nil {
// do the code mentioned above
} else {
_button1 = equals!
}
return (#function.replacingOccurrences(of: "()", with: ""), _button1)
}
let buttonState.state = Static.button1(equals: false) // false
let buttonStateAgain.state = Static.button1(equals: true) // true
print(Static.button1().name) // "button1"
How to print variable name in swift?
You could change your struct to an enum
enum API: String {
case apiEndPoint = "example.com/profile?"
case apiEndPoint1 = "example.com/user?"
}
func doSomething(endPoint: API) {
print("\(endPoint): \(endPoint.rawValue)")
}
Example
doSomething(endPoint: .apiEndPoint)
apiEndPoint: example.com/profile?
Get a Swift class's property name as a String
If you are ok with making your properties @objc you can get the property name like so:
class Person {
@objc var firstName: String
var lastName: String
var downloading: Bool
func excludePropertiesFromCloud() -> [String] {
return [#keyPath(firstName)]
}
}
Get variable name from value in Swift
Yes it is possible to do this.
All you'll need is a Dictionary.
var titles: [String: String] = ["Welcome": "This is the actual string to display"]
let title = dict["start"]["title"]
let Welcome = titles[title]
If you don't want to use a Dictionary you can use the KVC
to get the iVar property.
Just make sure your class is inheriting from NSObject
.
let Welcome = "This is the actual string to display"
let title = dict["start"]["title"]
let welcomeString = self.value(forKey: title)
print("Welcome:\(welcomeString)")
Alternatively you can move all your Strings in a separate class (not Struct, as KVC is not available for structs):
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let title = dict["start"]["title"]
let welcome = Strings.value(forKey: title)
print("Welcome:\(welcome)")
}
//MARK: - Strings for our ViewController
fileprivate class Strings:NSObject {
static let Welcome = "This is the actual string to display"
}
}
Related Topics
Difference Between a Weak Reference and an Unowned Reference
Swift 2 - Unsafemutablepointer≪Void≫ to Object
Swift 5.0: 'Withunsafebytes' Is Deprecated: Use 'Withunsafebytes≪R≫(...)
Swift: Print() VS Println() VS Nslog()
Sort Dictionary by Values in Swift
The Use of Swift 3 @Objc Inference in Swift 4 Mode Is Deprecated
How to Test Equality of Swift Enums With Associated Values
Examples of Delegates in Swift
How to Use String.Substringwithrange? (Or, How Do Ranges Work in Swift)
Error-Handling in Swift-Language
How to Create Instances of Managed Object Subclasses in a Nsmanagedobject Swift Extension
How Does String.Index Work in Swift
What's the Cleanest Way of Applying Map() to a Dictionary in Swift
Attach Parameter to Button.Addtarget Action in Swift
Uncaught Error/Exception Handling in Swift
How to Determine If a String Contains a Character from a Set in Swift