Try this
NSString *emaill = [contact valueForKey:@"email"];
if (emaill.length>0)//if exists
{
//your code here
}
else
{
NSLog(@"Empty!!!");
}
Hope it help!
문제
I made a contacts app for fun to dive into iOS.
Basically when you add a contact you can call email or text them.
Since i use the same viewcontroller when adding them and viewing them, how can i make it so that the buttons are disabled till the contact is viewed ie. coredata attributes not null?
For instance say I go to add a new contact, all the fields blank and i hit the email button, the app will crash since there is no entity to email cause the contact is not saved yet.
here is my working email code: I need it to not activate if the value for key email is null or empty to avoid a crash.
NSString *emaill = [contact valueForKey:@"email"];
MFMailComposeViewController *mailcontroller = [[MFMailComposeViewController alloc] init];
[mailcontroller setMailComposeDelegate:self];
NSString *email =[@""stringByAppendingString:emaill];
NSArray *emailArray = [[NSArray alloc] initWithObjects:email, nil];
[mailcontroller setToRecipients:emailArray];
[mailcontroller setSubject:@""];
[self presentViewController:mailcontroller animated:YES completion:nil];
해결책
Try this
NSString *emaill = [contact valueForKey:@"email"];
if (emaill.length>0)//if exists
{
//your code here
}
else
{
NSLog(@"Empty!!!");
}
Hope it help!
다른 팁
Just use an if statement to check if the string exists, if it exists then execute your code.
NSString *emaill = [contact valueForKey:@"email"];
if (emaill != nil) {
MFMail...
}