Questions tagged [class-extensions]
102 questions
89
votes
4 answers
Class extension vs class category
Class extensions @interface Class () are a lot more powerful and can inject variables into the class. Categories @interface Class (Category) can't.
What other differences are there, and when should one use a category over a class extension?

AWF4vk
- 5,810
- 3
- 37
- 70
36
votes
5 answers
Objective-C class extension
As a fairly new objective-c programmer (with a 4 years Java experience), I seem to be having a hard time understanding when to use class extensions. From what I understood (and please, correct me if I'm wrong), the main difference between categories…

Jean-Francois Gagnon
- 3,141
- 4
- 20
- 27
18
votes
2 answers
What's the harm of property override in Objective-C?
There are several situations where you might override a super class's property.
You declare a property with the same name and same attribute of its
superclass'.(since if you change the
attribute you can get an compiler
warning).And you can…

Jimmy
- 1,094
- 10
- 22
14
votes
2 answers
Swift and using class extension
I don't understand why programmers use the extension keyword in their class implementation. You can read in other topics that code is then more semantically separated and etc. But when I work with my own code, it feels clearer to me to use // MARK -…

Deny
- 1,441
- 1
- 13
- 26
13
votes
6 answers
Flutter extension-methods not working, it says "undefined class" and "requires the extension-methods language feature"
I'm slowly building my personal website over at dlblack.dev, and I'm trying to spice it up a little bit. For example, from a computer (rather than a tablet or phone since they don't have mouse pointers), if you hover over any of the clickable items,…

VitaminTussle
- 441
- 1
- 3
- 12
13
votes
2 answers
TypeScript: How to add static methods to built-in classes
Is there anyhow anyway to add some static method to types like Date, String, Array, etc?
For example I want to add method today to Date class and in JavaScript I can simply add a property to it or maybe I use Object.defineProperty:
Date.today =…

Morteza Tourani
- 3,506
- 5
- 41
- 48
13
votes
4 answers
Can I extend a final class in Swift?
I'm using a third-party library for a new app that I'm making using Swift. The author of the class/library has made it final using the final keyword, probably to optimise and to prevent overriding its properties and methods.
Example:
final public…

metpb
- 513
- 8
- 20
12
votes
4 answers
ObjectiveC: where to declare private instance properties?
I have the following class interface:
@interface MyClass : NSObject
@property int publicProperty;
@end
then the implementation:
@interface MyClass() // class extension
- (void)privateMethod; // private methods
@end
@implementation MyClass {
…

hzxu
- 5,753
- 11
- 60
- 95
9
votes
2 answers
Use Class Extension for Selective Visibility in Objective-C
Would it make any sense to put class extensions in their own .h files and #import them selectively to get various levels of visibility for a class' methods and properties? If this is a bad idea (or would not work), why?

Dan Rosenstark
- 68,471
- 58
- 283
- 421
9
votes
2 answers
Class extension vs. subclassing in Swift?
I'am using a third-party framework that provides a class whose instances only have properties. In my app, I'd like to add an extra property to this instances. What the appropriate way to do this would it be for this scenario?
a) Extending the…

AppsDev
- 12,319
- 23
- 93
- 186
8
votes
3 answers
Objective-C: Should I declare private methods?
I've been declaring private methods in class extensions, according to Best way to define private methods for a class in Objective-C.
But, I just realized that, in Xcode 4, if I leave out the declaration of a private method altogether and just…

ma11hew28
- 121,420
- 116
- 450
- 651
8
votes
3 answers
"Expected expression in Swift key path" Error while Refactoring code with Extensions
I have been trying refactor code by thanking functions and adding them in a separate file extension of a ViewController
What I have in this extension is a function that adds gesture recognizers to some views that have references to functions that I…

ISS
- 406
- 6
- 24
8
votes
1 answer
Issue with extensions in Objective-C
The following is a code snippet which deals with class extensions. What I am trying to do is generate a random ID called internal ID (that is used by the program later on) which is stored in an encrypted form in memory. The code fails to compile…

hecate
- 620
- 1
- 8
- 33
8
votes
1 answer
Visibility of properties in Class Extensions and inheritance in Objective C
Say I have 2 classes: Money and Dollar, where Dollar inherits from Money.
Money has a property declared in a class extension:
#import "Money.h"
@interface Money ()
@property(nonatomic) NSUInteger amount;
@end
@implementation Money
@end
Dollar…

cfischer
- 24,452
- 37
- 131
- 214
7
votes
2 answers
Identity 2.0: Creating custom ClaimsIdentity eg: User.Identity.GetUserById(int id) for Per Request Validation
See this similar question: Need access more user properties in User.Identity
I would like to create custom authentication methods to use with my Razor Views that allows easy access IdentityUser properties relational to the User.Identity object but I…

yardpenalty.com
- 1,244
- 2
- 17
- 32