Questions tagged [convenience-methods]

Convenience methods are shorthand abstractions which invoke another method by implicitly passing in default argument(s) for a common use case

Convenience methods are intended to encapsulate the verbosity of an existing API into a single expression by aliasing a method call or set of method calls with default arguments to a new method which represents a specific operation.

References

55 questions
1
vote
1 answer

Is convenience keyword really necessary in swift?

The convenience keyword in swift completely confused me. It doesn't seem useful at all. Calling other initializer (or say constructor) in same or super class is a very common feature in object oriented languages, like Java, C# and etc. If any member…
jarly
  • 231
  • 1
  • 4
  • 16
1
vote
2 answers

Why can't I use "self.init(type: .custom)" in convenience initializer at my subclass of UIButton

I know must call a designated initializer of the superclass, I think init(type: UIButtonType) had called a designated initializer, so I used it in subclass convenience initializer, but failed class TSContourButton: UIButton { enum…
GorCat
  • 174
  • 3
  • 11
1
vote
1 answer

UNNotificationCategory subclass init issue

I want to subclass UNNotificationCategory(UserNotifications), because I want to use enums instead of hard coded strings as category identifiers. There is one convenience init inside UNNotificationCategory definition public convenience…
1
vote
1 answer

In Swift, get syntax error in convenience initializer when creating an object

I know a little about how to use convenience initializer and designated initializer.Here is a sample class called ClassA class ClassA { var number:Int convenience init(){ self.init(newNumber: 10) } init(newNumber: Int) { …
sevenkplus
  • 178
  • 1
  • 12
1
vote
1 answer

Is there a handy way to pass function arguments to initialize a Dictionary(of string,string) in Vb.net

We can do this in vb.net: Dim d= new Dictionary(of string, string) from {{"a","valA"},{"b","valB"}} Please how can we make the following possible for convenience: public sub Setup(args) Dim d= new Dictionary(of string, string) from args end…
Charles Okwuagwu
  • 10,538
  • 16
  • 87
  • 157
1
vote
1 answer

How to set a non optional variable with a convenience initializer in swift?

I have a UITableViewController subclass, that should not work if there is no Model. If there is no Model, there isn't really any purpose in showing the View. So I'm thinking, my model property should not be an optional value. I want this kind of…
nmdias
  • 3,888
  • 5
  • 36
  • 59
1
vote
4 answers

Dealing with objects returned from cocoa convenience methods

I'm having a lot of issues with NSDate objects being prematurely deallocated. I suspect that the issues may be related to the way that I deal with the objects returned from NSDate convenience methods. I think that my showDate property declaration in…
kubi
  • 48,104
  • 19
  • 94
  • 118
1
vote
0 answers

Benefits of @implementation Convenience methods

I have been looking through sample code provided by Apple and stumbled upon something interesting I've not learned about. In a subclass in the .m file, there are multiple sections declared with @implementation. The last one is the usual…
Jordan H
  • 52,571
  • 37
  • 201
  • 351
1
vote
2 answers

return type from init methods in Objective-C

I use the following convenience method structure when writing Objective-C classes: + (MyClass *) myClass { return [[[self alloc] init] autorelease]; } - (id) init { if (self = [super init]) { // set-up code here... } return…
SundayMonday
  • 19,147
  • 29
  • 100
  • 154
1
vote
1 answer

What is the convenience method to compare two NSNumbers with float values using limited precision?

What is the Objective-C convenience method for comparing a couple of NSNumbers with float values to see if they are roughly equivalent (two decimal places of precision is fine)? I would use something other than floats if there was an option that…
0
votes
1 answer

How can I make that an object of the same class become the return value of initializer?

I have this function: extension UIImage { static func from(layer: CALayer) -> UIImage? { UIGraphicsBeginImageContext(layer.frame.size) layer.render(in: UIGraphicsGetCurrentContext()!) let outputImage: UIImage? =…
Chen Li Yong
  • 5,459
  • 8
  • 58
  • 124
0
votes
2 answers

Extra argument in call, Convenience Initializers

What am I doing wrong here? Everything seems fine. Function signatures are correct. I see no reason why the parent would be an extra argument. import CloudKit import CoreLocation public enum OrderDirection { case ascending, descending } open…
user1563544
  • 379
  • 3
  • 17
0
votes
2 answers

Memory management in iOS / ManagedObjectContext

Looks like I did not understand memory management in Objective C... sigh. I have the following code (note that in my case, placemark.thoroughfare and placemark.subThoroughfare are both filled with valid data, thus both if-conditions will be…
Axel
  • 1,716
  • 1
  • 16
  • 28
0
votes
2 answers

Swift: how to create a convenience init for UIAlertController with no parameters

I am trying to create a convenience init with no parameters for a subclassed UIAlertController, but it is giving me an error. It gives me this error: Use of self in delegating initializer before self.init is called class AvatarSelectionAlert:…
The Nomad
  • 7,155
  • 14
  • 65
  • 100
0
votes
2 answers

Convenience class method vs alloc init

I understand that objects may perform more effectively if you manually allocate and initialize them (ARC should have taken care of the majority of this, right?), however, is it really THAT BIG of a difference in performance if you were to just use…
giuseppe
  • 17
  • 4