-6

I have in in my app an function. Is there a way to transfer it to other Viewcontroller? if I use UserDefaults.standard.set(function(), forKey: "function")

I don't know how to load it, because

let function() = UserDefaults.standard.object(forKey: "function") as? CGFunction

doesn't work.

Thanks for answers!

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • What did you mean? – Mannopson Oct 31 '17 at 12:08
  • This is wrong. Your function is almost never a variable. There is no need to do this. Either write that function in a global space, or exten a protocol which they both adopt or write it in the baseclass of both these classes. Ot worst case scrnario, copy/paste the function. – mfaani Oct 31 '17 at 12:11
  • 2
    Apart from that `CGFunction` is not property list compliant `UserDefaults` is the wrong place for sharing data between view controllers. – vadian Oct 31 '17 at 12:17
  • Hey buddy, I didn't get what are you trying to achieve with that but if you're trying to pass data between viewcontrollers, check this answer: https://stackoverflow.com/a/31934786/6361488 – Yago Zardo Oct 31 '17 at 12:41

4 Answers4

1

Passing and returning functions

The following function is returning another function as its result which can be later assigned to a variable and called.

func jediTrainer () -> ((String, Int) -> String) {
  func train(name: String, times: Int) -> (String) {
    return "\(name) has been trained in the Force \(times) times"
  }
  return train
}

let train = jediTrainer()
train("Obi Wan", 3)
Baig
  • 4,737
  • 1
  • 32
  • 47
0

Yes, Swift allows you to pass functions or closures to other objects, since functions and closures are themselves first class objects.

However, you cannot save a function or closure to UserDefaults. To the best of my knowledge there is no way to serialize functions or closures, and in any case they certainly are not one of the very small list of types that can be saved to UserDefaults. (Known as "property list objects" since the same small set of types can be store to both property lists and to UserDefaults.)

Why do you want to pass a function to another view controller?

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

In Swift, functions are Closures. You can simply pass closures in code.

Class A {
    var someFunction: (Int) -> String

    init(f: (Int) -> String) {
        someFunction = f
    }
}

func f2(a: Int) -> String {
    return "Value is: \(a)"
}
let AInstance = A(f: f2)
print(AInstance.someFunction(5)) // prints "Value is: 5"

or specific someFunction as optional like var someFunction: ((Int) -> String)! and set it later in code.

farzadshbfn
  • 2,710
  • 1
  • 18
  • 38
0

I'm going to answer your initial question:

I have in in my app an function. Is there a way to transfer it to other Viewcontroller?

Yes, there is a way: use your segue rather than trying to store the function in userDefaults.

1) Make sure that the destination view controller has an instance variable that can hold your function. (Note that in Swift 4, you'll have to make sure you either set a default value for that variable, or create a custom initializer to ensure the variable is given a value on initialization.)

2) In the first view controller, wherever you handle your segue, instantiate your destination view controller. Then set the variable to your function. (You can do this, for example, in an override of the prepare(for segue: UIStoryboardSegue, sender: Any?) method.)

leanne
  • 7,940
  • 48
  • 77