ios - UItextfield initialized with empty string? not null? nor nil? -
i have uitextfield, when initialized , didn't input values it, found value of uitextfield not null nor nil.
nsstring *notes = (notesfield.text)?(notesfield.text):@"hello"; nslog(@"notes: %@",notes); it returns nothing notes
nsstring *notes1; //or use legnth if ([notesfield.text isequal:@""]) { notes1=@"hello"; nslog(@"empty textfield: %@",notes1); //then returns "hello" } else { notes1=notesfield.text; nslog(@"not empty textfield: %@",notes1); } why that? can still user ternary operator ? ?
nsstring *notes = ([notesfield.text length])?(notesfield.text):@"hello";
you can use
nsstring *notes = ([notesfield.text length])?(notesfield.text):@"hello"; or
nsstring *notes = ([notesfield.text length]==0)?@"hello":(notesfield.text); or
nsstring *notes = ([notesfield.text isequaltostring:@""])?@"hello":(notesfield.text); and case when uitextfield has no entry (initial case), use second or third option , beter. nsstring *notes = ([notesfield.text length])?@"hello":(notesfield.text); won't work fine expect because notesfield.text true if there no text in textfield. should use notesfield.text.length or [notesfield.text isequaltostring:@""].
hope clear now.
Comments
Post a Comment