从 null 向后工作

发布于 2024-10-02 08:04:58 字数 393 浏览 2 评论 0原文

我知道,一旦你在编码方面有了更好的表现,你就会知道变量是什么,并且 null 可能不会在这里或那里弹出。在达到这种心态的过程中,是否有任何方法可以阻止声称为空的变量并验证它确实为空,或者您只是使用了错误的代码?

示例:

-(IBAction) startMotion: (id)sender {
    NSLog(@"Forward or back button is being pressed.");
    UIButton * buttonName = (UIButton *) sender;
    NSLog(@"Button Name:  %@", buttonName.currentTitle);
}

按钮名称:(null)是控制台中显示的内容

谢谢

I know that once you get better at coding you know what variables are and null popping out here and there may not occur. On the way to that state of mind are there any methods to corner your variable that's claiming to be null and verify that it is indeed null, or you just using the wrong code?

Example:

-(IBAction) startMotion: (id)sender {
    NSLog(@"Forward or back button is being pressed.");
    UIButton * buttonName = (UIButton *) sender;
    NSLog(@"Button Name:  %@", buttonName.currentTitle);
}

Button Name: (null) is what shows up in the console

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱殇璃 2024-10-09 08:04:58

根据Apple的文档, currentTitle 可能是nil。 它可能只是没有设置。

您始终可以执行 if (myObject == nil) 进行检查,或者在这种情况下:

-(IBAction) startMotion: (id)sender {
  NSLog(@"Forward or back button is being pressed.");
  UIButton * buttonName = (UIButton *) sender;
  if (buttonName != nil) {
    NSString *title = buttonName.currentTitle;
    NSLog(@"Button Name:  %@", title);
  }
}

检查是否按下后退或前进按钮的另一种方法是检查 id本身。

//in the interface, and connect it up in IB
//IBOutlet UIButton *fwdButton;
//IBOutlet UIButton *bckButton;

-(IBAction) startMotion: (id)sender {
  NSLog(@"Forward or back button is being pressed.");
  UIButton * buttonName = (UIButton *) sender;
  if (buttonName == fwdButton) {
    NSLog(@"FWD Button");
  }

  if (buttonName == bckButton) {
    NSLog(@"BCK Button");
  }
}

另外,请确保您的出口和操作都在 IB 中连接,并且保存并重新构建项目。我已经在 IB 中更改了一些内容,保存了 .m 文件(不是笔尖),然后就想“为什么这不起作用???”

According to Apple's docs, the value for currentTitle may be nil. It may just not be set.

You can always do if (myObject == nil) to check, or in this case:

-(IBAction) startMotion: (id)sender {
  NSLog(@"Forward or back button is being pressed.");
  UIButton * buttonName = (UIButton *) sender;
  if (buttonName != nil) {
    NSString *title = buttonName.currentTitle;
    NSLog(@"Button Name:  %@", title);
  }
}

Another way to check if the back or forward button is pressed, is check the id itself.

//in the interface, and connect it up in IB
//IBOutlet UIButton *fwdButton;
//IBOutlet UIButton *bckButton;

-(IBAction) startMotion: (id)sender {
  NSLog(@"Forward or back button is being pressed.");
  UIButton * buttonName = (UIButton *) sender;
  if (buttonName == fwdButton) {
    NSLog(@"FWD Button");
  }

  if (buttonName == bckButton) {
    NSLog(@"BCK Button");
  }
}

also, make sure your outlets and actions are all connected in IB, and that you save and re-build the project. I've gone where I changed somehting in IB, saved the .m file (not the nib) and was like "why isn't this working???"

沧笙踏歌 2024-10-09 08:04:58

我在 Interface Builder 中使用了错误的字段,我使用 Interface Builder 标识中的名称而不是按钮设置中的标题。

I was using the wrong field in Interface Builder I was using Name from the Interface Builder Identity instead of Title from the button settings.

满地尘埃落定 2024-10-09 08:04:58

buttonName 不能为 null,否则 buttonName.currentTitle 将产生错误。

因此,currentTitle 属性本身必须为 null。

或者,currentTitle 可能是一个值为 (null) 的字符串。

一般来说,在 Objective-C 中,如果您有 [[[myObject aMethod] anotherMethod] xyz] 并且结果为 null,则很难知道哪个方法返回 null。但对于点语法 . ,情况并非如此。

buttonName cannot be null, otherwise buttonName.currentTitle would produce an error.

Therefore the currentTitle attribute itself must be null.

Or, maybe currentTitle is a string with the value (null).

In general, in Objective-C, if you have [[[myObject aMethod] anotherMethod] xyz] and the result is null it's difficult to know which method returned null. But with the dot syntax . that's not the case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文