从 null 向后工作
我知道,一旦你在编码方面有了更好的表现,你就会知道变量是什么,并且 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据Apple的文档,
currentTitle
可能是nil。
它可能只是没有设置。您始终可以执行
if (myObject == nil)
进行检查,或者在这种情况下:检查是否按下后退或前进按钮的另一种方法是检查
id
本身。另外,请确保您的出口和操作都在 IB 中连接,并且保存并重新构建项目。我已经在 IB 中更改了一些内容,保存了
.m
文件(不是笔尖),然后就想“为什么这不起作用???”According to Apple's docs, the value for
currentTitle
may benil.
It may just not be set.You can always do
if (myObject == nil)
to check, or in this case:Another way to check if the back or forward button is pressed, is check the
id
itself.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???"我在 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.
buttonName
不能为 null,否则buttonName.currentTitle
将产生错误。因此,
currentTitle
属性本身必须为 null。或者,
currentTitle
可能是一个值为(null)
的字符串。一般来说,在 Objective-C 中,如果您有
[[[myObject aMethod] anotherMethod] xyz]
并且结果为null
,则很难知道哪个方法返回 null。但对于点语法.
,情况并非如此。buttonName
cannot be null, otherwisebuttonName.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 isnull
it's difficult to know which method returned null. But with the dot syntax.
that's not the case.