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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/798030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:27:44  来源:igfitidea点击:

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

iphoneobjective-cpropertiesconstructor

提问by MrHus

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.

希望你能帮忙。

回答by Lyndsey Ferguson

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.

您可能会发现阅读一些有关 Objective-C 或 Cocoa 的文档很有用。如果您在 StackOverflow 或 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".

要回答您的问题,您应该拥有 CalcViewController 的 @implementation。人们通常会将这个@implementation 放在 *.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. 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
  • 您可能希望将该文件重命名为 CalcViewController。我相信 Xcode 的重构引擎更容易处理。
  • 当您将显示实例变量与 Interface Builder 连接时,您不需要为显示实例变量声明 @property。除非您希望 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 版本。

回答by Hyman

Very good question. The short answer is to init the values in the init function. You need to overwrite the default init function so that the default value is ready before the object can be used. I would like to suggest people stop suggesting others to read document; please answer the question directly if you can.

很好的问题。简短的回答是初始化 init 函数中的值。您需要覆盖默认的 init 函数,以便在使用对象之前准备好默认值。我想建议人们停止建议他人阅读文档;如果可以,请直接回答问题。

回答by Marc Charbonneau

Another option is to write your own getter for your input property, and return @"" if the instance variable is nil. This would work even if you accidentally or intentionally assigned nil to input, whereas using init to set a default value would break.

另一种选择是为输入属性编写自己的 getter,如果实例变量为 nil,则返回 @""。即使您不小心或故意将 nil 分配给输入,这也会起作用,而使用 init 设置默认值会中断。

回答by Anil

The most obvious way to implement it would be like following:

最明显的实现方式如下:

  1. In your viewcontroller.m file, override the default init method. 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];

  1. 在您的 viewcontroller.m 文件中,覆盖默认的 init 方法。每次初始化视图控制器时都会调用此方法。因此,这是初始化变量的最佳方式。

    - (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. 现在,无论您想在代码中的何处将字符串附加到原始字符串,只需使用: varName = [NSString stringwithformat:@"%@%@", varName, newString];