Here\'s the code:
- (IBAction) charlieInputText:(id)sender {
//getting value from text field when entered
charlieInputSelf = [sender stringValue];
Joshua has the right answer in the narrow case, but generally, you can't compare string objects using the == or != operators. You must use -isEqual:
or -isEqualToString:
This is because charlieImputSelf
and @""
are actually pointers to objects. Although the two sequences of characters may be the same, they need not point at the same location in memory.
Simply checks for nil and if length of text length is greater than 0 - not empty
if (textField.text && textField.text.length > 0)
{
/* not empty - do something */
}
else
{
/* what ever */
}
We already have inbuilt method that return boolean value that indicates whether the text-entry objects has any text or not.
// In Obj-C
if ([textField hasText]) {
//* Do Something you have text
}else{
/* what ever */
}
// In Swift
if textField.hasText {
//* Do Something you have text
}else{
/* what ever */
}
The Most efficent way to do this is by using this
// set it into an NSString
NSString *yourText = yourVariable.text;
if([theText length] == 0])
{
// Your Code if it is equal to zero
}
else
{
// of the field is not empty
}
Check whether text field is empty in Swift
@IBOutlet weak var textField: NSTextField!
@IBOutlet weak var multiLineTextField: NSTextField!
@IBAction func textChanged(sender: AnyObject) {
//println("text changed! \(textField.stringValue)")
if textField.stringValue.isEmpty == false {
multiLineTextField.becomeFirstResponder()
multiLineTextField.editable = true
} else {
multiLineTextField.editable = false
}
}
-(void)insert{
if ([_nameofView isEqual: @""]) {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert"
message:@"Fill the Name Field First."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
else if ([_detailofview isEqual: @""]){
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert"
message:@"Fill the Details Field First."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
else{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert"
message:@"Data Inserted Successfully."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
}