Macro compiler error: "Parse Issue" when an argument name appears elsewhere - objective-c

I tried making a straightforward macro for creating and showing a simple "ok" dialog box in iOS:
#define ALERT_DIALOG(title,message) \
do\
{\
UIAlertView *alert_Dialog = [[UIAlertView alloc] initWithTitle:(title) message:(message) delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];\
[alert_Dialog show];\
} while ( 0 )
If I try to use it in my code:
ALERT_DIALOG(#"Warning", #"Message");
I get the error:
Parse Issue. Expected ']'
And the error seems to be pointing at the second # right before "Message".
However, if I simply copy paste the macro I do not get this error:
NSString *title = #"Warning";
NSString *message = #"Message";
do
{
UIAlertView *alert_Dialog = [[UIAlertView alloc] initWithTitle:(title) message:(message) delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_Dialog show];
} while ( 0 );
Is there something against using Objective-c constructs in macros? Or is that something else?

The problem with your macro is that both occurrences of message in
... [[UIAlertView alloc] initWithTitle:(title) message:(message) ...
are replaced by #"Message", resulting in
.... [[UIAlertView alloc] initWithTitle:(#"Warning") #"Message":(#"Message") ...
and that causes the syntax error.
I don't think that it is really worth defining this as a macro, but if you do, you have to use macro arguments which do not occur at places where they should not be expanded, e.g.
#define ALERT_DIALOG(__title__,__message__) \
do\
{\
UIAlertView *alert_Dialog = [[UIAlertView alloc] initWithTitle:(__title__) message:(__message__) delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];\
[alert_Dialog show];\
} while ( 0 )
or similar.

Instead of declaring it as a macro, you can declare it as a C function instead:
void ALERT_DIALOG(NSString *title, NSString *message) {
UIAlertView *alert_Dialog = [[UIAlertView alloc] initWithTitle:(title) message:(message) delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];\
[alert_Dialog show];
}

Related

Error message on Xcode that I don't understand

I am getting an error message on my Xcode - Objective C.
Arithmetic on pointer to interface "UILabel", which is not constant size for this architecture and platform.
if([self checkforwin]){
NSString*winner = nil;
if (playertoken==1)
winner =#"Player 2 Wins";
_result1 = _result1+1
else if (playertoken==2)
_result2 = _result2 +1
winner =#"Player 1 Wins";
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Result"
message: winner
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[self resetboard];
[_audioPlayer play];
[alert show];
Any Ideas how to get rid of them??
The error shows on both the result1 and result2. Trying to add a score to my game.
Use parenthesis while using condition. Also don't set string to nil, set it with empty string #""
if([self checkforwin]){
NSString *winner = #"";
if (playertoken==1) {
winner =#"Player 2 Wins";
_result1 = _result1 + 1;
}
else if (playertoken==2) {
winner =#"Player 1 Wins";
_result2 = _result2 + 1;
}
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Result"
message: winner
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[self resetboard];
[_audioPlayer play];
[alert show];
}
You are trying to add 1 to the UILabel. You need an integer variable to do the increment. Either declare the _result 1 and 2 as int type variable or declare a int variable and increment it, and pass it to UILabel .text property
_result1 = _result1+1
here _result1 must be a integer variable.
You need to something like:
Declare the result1 as int,
#property(assign) int result1;
and this line should be fine
_result1=_result1+1;

No visible #interface for 'UIAlertView' declares the selector :NSString stringWithUTF8String

i have an error which i donno how to fix it it's making me crazy. i have searched about the solution a lot but didn't find anything which works here .
here is the code and you can see the error in the picture .
thanks
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:[NSString stringWithUTF8String:title]
message:[NSString stringWithUTF8String:body]
delegate:nil
cancleButtonTitle:[NSString stringWithUTF8String:cancleLable]
otherButtonTitles:[NSString stringWithUTF8String:firstLable],
[NSString stringWithUTF8String:secondLable], nil];
[alert show];

How to show cyrillic messages in UIAlertView in apportable

When I try to show cyrillic chars or specific chars like "★" in UIAlertView* instead of messages it's show some random chars (maybe 1, maybe 10..).
Examples:
[[[[UIAlertView alloc] initWithTitle:#"Тест тема (cyr)"
message:#"Message with char ★"
delegate:self
cancelButtonTitle:#"Ок (cyr)"
otherButtonTitles:nil] autorelease] show];
In result I have:
Title: " 5 A B
Message: M
Button: :
There is a bug with non-ASCII characters in strings that is planned to be fixed in the Apportable platform in the next few months. In the meantime, a workaround is:
[[[[UIAlertView alloc] initWithTitle:[NSString stringWithUTF8String:#"Тест тема (cyr)"]
message:[NSString stringWithUTF8String:#"Message with char ★"]
delegate:self
cancelButtonTitle:[NSString stringWithUTF8String:#"Ок (cyr)"]
otherButtonTitles:nil] autorelease] show];

Randomizing Alert view text for Objective C

I am new to programming. I am having trouble finding out what all is wrong with this. It is an alert view that i am trying to randomize the text displayed in the message.
-(void)alert:(id)sender{
int randomNumber;
randomNumber = (randomNumber() %3 + 1);
NSLog(#"%i", randomNumber);
if (randomNumber == 1) {
self.YouWin.text = [NSString stringWithFormat:#"You Win"];
}
else if (randomNumber == 2) {
self.YouWin.text = [NSString stringWithFormat:#"You Lose"];
}
else if (randomNumber == 3) {
self.YouWin.text = [NSString stringWithFormat:#"Tie"];
}
NSLog(#"%#",YouWin);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello" message:[NSString stringWithFormat:#"%#",YouWin] delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
button.hidden = YES;
Try this one:
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello"
message:self.YouWin.text
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
You needed text-value stored in YouWin, but you passed the YouWin object itself.
*Note: you can use arc4random() for generating random numbers.
There are many good suggestions; I agree with AKB8085.
Replacing the randomNumber() with arc4random() will help at compile time.
But you might want to re-think implementing a random number generator. The reason is in fairness to your user. I pose the question, “Is it fair to assume you want your user to guess a number with this large of a number range?”
Did you Know?
Using the arc4random(3), rand(3), or random(3) you are using a C function.
You are asking the user to guess a number with the following ranges:
arc4random(3) = 0 to 4294967296
rand(3) and random(3) that top out at a RAND_MAX of 0x7fffffff (214748647)
To help in answering your question, answer the following requirement questions:
Is there a min/max range restraint?
What type of compelling delays will happen
by using arc4random(3), rand(3), or random(3)?
Is using NSArray like in the Fisher–Yates_shuffle a better answer?
SUGGESTION:
Read an article on random numbers and NSArray.
NOTE:
Random numbers tend to task the compiler and your user experience will be hindered.
As Anoop noted you are using stringWithFormat but you're not providing a format string at all.
You should do
[NSString stringWithFormat:#"%#", #"You Win"];
but that's extremely redundant, although correct, and it's totally equivalent to just using #"You Win".
Also an advice for the general approach on the problem. Instead of having a big if-else statement, it's better to store all your string into a data structure and then randomly access to it.
In code this would translate to
NSArray * outcomes = #[ #"You Win", #"You lose", #"Tie" ];
int randomIndex = arc4random_uniform(outcomes.count);
NSString * randomOutcome = outcomes[randomIndex];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello"
message:randomOutcome
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
button.hidden = YES;
Note the usage of arc4random_uniform() that gives you back a random number between 0 and the argument provided, excluded.
Replace
randomNumber = (randomNumber() %3 + 1);
with
randomNumber = arc4random() %3 + 1;
Also use this...
if (randomNumber == 1) {
self.YouWin.text = [NSString stringWithFormat:#"You Win"];
}
else if (randomNumber == 2) {
self.YouWin.text = [NSString stringWithFormat:#"You Lose"];
}
else if (randomNumber == 3) {
self.YouWin.text = [NSString stringWithFormat:#"Tie"];
}
NSLog(#"%#",YouWin);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello"
message:self.YouWin.text
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
button.hidden = YES;

Incompatible pointer type initializing TaggedUIActionAheet with an expression of type UIActionSheet

When I run this:
TaggedUIActionSheet *sheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Delete"
otherButtonTitles:#"Save to photos",#"Email",nil];
I get the following error :
Incompatible pointer type initializing TaggedUIActionAheet with an expression of type UIActionSheet
Spot the error in your first line:
TaggedUIActionSheet *sheet = [[UIActionSheet alloc]
Should be:
TaggedUIActionSheet *sheet = [[TaggedUIActionSheet alloc]
Try following:
UIActionSheet *Actionsheet =[[UIActionSheet alloc]initWithTitle:title delegate:delegate cancelButtonTitle:Canclebutton destructiveButtonTitle:destructivebutton otherButtonTitles:nil, nil];
[Actionsheet showInView:self.view];
-(void)Click_Button:(UIButton*)sender
{
[self ShowActionsheet:#"Do u Want to Delete This Record!!!" Delegate:self canclebutton:#"Cancle" Destructivebutton:#"Yes"];
}