On Sep 1, 2014, at 9:28 AM, Keary Suska <[email protected]> wrote:

> On Sep 1, 2014, at 4:51 AM, Dave <[email protected]> wrote:
> 
>> I have a method that takes a String and depending on it’s value needs to 
>> return an NSTextAlignment type. However, if the string is invalid, then I 
>> want to indicate that an error so I pass back -1. This is fine expect I get 
>> warnings on this code:
>> 
>> if (myTextAllignment != -1)
>>      theFieldOutlet.textAlignment = myTextAllignment;
>> 
>> Warning:
>> 
>> Comparison of constant -1 with expression of type 'NSTextAlignment' (aka 
>> 'enum NSTextAlignment') is always true
>> 
>> What is the best way to fix this?
> 
> NSTextAlignment in declared in NSText.h, as you will see by looking at the 
> constants section of the doc. It is an NSUInteger, so obviously trying to 
> push -1 is an issue. If you don't want to return a proper NSTextAlignment 
> value in some instances, than the property should not be declared as an 
> NSTextAlignment type, because clearly it is not. You can change the type to 
> NSInteger, but that may not be future-proof. The truly best approach, IMHO, 
> is to catch the error at the point it occurs, rather than relying on a 
> fragile, interpreted value. E.g. having a method:
>       - (BOOL)parseTextAlignment:(NSTextAlignment *)alignPtr
> is a smarter way to go (IMHO).

One future-proof approach that occurred to me is to declare your own enum:

typedef NS_ENUM(NSInteger, MYTextAlignment) {
    MYInvalidTextAlignment      = -1,
    MYLeftTextAlignment         = NSLeftTextAlignment,
    MYRightTextAlignment        = NSRightTextAlignment,
    MYCenterTextAlignment       = NSCenterTextAlignment,
    MYJustifiedTextAlignment    = NSJustifiedTextAlignment,
    MYNaturalTextAlignment      = NSNaturalTextAlignment
};

Although  still think it is better to trap errors early and on-time, whenever 
possible.

HTH,

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to