Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-c Iphone how to set default value for a property

Hi i've got the following code in a ViewController.h:

#import <UIKit/UIKit.h>

@interface CalcViewController : UIViewController {
    NSNumber* result;
    NSString* input;
    //NSString* input = @"";

    IBOutlet UITextField* display;
}

@property (retain) NSNumber* result;
@property (retain) NSString* input;
@property (nonatomic, retain) UITextField* display;

@end

The problem is that I want to append a string to input but this is not possible when its still null. Thats why I want to set the default value of input to be @"". But where do I put this code.

I'm aware of one possible solution where you put it in a default constructor. But I've got no idea in what file to put this. And where I should call it from.

I unfortunately only got a limited understanding of C and realise that perhaps a .h file is not the right place.

The project type is a View-Based-Application if you should need this.

Hope you can help.

like image 496
MrHus Avatar asked Apr 28 '09 13:04

MrHus


People also ask

How do I set default value in property?

Right-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value. Press CTRL+S to save your changes.

What is the default value property?

The DefaultValue property specifies text or an expression that's automatically entered in a control or field when a new record is created.

Which is the default for synthesized properties?

Default Synthesis Of Properties Clang provides support for autosynthesis of declared properties. Using this feature, clang provides default synthesis of those properties not declared @dynamic and not having user provided backing getter and setter methods.


1 Answers

You may find it useful to read some documentation on Objective-C or Cocoa. You probably can find some good suggestions on reading material if you perform a search here in StackOverflow or on Google.

To answer your question, you should have a @implementation of CalcViewController. One would most often place this @implementation inside of the *.m file. If your *.h file is named "ViewController.h" then the implementation would go in "ViewController.m".

You would then create a copy of the UIViewController's initialization function and place it there (I don't know what the default init function is).

For example:

@implementation CalcViewController

@synthesize result;
@synthesize input;

- (id)initWithNibName:(NSString*)aNibName bundle:(NSBundle*)aBundle
{
    self = [super initWithNibName:aNibName bundle:aBundle]; // The UIViewController's version of init
    if (self) {
        input = [[NSString alloc] initWithString:@""]; // You should create a new string as you'll want to release this in your dealloc
    }
    return self;
}

- (void)dealloc
{
    [input release];
    [super dealloc];
}
@end // end of the @implementation of CalcViewController

NOTES:

  • You may want to rename the file to CalcViewController. I believe it is easier for Xcode's refactoring engine to deal with.
  • You do not need to declare the @property for the display instance variable as you connect that with Interface Builder. Unless you want clients of the CalcViewController to change it often

EDIT: April 28, 2009: 10:20 AM EST: I suggest actually allocating a NSString as you should technically release it in the dealloc.

EDIT: April 28, 2009: 11:11 AM EST: I updated the @implementation to use the UIViewController's version of init.

like image 193
Lyndsey Ferguson Avatar answered Oct 16 '22 14:10

Lyndsey Ferguson