Xcode 11.2.1 "Expected :" error in code segment when running build - objective-c

I am using Xcode 11.2.1 and I am getting a parse error of Expected ':'.
The section of code is:
-(IBAction)webview:(id)sender {
if ([Web isEqualToString:#"Not Available"]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"This airport does not have a Web site." message:Web delegate:self cancelButtonTitle:#"Close",nil otherButtonTitles:nil];
[alert show];
[alert release];
} else {
AirportWebViewController *web = [[AirportWebViewController alloc] initWithNibName:#"AirportWebViewController" bundle:nil];
[web setTitle:#"Selected airport"];
[web setWeb:self.Web];
[self.navigationController pushViewController:web animated:YES];
}
}
Where have I missed the placement of the ":"?

cancelButtonTitle:#"Close",nil otherButtonTitles contains an extra nil, it should be
cancelButtonTitle:#"Close" otherButtonTitles

Related

UIAlertView is causing a EXC_BAD_ACCESS and app hang

Ive the following code which is working fine , but when I add UIAlertView its start giving me exc_bad_access and the app hanged ,
The main idea is to show alert when timestamp dedtected ,
any tips what im doing wrong ?
- (void) onPayload:(PayloadEvent *) event {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *header = #"[OnPayload] ";
if (event.payloadType == TYPE_IDENTIFIED) {
if ((event.contentID != -1) && (event.timeStamp == -1)) {
[mUI performSelectorOnMainThread: #selector(Trace:) withObject:[NSString stringWithFormat:#"%# StaticID detected: %x\t\tConfidence: %f\n", header,(int)event.contentID, event.confidence] waitUntilDone:NO];
}
if ((event.timeStamp != -1) && (event.contentID == -1)) {
[mUI performSelectorOnMainThread: #selector(Trace:) withObject:[NSString stringWithFormat:#"%# Timestamp detected: %f\t\tConfidence: %f\n", header, event.timeStamp, event.confidence] waitUntilDone:NO];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Poll" message:#"MY alert Poll" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
else if (event.payloadType == TYPE_NOT_IDENTIFIED) {
[mUI performSelectorOnMainThread: #selector(Trace:) withObject:[NSString stringWithFormat:#"%# Content not marked\n", header] waitUntilDone:NO];
}
else if (event.payloadType == TYPE_MARKED_BUT_NOT_IDENTIFIED) {
[mUI performSelectorOnMainThread: #selector(Trace:) withObject:[NSString stringWithFormat:#"%# Content marked but not identified\n", header] waitUntilDone:NO];
}
[event release];
[pool release];
}
My error appear here
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Poll" message:#"MY alert Poll" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show]; <<=====
[alert release];
Any code that changes the UI should be called from the main thread:
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Poll" message:#"MY alert Poll" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
});
On the contrary to some comments, the view that is displaying the alert will retain the UIAlertView instance, so there is no need to call autorelease instead of release.
Considering that you are calling -performSelectorOnMainThread a few times in the same method, I imagine it's being called on a background thread. UI changes must occur on the main thread, and thus, you should be calling [alert show]; on the main thread in order to bypass the error.
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:NO];

Objective C dismissViewControllerAnimated move last view down

I have two ViewController
VC1 adding VC2 as an subview to itselfe.
VC2 has a UIButton, which is calling the follow Code:
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:[NSString stringWithFormat:#"BlaBla."]];
NSArray *toRecipients = [NSArray arrayWithObjects:#"blabla#gmail.com", nil];
[mailer setToRecipients:toRecipients];
NSString *emailBody = [NSString stringWithFormat:#"Testtext1"];
[mailer setMessageBody:emailBody isHTML:NO];
[self presentViewController:mailer animated:YES completion:nil];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Fehler" message:#"Ihr Gerät unterstützt die gewünschte Funktion nicht" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}
After the Mail is done it calls the following:
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result)
{
case MFMailComposeResultCancelled:
{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hinweis"
message:#"Vorgang abgebrochen, es wurde keine E-Mail versendet."
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
}
break;
case MFMailComposeResultSaved:
{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hinweis"
message:#"Ihre E-Mail wurde gespeichert."
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
}
break;
case MFMailComposeResultSent:
{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hinweis"
message:#"Ihre E-Mail wird versendet."
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
}
break;
case MFMailComposeResultFailed:
{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hinweis"
message:#"Vorgang abgebrochen, die E-Mail konnte nicht gesendet werden."
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
}
break;
default:
{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hinweis"
message:#"E-Mail wurde nicht gesendet."
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
}
break;
}
[self dismissViewControllerAnimated:YES completion:nil];}
All works fine, but all UIButtons etc in VC2 are now moved, I guess, 20 pixels down.
I cant Imagin why, I guess the problem is I adding a ViewController as Subview to an other ViewController and calling there the methode. But I cant imagin how to solve the issue.
I had similar problem but parent view was moved 20 px up.
Solution was based on changing "Wants full screen". I had to set this values to YES but not to all child view controllers.

MFMailComposeViewController won't dismiss on iOS 6

In iOS 6 the presented MFMailComposeViewController will not dismiss if user attempts to send second email...
Everything works perfectly the first go around and email is sent. However, if email option is selected again the MFMailComposeViewController will not dismiss on cancel.
Here is how I implemented it:
- (IBAction)buttonEmailClick:(id)sender {
if (![MFMailComposeViewController canSendMail]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Can't send" message:#"This device is unable to send emails." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return;
}
NSDictionary *contactsInfo = [self contactsInfoFromPlistNamed:kConfigPlistName];
[mailComposeViewController setToRecipients:[NSArray arrayWithObject:[contactsInfo objectForKey:#"email"]]];
//[mailComposeViewController setSubject:kEmailDefaultSubject];
//[mailComposeViewController setMessageBody:text isHTML:NO];
[self presentModalViewController:mailComposeViewController animated:YES];
}
and then this:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
UIAlertView *alert = nil;
if (result == MFMailComposeResultSent) {
alert = [[UIAlertView alloc] initWithTitle:#"Sent" message:#"Your email was sent." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
}
else if (result == MFMailComposeResultFailed) {
alert = [[UIAlertView alloc] initWithTitle:#"Failed" message:#"An error occured and your email was not sent." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
}
[alert show];
[self dismissModalViewControllerAnimated:YES];
}
It works fine in iOS 5 but not in iOS 6. I have tried replacing with non deprecated methods for iOS 6, but it doesn't work.
Have you tried creating a fresh MFMailComposeViewController each time they go to send an email? I'm not sure if you should be reusing it.
You can try this:
MFMailComposeViewController * composer = [[MFMailComposeViewController alloc] init];
composer.delegate = self;
-(void)mailComposeController:didFinishWithResult:error: should be called if you assign that class to the delegate

UIAlertView and memory leak

UIAlertView* av = [UIAlertView alloc];
int a = [self somefunc];
if (a == 1)
{
[[av initWithTitle:nil message:#"test msg 1" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
}
else if (a == 2)
{
[[av initWithTitle:nil message:#"test msg 2" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
}
[av release];
when I run anlyze on this code i am getting the error "refence counted object is used after it is released" at line [av release];
can I know, where av got released, does show function of UIAlertView released av?
strangly the below code doesnt show any error when analyze tool is used
if (a == 1)
{
UIAlertView* av = [[UIAlertView alloc] initWithTitle:nil message:#"test msg 1" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[av show];
[av release];
}
else if (a == 2)
{
UIAlertView* av = [[UIAlertView alloc] initWithTitle:nil message:#"test msg 1" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[av show];
[av release];
}
You must always use the return value of any init call, because an init function can return a different value. Therefore, if you really want to separate alloc and init then you have to do it like this:
UIAlertView *av = [UIAlertView alloc];
// ...
av = [av initWithTitle:...]; // Might change the value of av !!
[av show];
[av release];
The "spurious release" in your code happens here:
[av initWithTitle:...]
because that might (as explained above) release av and return a different object.
In your first code the object "av" is not sure to be initialized, what if the value of a is not 1 0r 2? av will not be initialized so when your release it there will be some problems.
In your second code av's scope became more specific or local to the conditions of if and else. Thats why xcode is sure that av will be initialized and it is safe to relese av.
show function is not releasing the UIAlertView if it is the question here (in the second code).

Objective-c formatting style causes an error in a switch-case

I'm getting an error in my switch statement with some multi-line Objective-c code:
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultFailed:
// NSLog(#"Mail Failed");
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Error", #"Error")
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(#"OK", #"OK")
otherButtonTitles:nil];
[alert show];
[alert release];
break;
default:
break;
}
}
If I uncomment the line with the NSLog, it works fine. What's causing this error? Is there any way to use this kind of formatting?
You should not declare a variable in a switch case unless you introduce a scope.
case MFMailComposeResultFailed: { // <--
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Error", #"Error")
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(#"OK", #"OK")
otherButtonTitles:nil];
[alert show];
[alert release];
break;
} // <--
The actual error is because in the C standard (§6.8.1), a label can only be followed by a statement (NSLog(#"Mail Failed")), not a declaration (UIAlertView* alert = ...).
The issues is with how switch is defined. You can't have a variable declaration on the line following the case. You can fix it by wrapping the entire case in a new scope
case MFMailComposeResultFailed:
{
// NSLog(#"Mail Failed");
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Error", #"Error")
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(#"OK", #"OK")
otherButtonTitles:nil];
[alert show];
[alert release];
break;
}