gpt4 book ai didi

ios - UItextfield 用空字符串初始化?不为空?也没有?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:47:18 25 4
gpt4 key购买 nike

我有一个 uitextfield,当它被初始化并且我没有向它输入任何值时,我发现 uitextfield 的值不是 null 也不是 nil。

 NSString *notes = (notesField.text)?(notesField.text):@"hello";
NSLog(@"notes: %@",notes);

它不返回任何注释

    NSString *notes1;

//or use legnth

if ([notesField.text isEqual:@""]) {
notes1=@"hello";
NSLog(@"empty textfield: %@",notes1);
//then it returns "hello"
}
else
{
notes1=notesField.text;
NSLog(@"not empty textfield: %@",notes1);
}

这是为什么呢?我还能使用三元运算符吗?像这样?

NSString *notes = ([notesField.text length])?(notesField.text):@"hello";

最佳答案

你可以使用

NSString *notes = ([notesField.text length])?(notesField.text):@"hello";

NSString *notes = ([notesField.text length]==0)?@"hello":(notesField.text);

NSString *notes = ([notesField.text isEqualToString:@""])?@"hello":(notesField.text);

如果您的 UITextField 没有条目(初始情况),请使用第二个或第三个选项,这样会更好。 NSString *notes = ([notesField.text length])?@"hello":(notesField.text); 不会像你期望的那样正常工作,因为 notesField.text将为 TRUE 即使文本字段中没有文本。因此,您应该使用 notesField.text.length[notesField.text isEqualToString:@""]

希望现在晴朗。

关于ios - UItextfield 用空字符串初始化?不为空?也没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15385319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com