简体   繁体   English

Objective-c Iphone如何设置属性的默认值

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

Hi i've got the following code in a ViewController.h: 您好我在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. 遗憾的是,我对C的理解有限,并且意识到也许.h文件不是正确的地方。

The project type is a View-Based-Application if you should need this. 如果您需要,项目类型是基于视图的应用程序。

Hope you can help. 希望你能帮忙。

You may find it useful to read some documentation on Objective-C or Cocoa. 您可能会发现阅读Objective-C或Cocoa上的一些文档很有用。 You probably can find some good suggestions on reading material if you perform a search here in StackOverflow or on Google. 如果您在StackOverflow或Google上执行搜索,您可能会在阅读材料上找到一些很好的建议。

To answer your question, you should have a @implementation of CalcViewController. 要回答你的问题,你应该有一个@implementation of CalcViewController。 One would most often place this @implementation inside of the *.m file. 人们通常会将此@implementation放在* .m文件中。 If your *.h file is named "ViewController.h" then the implementation would go in "ViewController.m". 如果您的* .h文件名为“ViewController.h”,那么实现将进入“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). 然后,您将创建UIViewController的初始化函数的副本并将其放在那里(我不知道默认的init函数是什么)。

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. 您可能希望将文件重命名为CalcViewController。 I believe it is easier for Xcode's refactoring engine to deal with. 我相信Xcode的重构引擎更容易处理。
  • You do not need to declare the @property for the display instance variable as you connect that with Interface Builder. 当您将它与Interface Builder连接时,您不需要为显示实例变量声明@property。 Unless you want clients of the CalcViewController to change it often 除非您希望CalcViewController的客户端经常更改它

EDIT: April 28, 2009: 10:20 AM EST: I suggest actually allocating a NSString as you should technically release it in the dealloc. 编辑: 2009年4月28日:美国东部时间上午10:20:我建议实际分配一个NSString,因为你应该在dealloc技术上释放它。

EDIT: April 28, 2009: 11:11 AM EST: I updated the @implementation to use the UIViewController's version of init. 编辑: 2009年4月28日:美国东部时间上午11:11:我更新了@implementation以使用UIViewController的init版本。

Very good question. 非常好的问题。 The short answer is to init the values in the init function. 简短的回答是在init函数中初始化值。 You need to overwrite the default init function so that the default value is ready before the object can be used. 您需要覆盖默认的init函数,以便在使用对象之前准备好默认值。 I would like to suggest people stop suggesting others to read document; 我想建议人们不要建议其他人阅读文件; please answer the question directly if you can. 如果可以,请直接回答问题。

Another option is to write your own getter for your input property, and return @"" if the instance variable is nil. 另一种选择是为输入属性编写自己的getter,如果实例变量为nil则返回@“”。 This would work even if you accidentally or intentionally assigned nil to input, whereas using init to set a default value would break. 即使您意外或故意将nil分配给输入,这也会起作用,而使用init设置默认值会中断。

The most obvious way to implement it would be like following: 最明显的实现方式如下:

  1. In your viewcontroller.m file, override the default init method. 在viewcontroller.m文件中,覆盖默认的init方法。 This method is called everytime a view controller is initialized. 每次初始化视图控制器时都会调用此方法。 So, it's the best way to initialize your variable. 因此,这是初始化变量的最佳方式。

     - (id)initWithNibName:(NSString*)aNibName bundle:(NSBundle*)aBundle { self = [super initWithNibName:aNibName bundle:aBundle]; // The UIViewController's version of init if (self) { varName = [[NSString alloc] initWithString:@""]; } return self; } 
  2. Now, whereever in your code you want to append a string to your original string, simply use: varName = [NSString stringwithformat:@"%@%@", varName, newString]; 现在,在您的代码中,您想要将字符串附加到原始字符串,只需使用: varName = [NSString stringwithformat:@"%@%@", varName, newString];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM