开发者

When putting an Outlet in Xcode into a variable, don't know what kind of pointer to use?

So I was going through Cocoa Programming for Mac OS X for Dummies by Erick Tejkowski. After doing the calculator example, I got the basics of Objective-C in Xcode, since I know basic stuff. I got that to开发者_StackOverflow中文版 put what's in a text field into a variable, you first have to put the text field as an outlet in the header file like this:

#import <Cocoa/Cocoa.h>

@interface Mah_Application__It_is_awesomeAppDelegate : NSObject <NSApplicationDelegate>
{

    NSWindow *window;
    IBOutlet id hi;

}
- (IBAction)Calculate:(id)sender;
@property (assign) IBOutlet NSWindow *window;

@end

And then you put it in a variable like so:

- (IBAction)Calculate:(id)sender
{

    int something;
    hi = [hi intValue];

}

However, when I want a Boolean value, like a checkbox or something, or perhaps even a radio group, I don't know what pointer to use in place of intValue. For a moment, assume the variable hi is now a boolean. I tried this:

- (IBAction)Calcluate:(id)sender
{

    BOOL something;
    something = [hi BOOL];

}

but it says that's not a valid pointer. What should I use, then?


boolValue is defined for NSString and NSNumber, so it depends on what type hi is

- (IBAction)Calcluate:(id)sender
{
    BOOL something = [hi boolValue];
}


Edit (I re-read the question and you might want what the other answer from Stew suggested):

BOOL myBool = [@"1" boolValue];
myBool = [someObject boolValue];

These are some of the conversion methods for NSString - Also see the NSString Reference Here:

- (double)doubleValue;
- (float)floatValue;
- (int)intValue;
- (NSInteger)integerValue;
- (long long)longLongValue;

// (boolValue) Skips initial space characters (whitespaceSet), 
// or optional -/+ sign followed by zeroes. 
// Returns YES on encountering one of "Y", "y", "T", "t", or a digit 1-9. 
// It ignores any trailing characters.
- (BOOL)boolValue;


Also, from NSNumber Class Reference:

boolValue Returns the receiver’s value as a BOOL.

- (BOOL)boolValue

Return Value The receiver’s value as a BOOL, converting it as necessary.

Special Considerations Prior to Mac OS X v10.3, the value returned isn’t guaranteed to be one of YES or NO. A 0 value always means NO or false, but any nonzero value should be interpreted as YES or true.


Try this:

[NSNumber numberWithBool:YES]
[NSNumber numberWithBool:NO]
[NSNumber numberWithInt:1]
[NSNumber numberWithFloat:0.25]
// etc...

- (IBAction)Calcluate:(id)sender
{

    BOOL something;
    something = [hi [NSNumber numberWithBool:BOOL]];

}

NSNumber reference

Since things like BOOL, NSInteger, NSUInteger, etc are not pointers, they cannot do certain things like be values for a dictionary, etc. For this purpose, NSNumber was created. It wraps these types in a class so they can be used as arguments for passing to selectors, or stored in NSDictionary's.

Here is a common thing I do:

[self.myLoadingView performSelectorOnMainThread:@selector(setHidden:) withObject:[NSNumber numberWithBool:YES] waitUntilDone:YES];

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜