开发者

Objective-C setter/getter naming conventions drive me mad?

I have been trying to understand something for several hours and I would like to get your point of view.

I have setter/getter on one of my class properties (I noticed that I MUST add "set" in front of the setter name else the compiler says that there is no setter):

@property (nonatomic, retain, readwrite, setter=setTopString:, ge开发者_开发百科tter=TopString) NSString* m_topString;

When I call the setter like this, the compiler is happy:

[secureKeyboardController setTopString:@"This action requires that your enter your authentication code."];

But when I try to use the "dot" convention, then I am rejected by the compiler:

                secureKeyboardController.topString = @"This action requires that your enter your authentication code.";

What is really weird is that the dot naming convention works fine with this property:

@property (nonatomic, readwrite, getter=PINMaxLength, setter=setPINMaxLength:) NSInteger m_PINMaxLength;

In this case i can do:

[secureKeyboardController setPINMaxLength:10];enter code here

or

secureKeyboardController.PINMaxLength = 10;

In both cases, the compiler is happy.

I really would like to fall asleep tonigh less stupid than I currently feel now. Thus any explanation would be greatly appreciated.

Regards, Apple92


What you're doing is declaring properties as if you were declaring instance variables. You should not be using the names in the getter and setter attributes on the @property declaration with dot syntax; that it happens to be working now is not - so far as I know - by design.

The property should be what you use with dot syntax. For some reason - unfamiliarity with Cocoa coding conventions, I expect - you named your properties m_topString and m_PINMaxLength. That means you should use them as someObject.m_topString and someObject.m_PINMaxLength.

If you want to use those names for the instance variables that you've decided to use for the properties' backing storage, you should declare that in the @synthesize directive instead.

This is how your class should look, to be more in line with regular Cocoa and Objective-C coding conventions:

@interface SomeClass : NSObject {
@private
    NSString *m_topString;
}
@property (nonatomic, readwrite, copy) NSString *topString;
- (id)initWithTopString:(NSString *)initialTopString;
@end

@implementation SomeClass
@synthesize topString = m_topString;
    // this says to use the instance variable m_topString
    // for the property topString's storage

- (id)initWithTopString:(NSString *)initialTopString {
    if ((self = [super init])) {
        m_topString = [initialTopString copy];
            // use the ivar directly in -init, not the property
    }
    return self;
}

- (void)dealloc {
    [m_topString release];
        // use the ivar directly in -dealloc, not the property

    [super dealloc];
}

- (NSString *)description {
    return [NSString stringWithFormat:@"SomeClass (%@)", self.topString];
        // elsewhere in your class, use the property
        // this will call through its getter and setter methods
}
@end


You are trying to fight the compiler, and the compiler fights back.

You are trying to declare a property named m_topString with setter setTopString and getter TopString, and that is plainly stupid. You are writing Objective-C code, not C++. Your code will be a maintenance nightmare (unless the next maintainer is just sensible and changes your code to Objective-C conventions).

Do yourself a favour, start writing Objective-C code. Just call the property topString, don't pick your own names for the setter and getter, don't pick your own names for the instance variable, and everything works just fine.


Capitalize the T in TopString, i.e. secureKeyboardController.TopString I'm 90% sure that will fix your problem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜