I want to return the class and function for debugging purposes. In Objective-C I would use the following, how would I do the same in Swift?
Obective-C
if (debug==1) {
NSLog(#"Running %# '%#'", self.class,NSStringFromSelector(_cmd));
}
Swift
if debug==1 {
println("Running: ", self ,__FUNCTION__)
}
In swift, this would return something like this:
(Running: , <myTeamManager.PlayerInformationTableViewController: 0x7fa18bd31680>, viewDidLoad())
but I only want PlayerInformationTableViewController
reflect(self).summary.pathExtension
Related
I have created my app using swiftui. Then I found that sometimes I may need to do some tasks quite frequently. Instead of placing it in the same swiftui files, I think that it should be placed inside another class. However, I wonder how can we call the function of another class inside swiftui? is that I must have new an object to call it?
As I understand your question:
class MyHelper {
static func helpMe(needHelp: Bool) -> String {
if(needHelp) {
return "Help in on the way!"
} else {
return "You are ok!"
}
}
}
Usage:
let result = MyHelper.helpMe(needHelp: true)
You can use static or class functions inside the helper class, and access to them by '.' syntax. Difference between static and class functions, that you can override class function in the subclass (which is not really needed with helper classes), static not.
I'm relatively new to Swift and was wondering how I could achieve this exact thing.
I have a Swift process, and I want to toggle a flag on completion, and I have the following code:
task.terminationHandler = { process in
isTaskComplete.toggle()
}
I have this exact code duplicated in various views and was trying to convert it to a function, with my first attempt of referencing the isTaskComplete flag by using inout.
However, because terminationHandler itself defines the parameters of the closure it takes, there is no way for me to attach a secondary inout parameter. I was wondering if there was any way for me to reference the flag inside the closure and have the value be referenced?
For what it's worth in using Swift 5 and Xcode 11.5.
Any help will be greatly appreciated! Thanks :)
You can use protocols and protocol extensions for that.
protocol NameYourProtocol {
var task: TypeOfTask { get } // { get set } If you need
var isTaskComplete: Bool { get }
}
extension NameYourProtocol {
func handleTermination() {
task.terminationHandler = { process in
isTaskComplete.toggle()
}
}
}
And this is what you will do in every class you need to call it:
class Worker: NameYourProtocol {
// This will be required by protocol.
var task = TypeOfTask()
var isTaskComplete = false
init() {
// Call `handleTermination` where you need to.
handleTermination()
}
}
In Xcode 6 Beta 6,
I am unable to use any NSFontManager methods in Swift.
For example:
var fontManager = NSFontManager.sharedFontManager()
fontManager.setAction("changeFont:")
I always get the error: NSFontManager does not have a member named setAction
This occurs for any method (e.g. setDelegate, setTarget)
Thanks for any help.
Try this:
fontManager.action = Selector("changeFont:")
Properties are set using the property syntax in Swift.
In ObjC, properties were declared with (if not using #property):
- (X) myProp { return ... }
- (void) setMyProp(X value) { ... }
In Swift, the property declarations look more like C#:
var myProp : X { get { return ... } set { ... } }
So you can't call the setter explicitly using the same method name as in ObjC (though I have actually no idea what it will be compiled into...).
Calling a method from its name (in a String format) can be sometimes useful.
In Swift it is recomended to change behavior and to use closures to do something "dynamically", so for example you can have a dictionary of functions, with the name as the key, and the implementation as the value.
However, sometimes you want to simply know "how to do it", and this is the reason of this question.
So, how to call dynamically a Swift method starting from it's name as string?
In Objective C it was simple:
[self performSelector:NSSelectorFromString(#"aSelector")];
But performSelector is banned in Swift
Is there any alternative?
In Swift, you should use closures and change your approach.
However, if you want to use performSelector to dynamically call a method given only it's String signature, altough it's not supported natively, I've found how to do it.
It is possible to create a C alternative to performSelector that:
works even on native swift classes (non objective-c)
takes a selector from string
However it's not so straightforward to implement a complete version of it, and it's necessary to create the method in C.
in C we have dlsym(), a function that returns a pointer to a function given the char symbol.
Well, reading this interesting post:
http://www.eswick.com/2014/06/inside-swift/
I've learned a lot of interesting things about swift.
Swift instance methods are plain functions with a specific signature, like this
_TFC14FirstSwiftTest12ASampleClass13aTestFunctionfS0_FT_CSo8NSString
where the "self" value is passed as the last parameter
in short you can call it directly from the c side without any kind of bridging, it is sufficient to rebuild the correct function signature.
In the signature above, there is the name of the project (FirstSwiftTest) and the lenght (14), the name of the class (ASampleClass) and the lenght (12), the name of the function (aTestFunction) and the lenght (13), then other values as the return type ecc ecc. For other details look at the previous link
The function above, is the representation of this:
class ASampleClass
{
func aTestFunction() -> NSString
{
println("called correctly")
return NSString(string: "test")
}
}
Well, on the c side, I was able to create this function
#include <stdio.h>
#include <dlfcn.h>
typedef struct objc_object *id;
id _performMethod(id stringMethod, id onObject)
{
// ...
// here the code (to be created) to translate stringMethod in _TFC14FirstSwiftTest12ASampleClass13aTestFunctionfS0_FT_CSo8NSString
// ...
id (*functionImplementation)(id);
*(void **) (&functionImplementation) = dlsym(RTLD_DEFAULT, "_TFC14FirstSwiftTest12ASampleClass13aTestFunctionfS0_FT_CSo8NSString");
char *error;
if ((error = dlerror()) != NULL) {
printf("Method not found \n");
} else {
return functionImplementation(onObject); // <--- call the function
}
return NULL
}
And then called it on the swift side
let sampleClassInstance = ASampleClass()
println(_performMethod("aTestFunction", sampleClassInstance))
The function resulted in these statement printed on the log:
called correctly
test
So it should be not so difficult to create a _performMethod() alternative in C that:
creates automatically the function signature (since it seems to have a logic :-)
manages different return value types and parameters
EDIT
In Swift 2 (and maybe in Beta3, I didn't try) It seems that performSelector() is permitted (and you can call it only on NSObject subclasses). Examining the binary, It seems that now Swift creates static functions that can be specifically called by performSelector.
I created this class
class TestClass: NSObject {
func test() -> Void {
print("Hello");
}
}
let test = TestClass()
let aSel : Selector = NSSelectorFromString("test")
test.performSelector(aSel)
and now in the binary I find
000000010026d830 t __TToFC7Perform9TestClass4testfT_T_
At this time, I don't understand well the reasons behind this, but I'll investigate further
You could call a method from a String this way:
let foo = <some NSObject subclass instance>
let selectorName = "name"
foo.perform(Selector(selectorName))
It is available only when your foo class is subclass of NSObject
swift3 version
class MyClass:NSObject
{
required public override init() { print("Hi!") }
public func test(){
print("This is Test")
}
public class func static_test(){
print("This is Static Test")
}
}
if let c: NSObject.Type = NSClassFromString("TestPerformSelector.MyClass") as? NSObject.Type{
let c_tmp = c.init()
c_tmp.perform(Selector("test"))
c.perform(Selector("static_test"))
}
I have an enum defined as follows:
typedef enum modifiers {
modifierNone=-1,
modifierCmd,
modifierShift,
modifierOption
} Modifier;
What i would like to do is pass a string value from one method to another for example (modifierCmd) and create the relevant Modifier to pass to a separate method.
- (void)methodOne:(NSString *)stringValue {
Modifier mod = (Modifier)stringValue;
[self methodTwo:mod];
}
Should this work?
Thanks
Nope. You can use a function, though:
Modifier makeModifier(NSString *s)
{
if ([s isEqualToString:#"modifierNone"]) {
return modifierNone;
} else if ([s isEqualToString:#"modifierCmd"]) {
return modifierCmd;
} /* etc... */
}
- (void)methodOne:(NSString *)stringValue
{
[self methodTwo:makeModifier(stringValue)];
}
I don't think it can work because the data type is really different. Enum is in fact, integer, when NSString is an object. You can use if else to check for modifier. But I recommend to pass the modifier directly.