-1

I'm pretty much brand new to Objective C and iOS development, but I do have a little experience with a few other languages.

I'm currently working on an experimental learning app for myself and I wanted to add a property to UIButton. The property I wanted to add would be of an enumeration type I'm trying to define, but I'm having some difficulty understanding scope well enough to know where to define the enum and if typedef is necessary (not too familiar with typedef at all just yet either).

So, my question is where and how do I need to define an enum to be able to use the enum as a property value to extend a class that's part of an existing framework. That's a mouth full... heh.

Thanks, Tim

Mythics
  • 773
  • 1
  • 10
  • 19

2 Answers2

0

Edited to reflect the real question being asked.

Apologies. Your post seemed to reflect a more naïve understanding of Objective-C than what you really have.

The bad news is that adding a property to a class extension in a framework is not something you can do. Apple states:

A class extension bears some similarity to a category, but it can only be added to a class for which you have the source code at compile time...

If a subclass won't accomplish what you need, you may be able to use a category with associated objects. A good example is here.

MattP
  • 1,920
  • 1
  • 15
  • 22
  • 2
    NS_ENUM macro is recommended than just enum – Ryan Jul 18 '14 at 02:32
  • If you are using ARC (which the OP probably is), you should just use `(nonatomic)`. – CrimsonChris Jul 18 '14 at 02:44
  • Where does the typedef go to be usable within a class that's part of a framework? – Mythics Jul 18 '14 at 03:03
  • Do you want to put your enum in a framework to make it available to a separate project? If so, you just need to put it in a .h file and `#import` that .h file anywhere you need it. – MattP Jul 18 '14 at 03:05
  • I'm referring to the UI framework and extending the class UIButton with a custom enumeration. I assume I don't have access to the UIButton's .h file.. so.. will adding the enum to any old .h file let me set the extended property to one of the enum values? (I hope I'm using proper terms here.. quite the newb still) – Mythics Jul 18 '14 at 03:13
  • Ah, you want to add what's called a *class extension*. Apple documents it [here](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html) where you will want search for the example that adds a property called `extraProperty`. – MattP Jul 18 '14 at 03:20
  • Yes, I want a class extension and I already used that link as a reference to create one. The problem is since the property itself is of an enum type that I create, it errors out not seeming to know the value the setter is attempting to use because the setter that gets generated seems to be outside the scope of my enum's declaration. – Mythics Jul 18 '14 at 03:27
  • this can't be a class extension as those are only allowed in the main implementation file. it must be a category. – vikingosegundo Jul 18 '14 at 03:53
0

Apple provides an Macro. This increases most notably the auto completion.

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault = 0,
    UITableViewCellStyleValue1,
    UITableViewCellStyleValue2,
    UITableViewCellStyleSubtitle
};

for distinct values.

if you want bitmasking for options, where several can go together, use NS_OPTIONS

typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};

Usage as a bitmask

UIViewAutoresizing resizing = (UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin)

If you want to be able to set it from outside the class, add it to the header file

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSUInteger, InsetCellAlignment){
    InsetCellAlignmentLeft,
    InsetCellAlignmentRight
};

@interface InsetCell : UITableViewCell
@property InsetCellAlignment alignment;
@property(nonatomic)CGFloat inset;
@end

If you need to extend UIButton with some state it is a strong indication, that you are not using MVC correctly. A controller should be aware of the state and configure the button respectively.


That said you might be able to get your desired result using "Associative References" which can be used to simulate added member variables. IMO they are smelly and I never used them.

A category used along with associative references

Community
  • 1
  • 1
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • I don't have access to my mac at the moment, so I need to ask, will creating the NS_ENUM within any header file allow me to extend a class within an existing framework to include a property of the NS_ENUM's type and set the property to one of the NS_ENUM's value from within one of my own custom classes? As the property forces the generation of the setter/getter, I currently seem to be getting an error regarding the setter it creates and using one of the values I specify in my enum. Btw, all that I just said is what I was trying to say in the original post that no one seems to be addressing. – Mythics Jul 18 '14 at 03:39
  • I am not sure, what you are trying to say. but if you have code, post it along with the error you are seeing. If you are subclassing, it just should work as expected, as shown in my last code. if you are talking about categories you should be aware that categories don't allow to add any kind of member variables. there are techniques to emulate those, but this goes beyond an comment and beyond the scope of your current question. – vikingosegundo Jul 18 '14 at 03:52