How to create function variables with parameter in extensions? - swift

When working with Swift - Extensions
As you know, we can create variables for function. i want to create a variable which can hold reference to a function which has parameters.
i have managed to create variable for a function which didn't have any parameters defined for
My code is as follows :-
extension UIAlertController {
var cancelBlock : ()->Void { return {} }
var nameBlock : (nameArg:String?)->Void { }
}
I am getting the following error with "nameBlock"
Computed property must have accessors specified
How should i specify return value ?

use
var nameBlock : (nameArg:String?)->Void? { return nil }

Related

Assigning a Function with Generic Parameter to a Variable

I'm trying to assign a function, which takes a generic parameter, to a variable.
I have a class, Loudspeaker, with a function, saySomething. The saySomething function is meant to take as a parameter any object conforming to the protocol SpeakProtocol:
class Loudspeaker {
func saySomething<T:SpeakProtocol> (speaker:T) {
print("\(speaker.thinkOfSomethingToSay())")
}
}
protocol SpeakProtocol {
func thinkOfSomethingToSay() -> String
}
The ThingSayer class implements SpeakProtocol:
class ThingSayer:SpeakProtocol {
func thinkOfSomethingToSay() -> String {
return "Hello"
}
}
I create a new member of the Loudspeaker class:
var aLoudspeaker:Loudspeaker
And I would like to assign the saySomething function (not the result of that function) to a variable:
var speakFunction = aLoudspeaker.saySomething
I would then invoke this function elsewhere by creating a ThingSayer and passing that to speakFunction:
var aSpeaker:Thingsayer
speakFunction(speaker:thingSayer)
The problem is with this line:
var speakFunction = aLoudspeaker.saySomething
Which gives me an error: "Generic parameter 'T' could not be inferred."
I've been beating my head against a wall for quite a while with this. Is there any way to do what I'm trying to do?
This is obviously a contrived example, but the gist of the problem is that I have a function, which takes a generic parameter, and I want to assign that function to a variable to be invoked elsewhere in my code. Any help would be most appreciated.
var speakFuction: (speaker: ThingSayer) -> Void = aLoudspeaker.saySomething
You'll need to explicit the type of your function when declaring the variable to help the compiler.

Self in protocol always need to be optional?

Example:
internal protocol PropertyProtocol {
var property: Self {
get
}
}
The only option I see to implement it, let us say in a class is
internal final class PropertyClass: PropertyProtocol {
let property: PropertyClass
internal init(otherOne pOtherOne: PropertyClass) {
self.property = pOtherOne
}
}
But then I do not see a possibility to use it.
let test: PropertyProtocol = PropertyProtocol(...) // hmm, how?
Does Self in a protocol property type declaration always have to be optional?
As a stored property, indeed it would have to be optional for you to create an instance, as each instance would require the stored property to be assigned during initialisation – leading to recursive behaviour. Therefore Self doesn't make too much sense as a stored property; it's really more designed to be used with methods or calculated properties.
Depending on what you're using this for (seems like a fairly hypothetical example), you could implement a calculated property like so:
protocol PropertyProtocol {
var property : Self { get }
}
final class PropertyClass : PropertyProtocol {
var property : PropertyClass {
get {
return // ...
}
set {
// ...
}
}
}
That way the class itself can manage the creation of the property when it's accessed, preventing the recursive behaviour of requiring it to be assigned during initialisation.

Whats the difference between these two properties?

What's the difference between these two??
var sharedContextA: NSManagedObjectContext {
return CoreDataStackManager.sharedInstantce().managedObjectContext
}
var sharedContextB = {
return CoreDataStackManager.sharedInstantce().managedObjectContext
}()
To clarify, I have seen:
var variable: Type {
code
return X
}
but I don't know the name of this or how it is different than the former:
var variable = {
code
return X
}()
sharedContextA is a computed property. The value to be returned is computed each time the getter of the property is called.
sharedContextB uses a closure to assign a default value to the property. The closure is executed once during initialization of the type the property belongs to, afterwards the stored value is read directly.

Unable to use NSFontManager methods in swift

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...).

How to call instance method inside class using Swift

I am having an issue with calling an instance method within the class itself. If someone can provide me some insight it would be greatly appreciated.
My current code looks like this:
class Rect
{
func printthis() -> String {
return "this is working or what"
}
var toPrint:String = self.printthis()
}
The error I am getting in Xcode is: Use of unresolved identifier 'self'.
What am I missing here?
You can't call an instance method without an instance. The class is merely the template for instances. So i don't what you are trying to do here...
But the answer is no, you cannot call an instance method form the class definition because there is no instance yet.
Perhaps you want to delcare a class method and use that to set an instance variable on creation? If so, you might do that like this:
class Rect {
class func printthis() -> String {
return "this is working or what"
}
var toPrint:String
init() {
toPrint = Rect.printthis()
}
}
var r = Rect()
println(r.toPrint) //-> this is working or what
An instance of a class is not initialized and able to be referenced (even as 'self') until all of its variables have been assigned values.
An option that may work for you is to declare your variable as an implicitly-unwrapped optional, which is assigned nil by default. Then in the class's init method, since all of the variables have been assigned values, you are able to start calling methods on your instance.
class Rect {
var toPrint: String!
init() {
toPrint = printthis()
}
printthis() -> String {
return "this will work"
}
}
the problem is that swift is strict about initing all properties.
you may as a workaround
class Rect
{
func printthis() -> String {
return "this is working or what"
}
var toPrint:String = ""
init() {
toPrint = printthis()
}
}