开发者

Custom class constants

开发者 https://www.devze.com 2023-01-31 17:29 出处:网络
I made a custom class for my iPhone project. One of the properties of this class is an int and I want to be able to assign only a handful of values to that property. I like the way Cocoa does this wit

I made a custom class for my iPhone project. One of the properties of this class is an int and I want to be able to assign only a handful of values to that property. I like the way Cocoa does this with many of its own classes, like fox example, setting the cell selection style in a UITableView. I'd like to implement this in my own class.

I opened up the headers for some of Cocoa's classes and found out a bunch of typedef enums around there but being new to objective-c I couldn't quite figure out how to make them work for what I wanted to do (or even where to put these).

typedef enum {
    UITableViewCellSelectionStyleNone,
    UITableViewCellSelectionStyleBlue,
    UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle;

So to recap... just like in Cocoa you'd do:

cell.accessoryType = UITableViewCellAccessoryNone;

I'd like to do the same thing with my own classes:

myObject.m开发者_运维问答yProperty = MyObjectMyPropertySomeValue;

Any help is greatly appreciated.


Declare an enum to contain all the various options and name it:

typedef enum {
    MyObjectMyPropertySomeValue,
    MyObjectMyPropertySomeOtherValue,
    MyObjectMyPropertySomeThirdValue
} MyObjectMyPropertyValues;

Then, in your code,

@property (nonatomic, assign, readwrite) MyObjectMyPropertyValues myProperty;

I presume that you can figure out the rest - it's pretty standard.

Typed in bowser without being tested.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号